use core::convert::TryFrom;
use core::fmt;
use core::str::FromStr;
use crate::LimitError;
use crate::ParseError;
use crate::SignedFixedDecimal;
#[derive(Debug, Clone, PartialEq, Default)]
pub struct FixedInteger(SignedFixedDecimal);
impl From<FixedInteger> for SignedFixedDecimal {
fn from(value: FixedInteger) -> Self {
value.0
}
}
macro_rules! impl_fixed_integer_from_integer_type {
($type:ident) => {
impl From<$type> for FixedInteger {
fn from(value: $type) -> Self {
FixedInteger(SignedFixedDecimal::from(value))
}
}
};
}
impl_fixed_integer_from_integer_type!(isize);
impl_fixed_integer_from_integer_type!(i128);
impl_fixed_integer_from_integer_type!(i64);
impl_fixed_integer_from_integer_type!(i32);
impl_fixed_integer_from_integer_type!(i16);
impl_fixed_integer_from_integer_type!(i8);
impl_fixed_integer_from_integer_type!(usize);
impl_fixed_integer_from_integer_type!(u128);
impl_fixed_integer_from_integer_type!(u64);
impl_fixed_integer_from_integer_type!(u32);
impl_fixed_integer_from_integer_type!(u16);
impl_fixed_integer_from_integer_type!(u8);
impl writeable::Writeable for FixedInteger {
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
self.0.write_to(sink)
}
}
impl TryFrom<SignedFixedDecimal> for FixedInteger {
type Error = LimitError;
fn try_from(signed_fd: SignedFixedDecimal) -> Result<Self, Self::Error> {
if signed_fd.absolute.magnitude_range().start() != &0 {
Err(LimitError)
} else {
Ok(FixedInteger(signed_fd))
}
}
}
impl FixedInteger {
#[inline]
pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
Self::try_from_utf8(s.as_bytes())
}
pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
FixedInteger::try_from(SignedFixedDecimal::try_from_utf8(code_units)?)
.map_err(|LimitError| ParseError::Limit)
}
}
impl FromStr for FixedInteger {
type Err = ParseError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from_str(s)
}
}