Add clap validator for tag string

This commit is contained in:
Matthias Beyer 2017-06-09 13:48:24 +02:00
parent ef82b2ab41
commit 98c33f7571
3 changed files with 18 additions and 0 deletions

View file

@ -20,6 +20,7 @@ regex = "0.2"
toml = "^0.4"
itertools = "0.5"
is-match = "0.1"
filters = "0.1"
[dependencies.libimagstore]
path = "../libimagstore"

View file

@ -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;

View file

@ -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))
}
}