diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-02-14 19:59:34 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-02-14 20:15:33 +0100 |
commit | f4e7969039cb86cc0e1787fe6dc1d5f969334b09 (patch) | |
tree | 088d4c73c0b93aeaa87a2d6dda371fca2625ceaf /libimagtag | |
parent | 31b966b9b435cf1e56b30330dc7e091933852fad (diff) |
Implement: check::has_tag{,s}()
Diffstat (limited to 'libimagtag')
-rw-r--r-- | libimagtag/src/check.rs | 45 | ||||
-rw-r--r-- | libimagtag/src/error.rs | 5 |
2 files changed, 45 insertions, 5 deletions
diff --git a/libimagtag/src/check.rs b/libimagtag/src/check.rs index 473df2e4..ecc092dc 100644 --- a/libimagtag/src/check.rs +++ b/libimagtag/src/check.rs @@ -1,14 +1,51 @@ +use toml::Value; -use libimagstore::store::Entry; +use libimagstore::store::{Entry, EntryHeader}; use result::Result; use tag::Tag; +use error::{TagError, TagErrorKind}; pub fn has_tag(e: &Entry, t: &Tag) -> Result<bool> { - unimplemented!() + header_has_tag(e.get_header(), t) } -pub fn has_tags(e: &Entry, ts: &Vec<Tag>) -> Result<bool> { - unimplemented!() +pub fn has_tags(e: &Entry, tags: &Vec<Tag>) -> Result<bool> { + let hdr = e.get_header(); + let mut result = true; + + for tag in tags { + let check = header_has_tag(hdr, tag); + if check.is_err() { + return Err(check.err().unwrap()); + } + let check = check.unwrap(); + + result = result && check; + } + + Ok(result) +} + +fn header_has_tag(head: &EntryHeader, t: &Tag) -> Result<bool> { + let tags = head.read("imag.tags"); + if tags.is_err() { + let kind = TagErrorKind::HeaderReadError; + return Err(TagError::new(kind, Some(Box::new(tags.err().unwrap())))); + } + let tags = tags.unwrap(); + + if !tags.iter().all(|t| match t { &Value::String(_) => true, _ => false }) { + return Err(TagError::new(TagErrorKind::TagTypeError, None)); + } + + Ok(tags + .iter() + .any(|tag| { + match tag { + &Value::String(ref s) => { s == t }, + _ => unreachable!() + } + })) } diff --git a/libimagtag/src/error.rs b/libimagtag/src/error.rs index 321ecf1a..58615a6a 100644 --- a/libimagtag/src/error.rs +++ b/libimagtag/src/error.rs @@ -6,11 +6,14 @@ use std::fmt; #[derive(Clone, Copy, Debug, PartialEq)] pub enum TagErrorKind { + TagTypeError, + HeaderReadError, } 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", } } |