From f65b39d7b57c7b72d4b18fd22737688adbbfb2f9 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 18 Feb 2022 14:01:42 -0500 Subject: [PATCH 1/2] Add macros for construct from literals --- dec/src/lib.rs | 1 + dec/src/macros.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++ dec/tests/dec.rs | 11 ++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 dec/src/macros.rs diff --git a/dec/src/lib.rs b/dec/src/lib.rs index 7cfc63b..165821e 100644 --- a/dec/src/lib.rs +++ b/dec/src/lib.rs @@ -102,6 +102,7 @@ mod decimal128; mod decimal32; mod decimal64; mod error; +mod macros; mod ordered; #[cfg(tests)] mod tests; diff --git a/dec/src/macros.rs b/dec/src/macros.rs new file mode 100644 index 0000000..4d4ba7d --- /dev/null +++ b/dec/src/macros.rs @@ -0,0 +1,63 @@ +#[macro_export] +/// A macro to construct a [`Decimal32`] from a literal. +/// Converts the input tokens to a string, and then parses the string into a [`Decimal32`]. +/// Panics if the provided input is not a valid [`Decimal32`] literal. +/// +/// [`Decimal32`]: crate::Decimal32 +/// +/// # Examples: +/// ``` +/// use dec::d32; +/// +/// assert!(d32!(1.753).to_string() == "1.753"); +/// ``` +macro_rules! d32 { + ($l:expr) => { + <$crate::Decimal32 as ::std::str::FromStr>::from_str(stringify!($l)) + .unwrap_or_else(|e| panic!("{}", e.to_string())) + }; +} + +#[macro_export] +/// A macro to construct a [`Decimal64`] from a literal. +/// Converts the input tokens to a string, and then parses the string into a [`Decimal64`]. +/// Panics if the provided input is not a valid [`Decimal64`] literal. +/// +/// [`Decimal64`]: crate::Decimal64 +/// +/// # Examples: +/// ``` +/// use dec::d64; +/// +/// assert!(d64!(NaN).is_nan()); +/// assert!(d64!(0).is_zero()); +/// assert!(d64!(-0.1).is_negative()); +/// ``` +macro_rules! d64 { + ($l:expr) => { + <$crate::Decimal64 as ::std::str::FromStr>::from_str(stringify!($l)) + .unwrap_or_else(|e| panic!("{}", e.to_string())) + }; +} + +#[macro_export] +///A macro to construct a [`Decimal128`] from a literal. +/// Converts the input tokens to a string, and then parses the string into a [`Decimal128`]. +/// Panics if the provided input is not a valid [`Decimal128`] literal. +/// +/// [`Decimal128`]: crate::Decimal128 +/// +/// # Examples: +/// ``` +/// use dec::d128; +/// +/// assert!(d128!(NaN).is_nan()); +/// assert!(d128!(0).is_zero()); +/// assert!(d128!(-0.1).is_negative()); +/// ``` +macro_rules! d128 { + ($l:expr) => { + <$crate::Decimal128 as ::std::str::FromStr>::from_str(stringify!($l)) + .unwrap_or_else(|e| panic!("{}", e.to_string())) + }; +} diff --git a/dec/tests/dec.rs b/dec/tests/dec.rs index e5e9650..572e871 100644 --- a/dec/tests/dec.rs +++ b/dec/tests/dec.rs @@ -23,7 +23,9 @@ use std::ops::{ Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign, }; -use dec::{Context, Decimal, Decimal128, Decimal32, Decimal64, OrderedDecimal, Status}; +use dec::{ + d128, d32, d64, Context, Decimal, Decimal128, Decimal32, Decimal64, OrderedDecimal, Status, +}; #[derive(Default)] struct ValidatingHasher { @@ -2057,3 +2059,10 @@ fn decnum_round_reduce_to_place() { inner("599.18", &["6E+2", "6E+2", "599", "599.2", "599.18"]); inner("1009", &["1E+3", "1E+3", "1.01E+3", "1009"]); } + +#[test] +fn literal_macros() { + assert_eq!(d32!(1.34).to_string(), "1.34"); + assert!(d64!(NaN).is_nan()); + assert!(d128!(-0).is_zero()); +} From 8bc6d767733d478a4edb925daf672cd6864c5e0b Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sat, 19 Feb 2022 19:00:58 -0500 Subject: [PATCH 2/2] Add macro for `Decimal` --- dec/src/macros.rs | 28 +++++++++++++++++++++++++--- dec/tests/dec.rs | 3 ++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/dec/src/macros.rs b/dec/src/macros.rs index 4d4ba7d..d5ebdbd 100644 --- a/dec/src/macros.rs +++ b/dec/src/macros.rs @@ -1,4 +1,3 @@ -#[macro_export] /// A macro to construct a [`Decimal32`] from a literal. /// Converts the input tokens to a string, and then parses the string into a [`Decimal32`]. /// Panics if the provided input is not a valid [`Decimal32`] literal. @@ -11,6 +10,7 @@ /// /// assert!(d32!(1.753).to_string() == "1.753"); /// ``` +#[macro_export] macro_rules! d32 { ($l:expr) => { <$crate::Decimal32 as ::std::str::FromStr>::from_str(stringify!($l)) @@ -18,7 +18,6 @@ macro_rules! d32 { }; } -#[macro_export] /// A macro to construct a [`Decimal64`] from a literal. /// Converts the input tokens to a string, and then parses the string into a [`Decimal64`]. /// Panics if the provided input is not a valid [`Decimal64`] literal. @@ -33,6 +32,7 @@ macro_rules! d32 { /// assert!(d64!(0).is_zero()); /// assert!(d64!(-0.1).is_negative()); /// ``` +#[macro_export] macro_rules! d64 { ($l:expr) => { <$crate::Decimal64 as ::std::str::FromStr>::from_str(stringify!($l)) @@ -40,7 +40,6 @@ macro_rules! d64 { }; } -#[macro_export] ///A macro to construct a [`Decimal128`] from a literal. /// Converts the input tokens to a string, and then parses the string into a [`Decimal128`]. /// Panics if the provided input is not a valid [`Decimal128`] literal. @@ -55,9 +54,32 @@ macro_rules! d64 { /// assert!(d128!(0).is_zero()); /// assert!(d128!(-0.1).is_negative()); /// ``` +#[macro_export] macro_rules! d128 { ($l:expr) => { <$crate::Decimal128 as ::std::str::FromStr>::from_str(stringify!($l)) .unwrap_or_else(|e| panic!("{}", e.to_string())) }; } + +///A macro to construct a [`Decimal128`] from a literal. +/// Converts the input tokens to a string, and then parses the string into a [`Decimal128`]. +/// Panics if the provided input is not a valid [`Decimal128`] literal. +/// +/// [`Decimal128`]: crate::Decimal128 +/// +/// # Examples: +/// ``` +/// use dec::dec; +/// +/// assert!(dec!(12, NaN).is_nan()); +/// assert!(dec!(13, 0).is_zero()); +/// assert!(dec!(21, -0.1).is_negative()); +/// ``` +#[macro_export] +macro_rules! dec { + ($n:expr, $l:expr) => { + <$crate::Decimal<$n> as ::std::str::FromStr>::from_str(stringify!($l)) + .unwrap_or_else(|e| panic!("{}", e.to_string())) + }; +} diff --git a/dec/tests/dec.rs b/dec/tests/dec.rs index 572e871..4631dee 100644 --- a/dec/tests/dec.rs +++ b/dec/tests/dec.rs @@ -24,7 +24,7 @@ use std::ops::{ }; use dec::{ - d128, d32, d64, Context, Decimal, Decimal128, Decimal32, Decimal64, OrderedDecimal, Status, + d128, d32, d64, dec, Context, Decimal, Decimal128, Decimal32, Decimal64, OrderedDecimal, Status, }; #[derive(Default)] @@ -2065,4 +2065,5 @@ fn literal_macros() { assert_eq!(d32!(1.34).to_string(), "1.34"); assert!(d64!(NaN).is_nan()); assert!(d128!(-0).is_zero()); + assert!(dec!(13, 0).is_zero()); }