Implement: check::has_tag{,s}()
This commit is contained in:
parent
31b966b9b4
commit
f4e7969039
2 changed files with 45 additions and 5 deletions
|
@ -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!()
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
|
|
|
@ -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",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue