Use Result for from_storeid

This commit is contained in:
Raphael Nestler 2016-10-19 22:24:33 +02:00
parent 4ca560af7f
commit 841c82b950
3 changed files with 32 additions and 16 deletions

View file

@ -30,6 +30,9 @@ use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
use libimagstore::store::Result as StoreResult; use libimagstore::store::Result as StoreResult;
use error::DiaryError as DE;
use error::DiaryErrorKind as DEK;
use module_path::ModuleEntryPath; use module_path::ModuleEntryPath;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -175,7 +178,7 @@ impl Into<NaiveDateTime> for DiaryId {
pub trait FromStoreId : Sized { pub trait FromStoreId : Sized {
fn from_storeid(&StoreId) -> Option<Self>; fn from_storeid(&StoreId) -> Result<Self, DE>;
} }
@ -190,7 +193,7 @@ fn component_to_str<'a>(com: Component<'a>) -> Option<&'a str> {
impl FromStoreId for DiaryId { impl FromStoreId for DiaryId {
fn from_storeid(s: &StoreId) -> Option<DiaryId> { fn from_storeid(s: &StoreId) -> Result<DiaryId, DE> {
use std::str::FromStr; use std::str::FromStr;
let mut cmps = s.components().rev(); let mut cmps = s.components().rev();
@ -212,12 +215,24 @@ impl FromStoreId for DiaryId {
}) })
{ {
Some(s) => s, Some(s) => s,
None => return None, None => return Err(DE::new(DEK::ParseError, None)),
}; };
let day :Option<u32> = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok()); let day: Result<u32,_> = cmps.next().and_then(component_to_str)
let month :Option<u32> = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok()); .ok_or(DE::new(DEK::ParseError, None))
let year :Option<i32> = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok()); .and_then(|s| s.parse::<u32>()
.map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e)))));
let month: Result<u32,_> = cmps.next().and_then(component_to_str)
.ok_or(DE::new(DEK::ParseError, None))
.and_then(|s| s.parse::<u32>()
.map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e)))));
let year: Result<i32,_> = cmps.next().and_then(component_to_str)
.ok_or(DE::new(DEK::ParseError, None))
.and_then(|s| s.parse::<i32>()
.map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e)))));
let name = cmps.next().and_then(component_to_str).map(String::from); let name = cmps.next().and_then(component_to_str).map(String::from);
debug!("Day = {:?}", day); debug!("Day = {:?}", day);
@ -225,12 +240,12 @@ impl FromStoreId for DiaryId {
debug!("Year = {:?}", year); debug!("Year = {:?}", year);
debug!("Name = {:?}", name); debug!("Name = {:?}", name);
let day = match day { None => return None, Some(day) => day }; let day = try!(day);
let month = match month { None => return None, Some(month) => month }; let month = try!(month);
let year = match year { None => return None, Some(year) => year }; let year = try!(year);
let name = match name { None => return None, Some(name) => name }; let name = try!(name.ok_or(DE::new(DEK::ParseError, None)));
Some(DiaryId::new(name, year, month, day, hour, minute)) Ok(DiaryId::new(name, year, month, day, hour, minute))
} }
} }

View file

@ -27,7 +27,8 @@ generate_error_module!(
PathConversionError => "Error while converting paths internally", PathConversionError => "Error while converting paths internally",
EntryNotInDiary => "Entry not in Diary", EntryNotInDiary => "Entry not in Diary",
IOError => "IO Error", IOError => "IO Error",
ViewError => "Error viewing diary entry" ViewError => "Error viewing diary entry",
ParseError => "Error while parsing"
); );
); );

View file

@ -99,9 +99,9 @@ impl<'a> Iterator for DiaryEntryIterator<'a> {
if next.is_in_diary(self.name) { if next.is_in_diary(self.name) {
debug!("Seems to be in diary: {:?}", next); debug!("Seems to be in diary: {:?}", next);
let id = match DiaryId::from_storeid(&next) { let id = match DiaryId::from_storeid(&next) {
Some(i) => i, Ok(i) => i,
None => { Err(e) => {
debug!("Couldn't parse {:?} into DiaryId", next); debug!("Couldn't parse {:?} into DiaryId: {:?}", next, e);
continue; continue;
} }
}; };