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,33 +49,39 @@ 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...");
if !tags.iter().all(|t| is_match!(*t, Value::String(_))) { val.as_array()
return Err(TagErrorKind::TagTypeError.into()); .map(|tags| {
} debug!("Got Array<T> of tags...");
if tags.iter().any(|t| match *t { if !tags.iter().all(|t| is_match!(*t, Value::String(_))) {
Value::String(ref s) => !is_tag_str(s).is_ok(), debug!("Got Array<T>, T != String of tags: {:?}", tags);
_ => unreachable!()}) return Err(TagErrorKind::TagTypeError.into());
{
return Err(TagErrorKind::NotATag.into());
}
Ok(tags.iter()
.cloned()
.map(|t| {
match t {
Value::String(s) => s,
_ => unreachable!(),
} }
debug!("Got Array<String> of tags...");
if tags.iter().any(|t| match *t {
Value::String(ref s) => !is_tag_str(s).is_ok(),
_ => unreachable!()})
{
debug!("At least one tag is not a valid tag string");
return Err(TagErrorKind::NotATag.into());
}
Ok(tags.iter()
.cloned()
.map(|t| {
match t {
Value::String(s) => s,
_ => unreachable!(),
}
})
.collect())
}) })
.collect()) .unwrap_or(Ok(vec![]))
}, })
None => Ok(vec![]), .unwrap_or(Ok(vec![]))
_ => Err(TagErrorKind::TagTypeError.into()),
}
} }
fn set_tags(&mut self, ts: &[Tag]) -> Result<()> { fn set_tags(&mut self, ts: &[Tag]) -> Result<()> {