summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-07-19 00:26:40 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-07-19 00:31:27 +0200
commite7bd43718d6bf68142150ed1d86dd20594f677e3 (patch)
tree10c8ba191eb5c64c2d6851c1525d1c6af3527031
parent798b95785a990f817bf2543a63ea470e18ea9a60 (diff)
Remove duplicated is_tag() function implementation
-rw-r--r--libimagentrytag/src/tag.rs5
-rw-r--r--libimagentrytag/src/tagable.rs12
-rw-r--r--libimagentrytag/src/ui.rs3
-rw-r--r--libimagutil/src/cli_validators.rs9
4 files changed, 11 insertions, 18 deletions
diff --git a/libimagentrytag/src/tag.rs b/libimagentrytag/src/tag.rs
index 6cfcf1c8..be968da7 100644
--- a/libimagentrytag/src/tag.rs
+++ b/libimagentrytag/src/tag.rs
@@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
+use regex::Regex;
+
pub type Tag = String;
pub type TagSlice<'a> = &'a str;
@@ -31,8 +33,9 @@ pub fn is_tag_str(s: &String) -> Result<(), String> {
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());
+ let matches_regex = |s: &String| Regex::new("^[a-zA-Z]([a-zA-Z0-9_-]*)$").unwrap().captures(s).is_some();
- if is_lower.and(no_whitespace).and(is_alphanum).filter(s) {
+ if is_lower.and(no_whitespace).and(is_alphanum).and(matches_regex).filter(s) {
Ok(())
} else {
Err(format!("The string '{}' is not a valid tag", s))
diff --git a/libimagentrytag/src/tagable.rs b/libimagentrytag/src/tagable.rs
index 9e0fa2fd..bd53a122 100644
--- a/libimagentrytag/src/tagable.rs
+++ b/libimagentrytag/src/tagable.rs
@@ -27,7 +27,7 @@ use error::TagErrorKind;
use error::MapErrInto;
use result::Result;
use tag::{Tag, TagSlice};
-use util::is_tag;
+use tag::is_tag_str;
use toml::Value;
@@ -55,7 +55,7 @@ impl Tagable for Value {
return Err(TagErrorKind::TagTypeError.into());
}
if tags.iter().any(|t| match *t {
- Value::String(ref s) => !is_tag(s),
+ Value::String(ref s) => !is_tag_str(s).is_ok(),
_ => unreachable!()})
{
return Err(TagErrorKind::NotATag.into());
@@ -77,8 +77,8 @@ impl Tagable for Value {
}
fn set_tags(&mut self, ts: &[Tag]) -> Result<()> {
- if ts.iter().any(|tag| !is_tag(tag)) {
- debug!("Not a tag: '{}'", ts.iter().filter(|t| !is_tag(t)).next().unwrap());
+ if ts.iter().any(|tag| !is_tag_str(tag).is_ok()) {
+ debug!("Not a tag: '{}'", ts.iter().filter(|t| !is_tag_str(t).is_ok()).next().unwrap());
return Err(TagErrorKind::NotATag.into());
}
@@ -90,7 +90,7 @@ impl Tagable for Value {
}
fn add_tag(&mut self, t: Tag) -> Result<()> {
- if !is_tag(&t) {
+ if !try!(is_tag_str(&t).map(|_| true).map_err(|_| TagErrorKind::NotATag.into_error())) {
debug!("Not a tag: '{}'", t);
return Err(TagErrorKind::NotATag.into());
}
@@ -104,7 +104,7 @@ impl Tagable for Value {
}
fn remove_tag(&mut self, t: Tag) -> Result<()> {
- if !is_tag(&t) {
+ if !try!(is_tag_str(&t).map(|_| true).map_err(|_| TagErrorKind::NotATag.into_error())) {
debug!("Not a tag: '{}'", t);
return Err(TagErrorKind::NotATag.into());
}
diff --git a/libimagentrytag/src/ui.rs b/libimagentrytag/src/ui.rs
index dad4c87e..734708b3 100644
--- a/libimagentrytag/src/ui.rs
+++ b/libimagentrytag/src/ui.rs
@@ -20,8 +20,7 @@
use clap::{Arg, ArgMatches, App, SubCommand};
use tag::Tag;
-
-use libimagutil::cli_validators::is_tag;
+use tag::is_tag;
/// Generates a `clap::SubCommand` to be integrated in the commandline-ui builder for building a
/// "tags --add foo --remove bar" subcommand to do tagging action.
diff --git a/libimagutil/src/cli_validators.rs b/libimagutil/src/cli_validators.rs
index bbd376f2..24340a4d 100644
--- a/libimagutil/src/cli_validators.rs
+++ b/libimagutil/src/cli_validators.rs
@@ -47,12 +47,3 @@ pub fn is_url(s: String) -> Result<(), String> {
Url::parse(&s).map(|_| ()).map_err(|_| format!("Not a URL: {}", s))
}
-pub fn is_tag(s: String) -> Result<(), String> {
- use regex::Regex;
- lazy_static! { static ref TAG_RE : Regex = Regex::new("[:alpha:][:word:]*").unwrap(); }
-
- TAG_RE
- .is_match(&s)
- .as_result((), format!("Not a valid Tag: '{}' - Valid is [a-zA-Z][0-9a-zA-Z]*", s))
-}
-