diff options
-rw-r--r-- | src/args.rs | 68 | ||||
-rw-r--r-- | tests/arguments.rs | 23 |
2 files changed, 80 insertions, 11 deletions
diff --git a/src/args.rs b/src/args.rs index 2a9bc28..8647ea7 100644 --- a/src/args.rs +++ b/src/args.rs @@ -177,6 +177,40 @@ pub struct IntegerArgumentType<T, R: RangeBounds<T>> { pub _ty: PhantomData<T>, } +/// Helper to create an integer argument with values not bounded by a range. +/// +/// # Examples +/// +/// ```rust +/// use ::iosonism::args::integer; +/// +/// let argtype = integer::<i32>(); +/// ``` +pub fn integer<T>() -> IntegerArgumentType<T, ::std::ops::RangeFull> { + IntegerArgumentType { + range: .., + _ty: PhantomData, + } +} + +/// Helper to create an integer argument with values bounded by a range. +/// +/// # Examples +/// +/// ```rust +/// use ::iosonism::args::bounded_integer; +/// +/// let argtype = bounded_integer(0..100i32); +/// ``` +pub fn bounded_integer<T, R: RangeBounds<T>>( + range: R, +) -> IntegerArgumentType<T, R> { + IntegerArgumentType { + range: range, + _ty: PhantomData, + } +} + /// An `ArgumentType` for integer types. impl<S, E, T, R> ArgumentType<S, E> for IntegerArgumentType<T, R> where @@ -218,6 +252,40 @@ pub struct FloatArgumentType<T, R: RangeBounds<T>> { pub _ty: PhantomData<T>, } +/// Helper to create a float argument with values not bounded by a range. +/// +/// # Examples +/// +/// ```rust +/// use ::iosonism::args::float; +/// +/// let argtype = float::<f32>(); +/// ``` +pub fn float<T>() -> FloatArgumentType<T, ::std::ops::RangeFull> { + FloatArgumentType { + range: .., + _ty: PhantomData, + } +} + +/// Helper to create a float argument with values bounded by a range. +/// +/// # Examples +/// +/// ```rust +/// use ::iosonism::args::bounded_float; +/// +/// let argtype = bounded_float(0.0..100f32); +/// ``` +pub fn bounded_float<T, R: RangeBounds<T>>( + range: R, +) -> FloatArgumentType<T, R> { + FloatArgumentType { + range: range, + _ty: PhantomData, + } +} + /// An `ArgumentType` for float types. impl<S, E, T, R> ArgumentType<S, E> for FloatArgumentType<T, R> where diff --git a/tests/arguments.rs b/tests/arguments.rs index 4b5a2a0..55b4a4b 100644 --- a/tests/arguments.rs +++ b/tests/arguments.rs @@ -7,12 +7,13 @@ #![allow(non_snake_case)] use ::std::io::Cursor; -use ::std::marker::PhantomData; use ::iosonism::args::ArgumentType; use ::iosonism::args::BoolArgumentType; -use ::iosonism::args::FloatArgumentType; -use ::iosonism::args::IntegerArgumentType; +use ::iosonism::args::bounded_float; +use ::iosonism::args::bounded_integer; +use ::iosonism::args::float; +use ::iosonism::args::integer; use ::iosonism::strcursor::StringReader; mod common; @@ -45,7 +46,7 @@ fn test_i32__parse() { let mut reader = Cursor::new("15"); assert_eq!( ArgumentType::<(), ErrorPanic>::parse( - &IntegerArgumentType { range: .., _ty: PhantomData::<i32> }, + &integer::<i32>(), &mut reader, ), Ok(15), @@ -57,7 +58,7 @@ fn test_i32__parse() { fn test_i32__parse__range() { let mut reader = Cursor::new("-5"); assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse( - &IntegerArgumentType { range: 0..=100, _ty: PhantomData::<i32> }, + &bounded_integer(0..100i32), &mut reader, ).is_err()); struct ErrFn; @@ -74,7 +75,7 @@ fn test_i64__parse() { let mut reader = Cursor::new("15"); assert_eq!( ArgumentType::<(), ErrorPanic>::parse( - &IntegerArgumentType { range: .., _ty: PhantomData::<i64> }, + &integer::<i64>(), &mut reader, ), Ok(15), @@ -86,7 +87,7 @@ fn test_i64__parse() { fn test_i64__parse__range() { let mut reader = Cursor::new("-5"); assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse( - &IntegerArgumentType { range: 0..=100, _ty: PhantomData::<i64> }, + &bounded_integer(0..100i64), &mut reader, ).is_err()); struct ErrFn; @@ -103,7 +104,7 @@ fn test_f32__parse() { let mut reader = Cursor::new("15"); assert_eq!( ArgumentType::<(), ErrorPanic>::parse( - &FloatArgumentType { range: .., _ty: PhantomData::<f32> }, + &float::<f32>(), &mut reader, ), Ok(15.0), @@ -115,7 +116,7 @@ fn test_f32__parse() { fn test_f32__parse__range() { let mut reader = Cursor::new("-5"); assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse( - &FloatArgumentType { range: 0.0..=100.0, _ty: PhantomData::<f32> }, + &bounded_float(0.0..100f32), &mut reader, ).is_err()); struct ErrFn; @@ -132,7 +133,7 @@ fn test_f64__parse() { let mut reader = Cursor::new("15"); assert_eq!( ArgumentType::<(), ErrorPanic>::parse( - &FloatArgumentType { range: .., _ty: PhantomData::<f64> }, + &float::<f64>(), &mut reader, ), Ok(15.0), @@ -144,7 +145,7 @@ fn test_f64__parse() { fn test_f64__parse__range() { let mut reader = Cursor::new("-5"); assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse( - &FloatArgumentType { range: 0.0..=100.0, _ty: PhantomData::<f64> }, + &bounded_float(0.0..100f64), &mut reader, ).is_err()); struct ErrFn; |