Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mod decimal128;
mod decimal32;
mod decimal64;
mod error;
mod macros;
mod ordered;
#[cfg(tests)]
mod tests;
Expand Down
85 changes: 85 additions & 0 deletions dec/src/macros.rs
Original file line number Diff line number Diff line change
@@ -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()))
};
}
12 changes: 11 additions & 1 deletion dec/tests/dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}