Return Result from component_to_str

This commit is contained in:
Raphael Nestler 2016-10-19 23:31:12 +02:00
parent 841c82b950
commit 42b0dd6083

View file

@ -184,11 +184,12 @@ pub trait FromStoreId : Sized {
use std::path::Component; 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 { match com {
Component::Normal(s) => Some(s), Component::Normal(s) => Some(s),
_ => None _ => None,
}.and_then(|s| s.to_str()) }.and_then(|s| s.to_str())
.ok_or(DE::new(DEK::ParseError, None))
} }
impl FromStoreId for DiaryId { impl FromStoreId for DiaryId {
@ -197,7 +198,9 @@ impl FromStoreId for DiaryId {
use std::str::FromStr; use std::str::FromStr;
let mut cmps = s.components().rev(); let mut cmps = s.components().rev();
let (hour, minute) = match cmps.next().and_then(component_to_str) let (hour, minute) = try!(cmps.next()
.ok_or(DE::new(DEK::ParseError, None))
.and_then(component_to_str)
.and_then(|time| { .and_then(|time| {
let mut time = time.split(":"); let mut time = time.split(":");
let hour = time.next().and_then(|s| FromStr::from_str(s).ok()); let hour = time.next().and_then(|s| FromStr::from_str(s).ok());
@ -209,31 +212,32 @@ impl FromStoreId for DiaryId {
debug!("Minute = {:?}", minute); debug!("Minute = {:?}", minute);
match (hour, minute) { match (hour, minute) {
(Some(h), Some(m)) => Some((h, m)), (Some(h), Some(m)) => Ok((h, m)),
_ => None, _ => return Err(DE::new(DEK::ParseError, None)),
} }
}) }));
{
Some(s) => s,
None => return Err(DE::new(DEK::ParseError, None)),
};
let day: Result<u32,_> = cmps.next().and_then(component_to_str) let day: Result<u32,_> = cmps.next()
.ok_or(DE::new(DEK::ParseError, None)) .ok_or(DE::new(DEK::ParseError, None))
.and_then(component_to_str)
.and_then(|s| s.parse::<u32>() .and_then(|s| s.parse::<u32>()
.map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e))))); .map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e)))));
let month: Result<u32,_> = cmps.next().and_then(component_to_str) let month: Result<u32,_> = cmps.next()
.ok_or(DE::new(DEK::ParseError, None)) .ok_or(DE::new(DEK::ParseError, None))
.and_then(component_to_str)
.and_then(|s| s.parse::<u32>() .and_then(|s| s.parse::<u32>()
.map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e))))); .map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e)))));
let year: Result<i32,_> = cmps.next().and_then(component_to_str) let year: Result<i32,_> = cmps.next()
.ok_or(DE::new(DEK::ParseError, None)) .ok_or(DE::new(DEK::ParseError, None))
.and_then(component_to_str)
.and_then(|s| s.parse::<i32>() .and_then(|s| s.parse::<i32>()
.map_err(|e| DE::new(DEK::ParseError, Some(Box::new(e))))); .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()
.ok_or(DE::new(DEK::ParseError, None))
.and_then(component_to_str).map(String::from);
debug!("Day = {:?}", day); debug!("Day = {:?}", day);
debug!("Month = {:?}", month); debug!("Month = {:?}", month);
@ -243,7 +247,7 @@ impl FromStoreId for DiaryId {
let day = try!(day); let day = try!(day);
let month = try!(month); let month = try!(month);
let year = try!(year); let year = try!(year);
let name = try!(name.ok_or(DE::new(DEK::ParseError, None))); let name = try!(name);
Ok(DiaryId::new(name, year, month, day, hour, minute)) Ok(DiaryId::new(name, year, month, day, hour, minute))
} }