Refactoring: Use function chaining rather than matching

This commit is contained in:
Matthias Beyer 2018-01-04 23:09:30 +01:00
parent 66b0611103
commit 824f88e4fd

View file

@ -49,17 +49,23 @@ pub trait Tagable {
impl Tagable for Value { impl Tagable for Value {
fn get_tags(&self) -> Result<Vec<Tag>> { fn get_tags(&self) -> Result<Vec<Tag>> {
let tags = self.read("tag.values").chain_err(|| TagErrorKind::HeaderReadError)?; self.read("tag.values")
.chain_err(|| TagErrorKind::HeaderReadError)?
match tags { .map(|val| {
Some(&Value::Array(ref tags)) => { debug!("Got Value of tags...");
val.as_array()
.map(|tags| {
debug!("Got Array<T> of tags...");
if !tags.iter().all(|t| is_match!(*t, Value::String(_))) { if !tags.iter().all(|t| is_match!(*t, Value::String(_))) {
debug!("Got Array<T>, T != String of tags: {:?}", tags);
return Err(TagErrorKind::TagTypeError.into()); return Err(TagErrorKind::TagTypeError.into());
} }
debug!("Got Array<String> of tags...");
if tags.iter().any(|t| match *t { if tags.iter().any(|t| match *t {
Value::String(ref s) => !is_tag_str(s).is_ok(), Value::String(ref s) => !is_tag_str(s).is_ok(),
_ => unreachable!()}) _ => unreachable!()})
{ {
debug!("At least one tag is not a valid tag string");
return Err(TagErrorKind::NotATag.into()); return Err(TagErrorKind::NotATag.into());
} }
@ -72,10 +78,10 @@ impl Tagable for Value {
} }
}) })
.collect()) .collect())
}, })
None => Ok(vec![]), .unwrap_or(Ok(vec![]))
_ => Err(TagErrorKind::TagTypeError.into()), })
} .unwrap_or(Ok(vec![]))
} }
fn set_tags(&mut self, ts: &[Tag]) -> Result<()> { fn set_tags(&mut self, ts: &[Tag]) -> Result<()> {