Add implementation for validators
This commit is contained in:
parent
7482ffb5b3
commit
11490b232c
1 changed files with 26 additions and 0 deletions
26
libimagutil/src/cli_validators.rs
Normal file
26
libimagutil/src/cli_validators.rs
Normal file
|
@ -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<i64, _> = 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))
|
||||
}
|
||||
|
Loading…
Reference in a new issue