From c92c91b342393594e72e02fb6b060ba8bcd57c4c Mon Sep 17 00:00:00 2001 From: Riccardo Attilio Galli Date: Sat, 21 May 2022 00:08:30 -0700 Subject: [PATCH] Ensure that a single bound cannot go backward --- src/bin/tuc.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/bin/tuc.rs b/src/bin/tuc.rs index fa13e4f..6e07c14 100644 --- a/src/bin/tuc.rs +++ b/src/bin/tuc.rs @@ -287,6 +287,9 @@ impl FromStr for UserBounds { (_, Side::Some(0)) => { return Err("Field value 0 is not allowed (fields are 1-indexed)".into()); } + (Side::Some(left), Side::Some(right)) if right < left => { + return Err("Field left value cannot be greater than right value".into()); + } _ => (), } @@ -815,6 +818,62 @@ mod tests { ); } + #[test] + fn test_user_bounds_from_str() { + assert_eq!( + UserBounds::from_str("1").ok(), + Some(UserBounds::new(Side::Some(1), Side::Some(1))), + ); + assert_eq!( + UserBounds::from_str("-1").ok(), + Some(UserBounds::new(Side::Some(-1), Side::Some(-1))), + ); + assert_eq!( + UserBounds::from_str("1:2").ok(), + Some(UserBounds::new(Side::Some(1), Side::Some(2))), + ); + assert_eq!( + UserBounds::from_str("-2:-1").ok(), + Some(UserBounds::new(Side::Some(-2), Side::Some(-1))), + ); + assert_eq!( + UserBounds::from_str("1:").ok(), + Some(UserBounds::new(Side::Some(1), Side::Continue)), + ); + assert_eq!( + UserBounds::from_str("-1:").ok(), + Some(UserBounds::new(Side::Some(-1), Side::Continue)), + ); + assert_eq!( + UserBounds::from_str(":1").ok(), + Some(UserBounds::new(Side::Continue, Side::Some(1))), + ); + assert_eq!( + UserBounds::from_str(":-1").ok(), + Some(UserBounds::new(Side::Continue, Side::Some(-1))), + ); + + { + #![allow(clippy::bind_instead_of_map)] + assert_eq!( + UserBounds::from_str("2:1") + .err() + .and_then(|x| Some(x.to_string())), + Some(String::from( + "Field left value cannot be greater than right value" + )) + ); + assert_eq!( + UserBounds::from_str("-1:-2") + .err() + .and_then(|x| Some(x.to_string())), + Some(String::from( + "Field left value cannot be greater than right value" + )) + ); + } + } + #[test] fn test_user_bounds_is_sortable() { assert!(UserBoundsList(Vec::new()).is_sortable());