summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-11-20 17:08:18 +0100
committerGitHub <noreply@github.com>2016-11-20 17:08:18 +0100
commit0310c2176f342fb42a55b5b3f025843bb8cf6a49 (patch)
treec4b78dc0397b253580d2b13df4c46df43828b5c2
parent4ca560af7ff82d4dc9fabc5ef9abc579f288d3d7 (diff)
parentc4b1f7ac8d16e6b2572f6c546f9220a9ba79fa25 (diff)
downloadimag-0310c2176f342fb42a55b5b3f025843bb8cf6a49.zip
imag-0310c2176f342fb42a55b5b3f025843bb8cf6a49.tar.gz
Merge pull request #818 from rnestler/libimagdiary/refactor_from_store_id
[WIP] Use Result for from_storeid
-rw-r--r--libimagdiary/src/diaryid.rs88
-rw-r--r--libimagdiary/src/error.rs3
-rw-r--r--libimagdiary/src/iter.rs8
3 files changed, 61 insertions, 38 deletions
diff --git a/libimagdiary/src/diaryid.rs b/libimagdiary/src/diaryid.rs
index ad91ccf..906097d 100644
--- a/libimagdiary/src/diaryid.rs
+++ b/libimagdiary/src/diaryid.rs
@@ -30,6 +30,11 @@ 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 error::MapErrInto;
+use libimagerror::into::IntoError;
+
use module_path::ModuleEntryPath;
#[derive(Debug, Clone)]
@@ -175,62 +180,77 @@ impl Into<NaiveDateTime> for DiaryId {
pub trait FromStoreId : Sized {
- fn from_storeid(&StoreId) -> Option<Self>;
+ fn from_storeid(&StoreId) -> Result<Self, DE>;
}
use std::path::Component;
-fn component_to_str<'a>(com: Component<'a>) -> Option<&'a str> {
+fn component_to_str<'a>(com: Component<'a>) -> Result<&'a str, DE> {
match com {
Component::Normal(s) => Some(s),
- _ => None
+ _ => None,
}.and_then(|s| s.to_str())
+ .ok_or(DEK::IdParseError.into_error())
}
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::path::Components;
+ use std::iter::Rev;
+
+ fn next_component<'a>(components: &'a mut Rev<Components>) -> Result<&'a str, DE> {
+ components.next()
+ .ok_or(DEK::IdParseError.into_error())
+ .and_then(component_to_str)
+ }
+
let mut cmps = s.components().rev();
- let (hour, minute) = match cmps.next().and_then(component_to_str)
- .and_then(|time| {
- let mut time = time.split(":");
- let hour = time.next().and_then(|s| FromStr::from_str(s).ok());
- let minute = time.next()
- .and_then(|s| s.split("~").next())
- .and_then(|s| FromStr::from_str(s).ok());
-
- debug!("Hour = {:?}", hour);
- debug!("Minute = {:?}", minute);
-
- match (hour, minute) {
- (Some(h), Some(m)) => Some((h, m)),
- _ => None,
- }
- })
- {
- Some(s) => s,
- None => return None,
- };
-
- let day :Option<u32> = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok());
- let month :Option<u32> = cmps.next().and_then(component_to_str).and_then(|s| FromStr::from_str(s).ok());
- let year :Option<i32> = 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 (hour, minute) = try!(next_component(&mut cmps).and_then(|time| {
+ let mut time = time.split(":");
+ let hour = time.next().and_then(|s| FromStr::from_str(s).ok());
+ let minute = time.next()
+ .and_then(|s| s.split("~").next())
+ .and_then(|s| FromStr::from_str(s).ok());
+
+ debug!("Hour = {:?}", hour);
+ debug!("Minute = {:?}", minute);
+
+ match (hour, minute) {
+ (Some(h), Some(m)) => Ok((h, m)),
+ _ => return Err(DE::new(DEK::IdParseError, None)),
+ }
+ }));
+
+ let day: Result<u32,_> = next_component(&mut cmps)
+ .and_then(|s| s.parse::<u32>()
+ .map_err_into(DEK::IdParseError));
+
+ let month: Result<u32,_> = next_component(&mut cmps)
+ .and_then(|s| s.parse::<u32>()
+ .map_err_into(DEK::IdParseError));
+
+ let year: Result<i32,_> = next_component(&mut cmps)
+ .and_then(|s| s.parse::<i32>()
+ .map_err_into(DEK::IdParseError));
+
+ let name = next_component(&mut cmps).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);
- 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 b6dc2bf..b5406b1 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",
+ IdParseError => "Error while parsing ID"
);
);
diff --git a/libimagdiary/src/iter.rs b/libimagdiary/src/iter.rs
index 73f180d..de84987 100644
--- a/libimagdiary/src/iter.rs
+++ b/libimagdiary/src/iter.rs
@@ -30,6 +30,7 @@ use entry::Entry as DiaryEntry;
use error::DiaryError as DE;
use error::DiaryErrorKind as DEK;
use result::Result;
+use libimagerror::trace::trace_error;
/// A iterator for iterating over diary entries
pub struct DiaryEntryIterator<'a> {
@@ -99,9 +100,10 @@ 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) => {
+ trace_error(&e);
+ debug!("Couldn't parse {:?} into DiaryId: {:?}", next, e);
continue;
}
};