#[cfg(feature = "decimal-precision")]
pub use rust_decimal::Decimal as Number;
#[cfg(feature = "f64-floats")]
pub type Number = f64;
#[cfg(all(feature = "f64-floats", feature = "decimal-precision"))]
compile_error!("Cannot enable both 'f64-floats' and 'decimal-precision' features");
#[cfg(not(any(feature = "f64-floats", feature = "decimal-precision")))]
compile_error!("Must enable either 'f64-floats' or 'decimal-precision' feature");
pub(crate) mod consts {
use super::Number;
#[cfg(feature = "decimal-precision")]
pub use rust_decimal::Decimal;
#[cfg(feature = "decimal-precision")]
pub const ZERO: Number = Decimal::ZERO;
#[cfg(feature = "f64-floats")]
pub const ZERO: Number = 0.0;
#[cfg(feature = "decimal-precision")]
pub const ONE: Number = Decimal::ONE;
#[cfg(feature = "f64-floats")]
pub const ONE: Number = 1.0;
}
pub trait ParseNumber: Sized {
fn parse_number(s: &str) -> Result<Self, String>;
}
#[cfg(feature = "decimal-precision")]
impl ParseNumber for Number {
fn parse_number(s: &str) -> Result<Self, String> {
use rust_decimal::prelude::FromStr;
Number::from_str(s).map_err(|e| e.to_string())
}
}
#[cfg(feature = "f64-floats")]
impl ParseNumber for Number {
fn parse_number(s: &str) -> Result<Self, String> {
s.parse::<f64>().map_err(|e| e.to_string())
}
}
#[macro_export]
#[cfg(feature = "decimal-precision")]
macro_rules! num {
($val:expr) => {
rust_decimal_macros::dec!($val)
};
}
#[macro_export]
#[cfg(feature = "f64-floats")]
macro_rules! num {
($val:expr) => {
$val as f64
};
}