diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-02-21 19:18:38 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-02-21 19:18:38 +0100 |
commit | 38ca44d10d6d87a3d18fafff84322d77e1d5fd89 (patch) | |
tree | 747cc27bb320c6f00a88c246726dc207c416727b /libimagtag | |
parent | f0424e9e60384d01d4c8b2797051d48c15c39e98 (diff) |
Add is_tag() checks
Diffstat (limited to 'libimagtag')
-rw-r--r-- | libimagtag/src/error.rs | 8 | ||||
-rw-r--r-- | libimagtag/src/tagable.rs | 22 |
2 files changed, 27 insertions, 3 deletions
diff --git a/libimagtag/src/error.rs b/libimagtag/src/error.rs index dcd36426..ed15e283 100644 --- a/libimagtag/src/error.rs +++ b/libimagtag/src/error.rs @@ -9,13 +9,15 @@ pub enum TagErrorKind { TagTypeError, HeaderReadError, HeaderWriteError, + NotATag, } fn tag_error_type_as_str(e: &TagErrorKind) -> &'static str { match e { - &TagErrorKind::TagTypeError => "Entry Header Tag Type wrong", - &TagErrorKind::HeaderReadError => "Error while reading entry header", - &TagErrorKind::HeaderWriteError=> "Error while writing entry header", + &TagErrorKind::TagTypeError => "Entry Header Tag Type wrong", + &TagErrorKind::HeaderReadError => "Error while reading entry header", + &TagErrorKind::HeaderWriteError => "Error while writing entry header", + &TagErrorKind::NotATag => "String is not a tag", } } diff --git a/libimagtag/src/tagable.rs b/libimagtag/src/tagable.rs index 3eca3856..f38f49d6 100644 --- a/libimagtag/src/tagable.rs +++ b/libimagtag/src/tagable.rs @@ -3,6 +3,7 @@ use libimagstore::store::{Entry, EntryHeader}; use error::{TagError, TagErrorKind}; use result::Result; use tag::Tag; +use util::is_tag; use toml::Value; @@ -34,6 +35,12 @@ impl Tagable for EntryHeader { if !tags.iter().all(|t| match t { &Value::String(_) => true, _ => false }) { return Err(TagError::new(TagErrorKind::TagTypeError, None)); } + if tags.iter().any(|t| match t { + &Value::String(ref s) => !is_tag(&s), + _ => unreachable!()}) + { + return Err(TagError::new(TagErrorKind::NotATag, None)); + } Ok(tags.iter() .cloned() @@ -51,6 +58,11 @@ impl Tagable for EntryHeader { } fn set_tags(&mut self, ts: Vec<Tag>) -> Result<()> { + if ts.iter().any(|tag| !is_tag(tag)) { + debug!("Not a tag: '{}'", ts.iter().filter(|t| !is_tag(t)).next().unwrap()); + return Err(TagError::new(TagErrorKind::NotATag, None)); + } + let a = ts.iter().map(|t| Value::String(t.clone())).collect(); self.set("imag.tags", Value::Array(a)) .map(|_| ()) @@ -58,6 +70,11 @@ impl Tagable for EntryHeader { } fn add_tag(&mut self, t: Tag) -> Result<()> { + if !is_tag(&t) { + debug!("Not a tag: '{}'", t); + return Err(TagError::new(TagErrorKind::NotATag, None)); + } + self.get_tags() .map(|mut tags| { tags.push(t); @@ -67,6 +84,11 @@ impl Tagable for EntryHeader { } fn remove_tag(&mut self, t: Tag) -> Result<()> { + if !is_tag(&t) { + debug!("Not a tag: '{}'", t); + return Err(TagError::new(TagErrorKind::NotATag, None)); + } + self.get_tags() .map(|mut tags| { tags.retain(|tag| tag.clone() != t); |