Remove duplicated is_tag() function implementation

This commit is contained in:
Matthias Beyer 2017-07-19 00:26:40 +02:00
parent 798b95785a
commit e7bd43718d
4 changed files with 11 additions and 18 deletions

View file

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

View file

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

View file

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

View file

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