diff --git a/libimagdiary/src/diaryid.rs b/libimagdiary/src/diaryid.rs index ad91ccf3..2a4ef8fe 100644 --- a/libimagdiary/src/diaryid.rs +++ b/libimagdiary/src/diaryid.rs @@ -30,6 +30,9 @@ use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; use libimagstore::store::Result as StoreResult; +use error::DiaryError as DE; +use error::DiaryErrorKind as DEK; + use module_path::ModuleEntryPath; #[derive(Debug, Clone)] @@ -175,7 +178,7 @@ impl Into for DiaryId { pub trait FromStoreId : Sized { - fn from_storeid(&StoreId) -> Option; + fn from_storeid(&StoreId) -> Result; } @@ -190,7 +193,7 @@ fn component_to_str<'a>(com: Component<'a>) -> Option<&'a str> { impl FromStoreId for DiaryId { - fn from_storeid(s: &StoreId) -> Option { + fn from_storeid(s: &StoreId) -> Result { use std::str::FromStr; let mut cmps = s.components().rev(); @@ -212,25 +215,37 @@ impl FromStoreId for DiaryId { }) { Some(s) => s, - None => return None, + None => return Err(DE::new(DEK::ParseError, None)), }; - let day :Option = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok()); - let month :Option = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok()); - let year :Option = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok()); - let name = cmps.next().and_then(component_to_str).map(String::from); + let day: Result = cmps.next().and_then(component_to_str) + .ok_or(DE::new(DEK::ParseError, None)) + .and_then(|s| s.parse::() + .map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e))))); + + let month: Result = cmps.next().and_then(component_to_str) + .ok_or(DE::new(DEK::ParseError, None)) + .and_then(|s| s.parse::() + .map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e))))); + + let year: Result = cmps.next().and_then(component_to_str) + .ok_or(DE::new(DEK::ParseError, None)) + .and_then(|s| s.parse::() + .map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e))))); + + let name = cmps.next().and_then(component_to_str).map(String::from); debug!("Day = {:?}", day); debug!("Month = {:?}", month); debug!("Year = {:?}", year); debug!("Name = {:?}", name); - let day = match day { None => return None, Some(day) => day }; - let month = match month { None => return None, Some(month) => month }; - let year = match year { None => return None, Some(year) => year }; - let name = match name { None => return None, Some(name) => name }; + let day = try!(day); + let month = try!(month); + let year = try!(year); + 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)) } } diff --git a/libimagdiary/src/error.rs b/libimagdiary/src/error.rs index b6dc2bfd..d70f8967 100644 --- a/libimagdiary/src/error.rs +++ b/libimagdiary/src/error.rs @@ -27,7 +27,8 @@ generate_error_module!( PathConversionError => "Error while converting paths internally", EntryNotInDiary => "Entry not in Diary", IOError => "IO Error", - ViewError => "Error viewing diary entry" + ViewError => "Error viewing diary entry", + ParseError => "Error while parsing" ); ); diff --git a/libimagdiary/src/iter.rs b/libimagdiary/src/iter.rs index 73f180d2..6a50819d 100644 --- a/libimagdiary/src/iter.rs +++ b/libimagdiary/src/iter.rs @@ -99,9 +99,9 @@ impl<'a> Iterator for DiaryEntryIterator<'a> { if next.is_in_diary(self.name) { debug!("Seems to be in diary: {:?}", next); let id = match DiaryId::from_storeid(&next) { - Some(i) => i, - None => { - debug!("Couldn't parse {:?} into DiaryId", next); + Ok(i) => i, + Err(e) => { + debug!("Couldn't parse {:?} into DiaryId: {:?}", next, e); continue; } };