From 7a23097b00edd98f33db2b10da7d6707a51b65f1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 27 Sep 2016 20:12:18 +0200 Subject: [PATCH 01/11] Add dependencies --- libimagutil/Cargo.toml | 1 + libimagutil/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/libimagutil/Cargo.toml b/libimagutil/Cargo.toml index 87be5306..b7a20fc0 100644 --- a/libimagutil/Cargo.toml +++ b/libimagutil/Cargo.toml @@ -4,6 +4,7 @@ version = "0.2.0" authors = ["Matthias Beyer "] [dependencies] +url = "1.1" lazy_static = "0.1.15" log = "0.3" regex = "0.1" diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index c0026c6f..901f7f5c 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -16,6 +16,7 @@ #[macro_use] extern crate lazy_static; #[macro_use] extern crate log; extern crate regex; +extern crate url; extern crate tempfile; #[macro_use] mod log_result; From 719e49271cd0abf4f0e06874b7aafc8192707ee0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 27 Sep 2016 20:12:47 +0200 Subject: [PATCH 02/11] Add module: cli_validators --- libimagutil/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index 901f7f5c..ac1801f7 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -20,6 +20,7 @@ extern crate url; extern crate tempfile; #[macro_use] mod log_result; +pub mod cli_validators; pub mod debug_result; pub mod edit; pub mod info_result; From 7482ffb5b3c75cd2508983ad40574cb1833fbe50 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 27 Sep 2016 20:13:51 +0200 Subject: [PATCH 03/11] Add deps --- libimagutil/Cargo.toml | 1 + libimagutil/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/libimagutil/Cargo.toml b/libimagutil/Cargo.toml index b7a20fc0..61dd88e7 100644 --- a/libimagutil/Cargo.toml +++ b/libimagutil/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Matthias Beyer "] [dependencies] url = "1.1" +boolinator = "2.4.0" lazy_static = "0.1.15" log = "0.3" regex = "0.1" diff --git a/libimagutil/src/lib.rs b/libimagutil/src/lib.rs index ac1801f7..72e6b29a 100644 --- a/libimagutil/src/lib.rs +++ b/libimagutil/src/lib.rs @@ -17,6 +17,7 @@ #[macro_use] extern crate log; extern crate regex; extern crate url; +extern crate boolinator; extern crate tempfile; #[macro_use] mod log_result; From 11490b232c71e3ee03d8f8c424902465fb3f7ae3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 27 Sep 2016 20:18:13 +0200 Subject: [PATCH 04/11] Add implementation for validators --- libimagutil/src/cli_validators.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 libimagutil/src/cli_validators.rs diff --git a/libimagutil/src/cli_validators.rs b/libimagutil/src/cli_validators.rs new file mode 100644 index 00000000..20c4f0b8 --- /dev/null +++ b/libimagutil/src/cli_validators.rs @@ -0,0 +1,26 @@ +//! Functions to be used for clap::Arg::validator() +//! to validate arguments + +use std::path::PathBuf; +use boolinator::Boolinator; + +pub fn is_file(s: String) -> Result<(), String> { + PathBuf::from(s.clone()).is_file().as_result((), format!("Not a File: {}", s)) +} + +pub fn is_directory(s: String) -> Result<(), String> { + PathBuf::from(s.clone()).is_dir().as_result((), format!("Not a Directory: {}", s)) +} + +pub fn is_integer(s: String) -> Result<(), String> { + use std::str::FromStr; + + let i : Result = FromStr::from_str(&s); + i.map(|_| ()).map_err(|_| format!("Not an integer: {}", s)) +} + +pub fn is_url(s: String) -> Result<(), String> { + use url::Url; + Url::parse(&s).map(|_| ()).map_err(|_| format!("Not a URL: {}", s)) +} + From 6999f95c4d7653b9c44960a9cb54b8675f428269 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 28 Sep 2016 19:44:23 +0200 Subject: [PATCH 05/11] imag-bookmark: Validate URLs --- imag-bookmark/src/ui.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/imag-bookmark/src/ui.rs b/imag-bookmark/src/ui.rs index 701cdbba..6c49ba72 100644 --- a/imag-bookmark/src/ui.rs +++ b/imag-bookmark/src/ui.rs @@ -1,6 +1,7 @@ use clap::{Arg, App, SubCommand}; use libimagentrytag::ui::tag_add_arg; +use libimagutil::cli_validators::*; pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app @@ -22,6 +23,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .required(true) .multiple(true) .value_name("URL") + .validator(is_url) .help("Add this URL, multiple possible")) .arg(tag_add_arg()) ) @@ -44,6 +46,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .required(true) .multiple(true) .value_name("URL") + .validator(is_url) .help("Remove these urls, regex supported")) ) From d6f705dc9036bcb9777530aa259c83ff0e11293d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 28 Sep 2016 19:51:21 +0200 Subject: [PATCH 06/11] Add is_tag clap validator helper --- libimagutil/src/cli_validators.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libimagutil/src/cli_validators.rs b/libimagutil/src/cli_validators.rs index 20c4f0b8..ed102ca5 100644 --- a/libimagutil/src/cli_validators.rs +++ b/libimagutil/src/cli_validators.rs @@ -24,3 +24,12 @@ 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)) +} + From da439e60e996c8eac0ff07c4fe8c74e368991a3a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 28 Sep 2016 19:53:59 +0200 Subject: [PATCH 07/11] Add documentation what a valid tag is --- doc/src/04020-module-tag.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/src/04020-module-tag.md b/doc/src/04020-module-tag.md index 9e644efd..08c66ecf 100644 --- a/doc/src/04020-module-tag.md +++ b/doc/src/04020-module-tag.md @@ -2,6 +2,8 @@ The Tagging module. +A valid tag matches the regex `[a-zA-Z][0-9a-zA-Z]*`. + ### Description From 3d06933860f9286ef6117f43e7fc8aca9615c843 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 28 Sep 2016 19:55:57 +0200 Subject: [PATCH 08/11] Add is_tag validator --- libimagentrytag/src/ui.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libimagentrytag/src/ui.rs b/libimagentrytag/src/ui.rs index e74b1ac9..497eb596 100644 --- a/libimagentrytag/src/ui.rs +++ b/libimagentrytag/src/ui.rs @@ -2,6 +2,8 @@ use clap::{Arg, ArgMatches, App, SubCommand}; use tag::Tag; +use libimagutil::cli_validators::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. pub fn tag_subcommand<'a, 'b>() -> App<'a, 'b> { @@ -20,6 +22,7 @@ pub fn tag_add_arg<'a, 'b>() -> Arg<'a, 'b> { .takes_value(true) .value_name("tags") .multiple(true) + .validator(is_tag) .help("Add tags, seperated by comma or by specifying multiple times") } @@ -30,6 +33,7 @@ pub fn tag_remove_arg<'a, 'b>() -> Arg<'a, 'b> { .takes_value(true) .value_name("tags") .multiple(true) + .validator(is_tag) .help("Remove tags, seperated by comma or by specifying multiple times") } @@ -57,6 +61,7 @@ pub fn tag_argument<'a, 'b>() -> Arg<'a, 'b> { .long("tags") .takes_value(true) .multiple(true) + .validator(is_tag) .help("Add or remove tags, prefixed by '+' (for adding) or '-' (for removing)") } From f8ed44c1e70fd6c2c1c8d6b021f4e43d7c99c17a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 30 Sep 2016 10:47:11 +0200 Subject: [PATCH 09/11] Add cli validator: is_existing_path() --- libimagutil/src/cli_validators.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libimagutil/src/cli_validators.rs b/libimagutil/src/cli_validators.rs index ed102ca5..068d5834 100644 --- a/libimagutil/src/cli_validators.rs +++ b/libimagutil/src/cli_validators.rs @@ -4,6 +4,10 @@ use std::path::PathBuf; use boolinator::Boolinator; +pub fn is_existing_path(s: String) -> Result<(), String> { + PathBuf::from(s.clone()).exists().as_result((), format!("Not a File or Directory: {}", s)) +} + pub fn is_file(s: String) -> Result<(), String> { PathBuf::from(s.clone()).is_file().as_result((), format!("Not a File: {}", s)) } From 4d2ac38ceb2a7fcbf47b8f6d5e7857602b4a3cc0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 30 Sep 2016 10:47:38 +0200 Subject: [PATCH 10/11] imag-ref: Use validator is_existing_path --- imag-ref/src/ui.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/imag-ref/src/ui.rs b/imag-ref/src/ui.rs index e884c90f..37cbb799 100644 --- a/imag-ref/src/ui.rs +++ b/imag-ref/src/ui.rs @@ -1,5 +1,7 @@ use clap::{Arg, App, SubCommand}; +use libimagutil::cli_validators::is_existing_path; + pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app .subcommand(SubCommand::with_name("add") @@ -11,6 +13,7 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .takes_value(true) .required(true) .help("The path of the file") + .validator(is_existing_path) .value_name("PATH")) .arg(Arg::with_name("track-content") .long("content-hash") From d506e869ca62e740c6895d72decf70db719b634d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 7 Oct 2016 17:35:43 +0200 Subject: [PATCH 11/11] Add dependency: libimagutil --- imag-ref/Cargo.toml | 3 +++ imag-ref/src/main.rs | 1 + 2 files changed, 4 insertions(+) diff --git a/imag-ref/Cargo.toml b/imag-ref/Cargo.toml index da41fdcf..221615a2 100644 --- a/imag-ref/Cargo.toml +++ b/imag-ref/Cargo.toml @@ -27,3 +27,6 @@ path = "../libimaginteraction" [dependencies.libimagentrylist] path = "../libimagentrylist" +[dependencies.libimagutil] +path = "../libimagutil" + diff --git a/imag-ref/src/main.rs b/imag-ref/src/main.rs index fd29a4a6..54465494 100644 --- a/imag-ref/src/main.rs +++ b/imag-ref/src/main.rs @@ -9,6 +9,7 @@ extern crate libimagref; extern crate libimagerror; extern crate libimagentrylist; extern crate libimaginteraction; +extern crate libimagutil; mod ui; use ui::build_ui;