diff options
author | dvvvvvv <dvvvvvv@dvvvvvv.com> | 2019-11-08 12:14:39 +0900 |
---|---|---|
committer | Abin Simon <abinsimon10@gmail.com> | 2019-12-16 16:41:50 +0530 |
commit | 50cf660567d2178f843df4f8ea7a3f2b9f60000e (patch) | |
tree | 8f38593b210c91aa0dc63a42ede5b73f8624b12b | |
parent | 698bc2ef3bd714144992f92e310a78d67e0e6048 (diff) |
change test parse to validation function
-rw-r--r-- | src/app.rs | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -1,6 +1,5 @@ use clap::{App, Arg}; - pub fn build() -> App<'static, 'static> { App::new("lsd") .version(crate_version!()) @@ -208,7 +207,7 @@ pub fn build() -> App<'static, 'static> { fn validate_date_argument(arg: String) -> Result<(), String> { use std::error::Error; if arg.starts_with("+") { - time::now().strftime(&arg).map(drop).map_err(|err| err.description().to_string()) + validate_time_format(&arg).map_err(|err| err.description().to_string()) } else if &arg == "date" { Result::Ok(()) } else if &arg == "relative" { @@ -217,3 +216,26 @@ fn validate_date_argument(arg: String) -> Result<(), String> { Result::Err("possible values: date, relative".to_owned()) } } + +fn validate_time_format(formatter: &str) -> Result<(), time::ParseError> { + let mut chars = formatter.chars(); + loop { + match chars.next() { + Some('%') => match chars.next() { + Some('A') | Some('a') | Some('B') | Some('b') | Some('C') | Some('c') + | Some('D') | Some('d') | Some('e') | Some('F') | Some('f') | Some('G') + | Some('g') | Some('H') | Some('h') | Some('I') | Some('j') | Some('k') + | Some('l') | Some('M') | Some('m') | Some('n') | Some('P') | Some('p') + | Some('R') | Some('r') | Some('S') | Some('s') | Some('T') | Some('t') + | Some('U') | Some('u') | Some('V') | Some('v') | Some('W') | Some('w') + | Some('X') | Some('x') | Some('Y') | Some('y') | Some('Z') | Some('z') + | Some('+') | Some('%') => (), + Some(c) => return Err(time::ParseError::InvalidFormatSpecifier(c)), + None => return Err(time::ParseError::MissingFormatConverter), + }, + None => break, + _ => continue, + } + } + Ok(()) +} |