From 8037d5f4ce97b04c910bfa8b162e589dd20a3aed Mon Sep 17 00:00:00 2001 From: Arkadiusz Bielewicz Date: Wed, 13 Oct 2021 13:13:32 +0200 Subject: [PATCH] #532 Improve date format validation to include all supported cases | Added missing valid cases --- src/app.rs | 37 +++++++++++++++++++++++++------------ tests/integration.rs | 16 ++++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 90cd699..9850b69 100644 --- a/src/app.rs +++ b/src/app.rs @@ -246,8 +246,8 @@ pub fn build() -> App<'static, 'static> { ) .arg( Arg::with_name("classic") - .long("classic") - .help("Enable classic mode (display output similar to ls)"), + .long("classic") + .help("Enable classic mode (display output similar to ls)"), ) .arg( Arg::with_name("no-symlink") @@ -296,6 +296,29 @@ pub fn validate_time_format(formatter: &str) -> Result<(), String> { loop { match chars.next() { Some('%') => match chars.next() { + Some('.') => match chars.next() { + Some('f') => (), + Some(n @ '3') | Some(n @ '6') | Some(n @ '9') => match chars.next() { + Some('f') => (), + Some(c) => return Err(format!("invalid format specifier: %.{}{}", n, c)), + None => return Err("missing format specifier".to_owned()), + }, + Some(c) => return Err(format!("invalid format specifier: %.{}", c)), + None => return Err("missing format specifier".to_owned()), + }, + Some(n @ ':') | Some(n @ '#') => match chars.next() { + Some('z') => (), + Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)), + None => return Err("missing format specifier".to_owned()), + }, + Some(n @ '-') | Some(n @ '_') | Some(n @ '0') => match chars.next() { + Some('C') | Some('d') | Some('e') | Some('f') | Some('G') | Some('g') + | Some('H') | Some('I') | Some('j') | Some('k') | Some('l') | Some('M') + | Some('m') | Some('S') | Some('s') | Some('U') | Some('u') | Some('V') + | Some('W') | Some('w') | Some('Y') | Some('y') => (), + Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)), + None => return Err("missing format specifier".to_owned()), + } 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') @@ -309,16 +332,6 @@ pub fn validate_time_format(formatter: &str) -> Result<(), String> { Some(c) => return Err(format!("invalid format specifier: %{}{}", n, c)), None => return Err("missing format specifier".to_owned()), }, - Some('.') => match chars.next() { - Some('f') => (), - Some(n @ '3') | Some(n @ '6') | Some(n @ '9') => match chars.next() { - Some('f') => (), - Some(c) => return Err(format!("invalid format specifier: %.{}{}", n, c)), - None => return Err("missing format specifier".to_owned()), - }, - Some(c) => return Err(format!("invalid format specifier: %.{}", c)), - None => return Err("missing format specifier".to_owned()), - }, Some(c) => return Err(format!("invalid format specifier: %{}", c)), None => return Err("missing format specifier".to_owned()), }, diff --git a/tests/integration.rs b/tests/integration.rs index 8eddc78..b3f8073 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -608,3 +608,19 @@ fn test_date_custom_format_supports_nanos_with_length() { .assert() .stdout(predicate::str::is_match("testDateFormat\\.[0-9]{3}").unwrap().count(2)); } + +#[test] +fn test_date_custom_format_supports_padding() { + let dir = tempdir(); + dir.child("one").touch().unwrap(); + dir.child("two").touch().unwrap(); + + cmd() + .arg("-l") + .arg("--date") + .arg("+testDateFormat%_d") + .arg("--ignore-config") + .arg(dir.path()) + .assert() + .stdout(predicate::str::is_match("testDateFormat[\\s0-9]{2}").unwrap().count(2)); +}