From 2ef57a69eda86d805261b77713de1b7545c04494 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 26 Jun 2019 20:08:42 +0200 Subject: [PATCH] Simplify implementation using toml-query Partial This patch rewrites the get_tags() function to use toml_query::read::Partial. The helper functions is_tag_str and is_tag are rewritten for compatiblity. Signed-off-by: Matthias Beyer --- lib/entry/libimagentrytag/Cargo.toml | 8 +++- lib/entry/libimagentrytag/src/lib.rs | 2 + lib/entry/libimagentrytag/src/tag.rs | 7 ++-- lib/entry/libimagentrytag/src/tagable.rs | 50 +++++++++--------------- 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/lib/entry/libimagentrytag/Cargo.toml b/lib/entry/libimagentrytag/Cargo.toml index 2b4fca20..9cda6b0b 100644 --- a/lib/entry/libimagentrytag/Cargo.toml +++ b/lib/entry/libimagentrytag/Cargo.toml @@ -26,12 +26,18 @@ toml = "0.5" itertools = "0.7" is-match = "0.1" filters = "0.3" -toml-query = "0.9" failure = "0.1" +serde = "1" +serde_derive = "1" libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" } +[dependencies.toml-query] +version = "0.9" +default-features = false +features = [ "typed" ] + [dependencies.clap] version = "^2.29" default-features = false diff --git a/lib/entry/libimagentrytag/src/lib.rs b/lib/entry/libimagentrytag/src/lib.rs index ba944409..93fcfeb0 100644 --- a/lib/entry/libimagentrytag/src/lib.rs +++ b/lib/entry/libimagentrytag/src/lib.rs @@ -43,6 +43,8 @@ extern crate itertools; extern crate regex; extern crate toml; extern crate toml_query; +extern crate serde; +#[macro_use] extern crate serde_derive; #[macro_use] extern crate is_match; extern crate filters; #[macro_use] extern crate failure; diff --git a/lib/entry/libimagentrytag/src/tag.rs b/lib/entry/libimagentrytag/src/tag.rs index 227bc546..c9607bb5 100644 --- a/lib/entry/libimagentrytag/src/tag.rs +++ b/lib/entry/libimagentrytag/src/tag.rs @@ -20,16 +20,17 @@ use std::result::Result; use regex::Regex; +use failure::Error; pub type Tag = String; pub type TagSlice<'a> = &'a str; /// validator which can be used by clap to validate that a string is a valid tag pub fn is_tag(s: String) -> Result<(), String> { - is_tag_str(&s) + is_tag_str(&s).map_err(|_| format!("The string '{}' is not a valid tag", s)) } -pub fn is_tag_str(s: &String) -> Result<(), String> { +pub fn is_tag_str(s: &String) -> Result<(), Error> { use filters::filter::Filter; trace!("Checking whether '{}' is a valid tag", s); @@ -41,7 +42,7 @@ pub fn is_tag_str(s: &String) -> Result<(), String> { if is_lower.and(no_whitespace).and(is_alphanum).and(matches_regex).filter(s) { Ok(()) } else { - Err(format!("The string '{}' is not a valid tag", s)) + Err(format_err!("The string '{}' is not a valid tag", s)) } } diff --git a/lib/entry/libimagentrytag/src/tagable.rs b/lib/entry/libimagentrytag/src/tagable.rs index 196b3f0d..7a7857ad 100644 --- a/lib/entry/libimagentrytag/src/tagable.rs +++ b/lib/entry/libimagentrytag/src/tagable.rs @@ -23,6 +23,7 @@ use libimagstore::store::Entry; use libimagerror::errors::ErrorMsg as EM; use toml_query::read::TomlValueReadExt; +use toml_query::read::Partial; use toml_query::insert::TomlValueInsertExt; use failure::Error; @@ -47,42 +48,29 @@ pub trait Tagable { } +#[derive(Serialize, Deserialize, Debug)] +struct TagHeader { + values: Vec, +} + +impl<'a> Partial<'a> for TagHeader { + const LOCATION: &'static str = "tags"; + type Output = Self; +} + impl Tagable for Value { fn get_tags(&self) -> Result> { - self.read("tag.values") - .context(format_err!("Failed to read header at 'tag.values'")) - .map_err(Error::from) - .context(EM::EntryHeaderReadError)? - .map(|val| { - debug!("Got Value of tags..."); - val.as_array() - .map(|tags| { - debug!("Got Array of tags..."); - if !tags.iter().all(|t| is_match!(*t, Value::String(_))) { - return Err(format_err!("Tag type error: Got Array where T is not a String: {:?}", tags)); - } - debug!("Got Array of tags..."); - if tags.iter().any(|t| match *t { - Value::String(ref s) => !is_tag_str(s).is_ok(), - _ => unreachable!()}) - { - return Err(format_err!("At least one tag is not a valid tag string")); - } + self.read_partial::()? + .map(|header| { + let _ = header.values + .iter() + .map(is_tag_str) + .collect::>()?; - Ok(tags.iter() - .cloned() - .map(|t| { - match t { - Value::String(s) => s, - _ => unreachable!(), - } - }) - .collect()) - }) - .unwrap_or(Ok(vec![])) + Ok(header.values) }) - .unwrap_or(Ok(vec![])) + .unwrap_or_else(|| Ok(vec![])) } fn set_tags(&mut self, ts: &[Tag]) -> Result<()> {