summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-06-09 14:55:34 +0200
committerGitHub <noreply@github.com>2017-06-09 14:55:34 +0200
commit15b77ac2c140dc14f6670a05e2b0f324165c8b2f (patch)
tree94d41fddf98989ab0fe634a60784aa11960b2295
parentef82b2ab415c7264109cf21d5c60f2a85340d627 (diff)
parent98c33f7571731e9ad0037d498af8d5a233f5de1d (diff)
downloadimag-15b77ac2c140dc14f6670a05e2b0f324165c8b2f.zip
imag-15b77ac2c140dc14f6670a05e2b0f324165c8b2f.tar.gz
Merge pull request #965 from matthiasbeyer/libimagentrytag/clap-validators
Add clap validator for tag string
-rw-r--r--libimagentrytag/Cargo.toml1
-rw-r--r--libimagentrytag/src/lib.rs1
-rw-r--r--libimagentrytag/src/tag.rs16
3 files changed, 18 insertions, 0 deletions
diff --git a/libimagentrytag/Cargo.toml b/libimagentrytag/Cargo.toml
index 69de999..97cdde6 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 d933fb9..6cac969 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 28ecd93..cf3d676 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))
+ }
+}
+