#![no_std]
#![doc = include_str!("../README.md")]
mod const_scale_fpdec;
mod fpdec_inner;
mod inner_i128;
mod inner_shorts;
mod none_scale_common;
mod oob_scale_fpdec;
pub use crate::const_scale_fpdec::ConstScaleFpdec;
pub use crate::fpdec_inner::FpdecInner;
pub use crate::oob_scale_fpdec::{OobFmt, OobScaleFpdec};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParseError {
Empty,
Invalid,
Overflow,
Precision,
}
use core::num::{IntErrorKind, ParseIntError};
impl From<ParseIntError> for ParseError {
fn from(pie: ParseIntError) -> Self {
match pie.kind() {
IntErrorKind::Empty => ParseError::Empty,
IntErrorKind::InvalidDigit => ParseError::Invalid,
_ => ParseError::Overflow,
}
}
}
use core::fmt;
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
ParseError::Empty => "empty string",
ParseError::Invalid => "invalid digit in the string",
ParseError::Overflow => "overflow",
ParseError::Precision => "precision out of range",
};
write!(f, "{s}")
}
}
impl core::error::Error for ParseError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Rounding {
#[default]
Round,
Floor,
Ceiling,
TowardsZero,
AwayFromZero,
}
#[macro_export]
macro_rules! fpdec {
($n:expr) => {
primitive_fixed_point_decimal::ConstScaleFpdec::try_from($n).unwrap()
};
($n:expr, $scale:expr) => {
primitive_fixed_point_decimal::OobScaleFpdec::try_from(($n, $scale)).unwrap()
};
}
pub trait IntoRatioInt<T> {
fn to_int(self) -> T;
}
impl<I, T> IntoRatioInt<T> for I
where
I: Into<T> + num_traits::PrimInt,
{
fn to_int(self) -> T {
self.into()
}
}