Merge pull request #965 from matthiasbeyer/libimagentrytag/clap-validators

Add clap validator for tag string
This commit is contained in:
Matthias Beyer 2017-06-09 14:55:34 +02:00 committed by GitHub
commit 15b77ac2c1
3 changed files with 18 additions and 0 deletions

View File

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

View File

@ -37,6 +37,7 @@ extern crate itertools;
extern crate regex; extern crate regex;
extern crate toml; extern crate toml;
#[macro_use] extern crate is_match; #[macro_use] extern crate is_match;
extern crate filters;
extern crate libimagstore; extern crate libimagstore;
#[macro_use] extern crate libimagerror; #[macro_use] extern crate libimagerror;

View File

@ -19,3 +19,19 @@
pub type Tag = String; pub type Tag = String;
pub type TagSlice<'a> = &'a str; 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))
}
}