summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-11-11 19:24:51 +0100
committerMatthias Beyer <mail@beyermatthias.de>2019-11-11 19:48:09 +0100
commit61ade452de4694e8347be01db4914509847f8aba (patch)
tree3998f2424900536083c9410a73879ca8920dacf1
parent164880dbf9f43fd16890513bbb8be13b1ebd335f (diff)
Make tag-checking error message more explicit about what is wrong
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/entry/libimagentrytag/src/tag.rs18
1 files changed, 12 insertions, 6 deletions
diff --git a/lib/entry/libimagentrytag/src/tag.rs b/lib/entry/libimagentrytag/src/tag.rs
index 3cba2016..a2d68e73 100644
--- a/lib/entry/libimagentrytag/src/tag.rs
+++ b/lib/entry/libimagentrytag/src/tag.rs
@@ -26,21 +26,27 @@ 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).map_err(|_| format!("The string '{}' is not a valid tag", s))
+ check_tag_string(&s)
}
pub fn is_tag_str(s: &str) -> Result<(), Error> {
- use filters::filter::Filter;
+ check_tag_string(s).map_err(|s| format_err!("{}", s))
+}
+
+fn check_tag_string(s: &str) -> Result<(), String> {
trace!("Checking whether '{}' is a valid tag", s);
let is_lower = |s: &&str| s.chars().all(|c| c.is_lowercase());
let no_whitespace = |s: &&str| s.chars().all(|c| !c.is_whitespace());
let is_alphanum = |s: &&str| s.chars().all(|c| c.is_alphanumeric());
- if is_lower.and(no_whitespace).and(is_alphanum).filter(&s) {
- Ok(())
- } else {
- Err(format_err!("The string '{}' is not a valid tag", s))
+ match (is_lower(&s), no_whitespace(&s), is_alphanum(&s)) {
+ (true, true, true) => Ok(()),
+ (false, false, false) => Err(format!("The string '{}' is not valid, because it is not all-lowercase, has whitespace and is not alphanumeric", s)),
+ (false, false, _ ) => Err(format!("The string '{}' is not valid, because it is not all-lowercase and has whitespace", s)),
+ (false, _, _ ) => Err(format!("The string '{}' is not valid, because it is not all-lowercase", s)),
+ (_, false, _ ) => Err(format!("The string '{}' is not valid, because it has whitespace", s)),
+ (_, _, false) => Err(format!("The string '{}' is not valid, because it is not alphanumeric", s)),
}
}