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..d5ebdbd --- /dev/null +++ b/dec/src/macros.rs @@ -0,0 +1,85 @@ +/// 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_export] +macro_rules! d32 { + ($l:expr) => { + <$crate::Decimal32 as ::std::str::FromStr>::from_str(stringify!($l)) + .unwrap_or_else(|e| panic!("{}", e.to_string())) + }; +} + +/// 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_export] +macro_rules! d64 { + ($l:expr) => { + <$crate::Decimal64 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::d128; +/// +/// assert!(d128!(NaN).is_nan()); +/// 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 e5e9650..4631dee 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, dec, Context, Decimal, Decimal128, Decimal32, Decimal64, OrderedDecimal, Status, +}; #[derive(Default)] struct ValidatingHasher { @@ -2057,3 +2059,11 @@ 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()); + assert!(dec!(13, 0).is_zero()); +}