diff --git a/libimagentrytag/Cargo.toml b/libimagentrytag/Cargo.toml index 69de9999..97cdde62 100644 --- a/libimagentrytag/Cargo.toml +++ b/libimagentrytag/Cargo.toml @@ -20,6 +20,7 @@ regex = "0.2" toml = "^0.4" itertools = "0.5" is-match = "0.1" +filters = "0.1" [dependencies.libimagstore] path = "../libimagstore" diff --git a/libimagentrytag/src/lib.rs b/libimagentrytag/src/lib.rs index d933fb90..6cac9690 100644 --- a/libimagentrytag/src/lib.rs +++ b/libimagentrytag/src/lib.rs @@ -37,6 +37,7 @@ extern crate itertools; extern crate regex; extern crate toml; #[macro_use] extern crate is_match; +extern crate filters; extern crate libimagstore; #[macro_use] extern crate libimagerror; diff --git a/libimagentrytag/src/tag.rs b/libimagentrytag/src/tag.rs index 28ecd934..cf3d6765 100644 --- a/libimagentrytag/src/tag.rs +++ b/libimagentrytag/src/tag.rs @@ -19,3 +19,19 @@ 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> { + use filters::filter::Filter; + + let is_lower = |s: &String| s.chars().all(|c| c.is_lowercase()); + let no_whitespace = |s: &String| s.chars().all(|c| !c.is_whitespace()); + let is_alphanum = |s: &String| s.chars().all(|c| c.is_alphanumeric()); + + if is_lower.and(no_whitespace).and(is_alphanum).filter(&s) { + Ok(()) + } else { + Err(format!("The string '{}' is not a valid tag", s)) + } +} +