From 3dc2648f09ab2c27e22df1e0857bfea918f0c466 Mon Sep 17 00:00:00 2001 From: ClementTsang <34804052+ClementTsang@users.noreply.github.com> Date: Sun, 19 Nov 2023 01:48:51 -0500 Subject: [PATCH 1/2] other: add additional clamping functions on numeric types --- src/utils/gen_util.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/utils/gen_util.rs b/src/utils/gen_util.rs index ae35da8d2..42e24612b 100644 --- a/src/utils/gen_util.rs +++ b/src/utils/gen_util.rs @@ -186,6 +186,41 @@ macro_rules! multi_eq_ignore_ascii_case { }; } +/// A trait for additional clamping functions on numeric types. +pub trait ClampExt { + /// Restrict a value by a lower bound. + fn clamp_lower(&self, lower_bound: Self) -> Self; + + /// Restrict a value by an upper bound. + fn clamp_upper(&self, upper_bound: Self) -> Self; +} + +macro_rules! clamp_num_impl { + ( $($NumType:ty),+ $(,)? ) => { + $( + impl ClampExt for $NumType { + fn clamp_lower(&self, lower_bound: Self) -> Self { + if *self < lower_bound { + lower_bound + } else { + *self + } + } + + fn clamp_upper(&self, upper_bound: Self) -> Self { + if *self > upper_bound { + upper_bound + } else { + *self + } + } + } + )* + }; +} + +clamp_num_impl!(u8, u16, u32, u64, usize); + #[cfg(test)] mod test { use super::*; From f2c4520ec2834eed02a990d982cad68a4447e244 Mon Sep 17 00:00:00 2001 From: ClementTsang <34804052+ClementTsang@users.noreply.github.com> Date: Sun, 19 Nov 2023 02:46:21 -0500 Subject: [PATCH 2/2] add tests and replace one usage --- src/components/data_table.rs | 4 +++- src/utils/gen_util.rs | 42 +++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/components/data_table.rs b/src/components/data_table.rs index 5fb5cb2c1..ed79961a6 100644 --- a/src/components/data_table.rs +++ b/src/components/data_table.rs @@ -21,6 +21,8 @@ pub use data_type::*; pub mod sortable; pub use sortable::*; +use crate::utils::gen_util::ClampExt; + /// A [`DataTable`] is a component that displays data in a tabular form. /// /// Note that [`DataTable`] takes a generic type `S`, bounded by [`SortType`]. This controls whether this table @@ -120,7 +122,7 @@ impl, H: ColumnHeader, S: SortType, C: DataTableColumn new_index { diff --git a/src/utils/gen_util.rs b/src/utils/gen_util.rs index 42e24612b..cc72c21af 100644 --- a/src/utils/gen_util.rs +++ b/src/utils/gen_util.rs @@ -188,10 +188,12 @@ macro_rules! multi_eq_ignore_ascii_case { /// A trait for additional clamping functions on numeric types. pub trait ClampExt { - /// Restrict a value by a lower bound. + /// Restrict a value by a lower bound. If the current value is _lower_ than `lower_bound`, + /// it will be set to `_lower_bound`. fn clamp_lower(&self, lower_bound: Self) -> Self; - /// Restrict a value by an upper bound. + /// Restrict a value by an upper bound. If the current value is _greater_ than `upper_bound`, + /// it will be set to `upper_bound`. fn clamp_upper(&self, upper_bound: Self) -> Self; } @@ -244,7 +246,7 @@ mod test { } #[test] - fn test_truncate() { + fn test_truncate_str() { let cpu_header = "CPU(c)▲"; assert_eq!( @@ -267,7 +269,7 @@ mod test { } #[test] - fn test_truncate_cjk() { + fn truncate_cjk() { let cjk = "施氏食獅史"; assert_eq!( @@ -290,7 +292,7 @@ mod test { } #[test] - fn test_truncate_mixed() { + fn truncate_mixed() { let test = "Test (施氏食獅史) Test"; assert_eq!( @@ -323,7 +325,7 @@ mod test { } #[test] - fn test_truncate_flags() { + fn truncate_flags() { let flag = "🇨🇦"; assert_eq!(truncate_str(flag, 3_usize), flag); assert_eq!(truncate_str(flag, 2_usize), flag); @@ -366,7 +368,7 @@ mod test { /// This might not be the best way to handle it, but this at least tests that it doesn't crash... #[test] - fn test_truncate_hindi() { + fn truncate_hindi() { // cSpell:disable let test = "हिन्दी"; assert_eq!(truncate_str(test, 10_usize), test); @@ -381,7 +383,7 @@ mod test { } #[test] - fn test_truncate_emoji() { + fn truncate_emoji() { let heart = "❤️"; assert_eq!(truncate_str(heart, 2_usize), heart); assert_eq!(truncate_str(heart, 1_usize), heart); @@ -431,4 +433,28 @@ mod test { "multi non-matching should fail" ); } + + #[test] + fn test_clamp_upper() { + let val: usize = 100; + assert_eq!(val.clamp_upper(150), 100); + + let val: usize = 100; + assert_eq!(val.clamp_upper(100), 100); + + let val: usize = 100; + assert_eq!(val.clamp_upper(50), 50); + } + + #[test] + fn test_clamp_lower() { + let val: usize = 100; + assert_eq!(val.clamp_lower(150), 150); + + let val: usize = 100; + assert_eq!(val.clamp_lower(100), 100); + + let val: usize = 100; + assert_eq!(val.clamp_lower(50), 100); + } }