use std::ffi::{CStr, CString};
use std::fmt;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::str::FromStr;
use libc::c_char;
use crate::context::Context;
#[cfg(feature = "arbitrary-precision")]
use crate::decimal::Decimal;
use crate::decimal64::Decimal64;
use crate::error::ParseDecimalError;
#[derive(Clone, Copy)]
pub struct Decimal32 {
pub(crate) inner: decnumber_sys::decSingle,
}
impl Decimal32 {
pub const NAN: Decimal32 = Decimal32::from_ne_bytes(if cfg!(target_endian = "little") {
[0x0, 0x0, 0x0, 0x7c]
} else {
[0x7c, 0x0, 0x0, 0x0]
});
pub const ZERO: Decimal32 = Decimal32::from_ne_bytes(if cfg!(target_endian = "little") {
[0x0, 0x0, 0x50, 0x22]
} else {
[0x22, 0x50, 0x0, 0x0]
});
pub const ONE: Decimal32 = Decimal32::from_ne_bytes(if cfg!(target_endian = "little") {
[0x1, 0x0, 0x50, 0x22]
} else {
[0x22, 0x50, 0x0, 0x1]
});
pub fn from_le_bytes(mut bytes: [u8; 4]) -> Decimal32 {
if cfg!(target_endian = "big") {
bytes.reverse();
}
Decimal32::from_ne_bytes(bytes)
}
pub fn from_be_bytes(mut bytes: [u8; 4]) -> Decimal32 {
if cfg!(target_endian = "little") {
bytes.reverse();
}
Decimal32::from_ne_bytes(bytes)
}
pub const fn from_ne_bytes(bytes: [u8; 4]) -> Decimal32 {
Decimal32 {
inner: decnumber_sys::decSingle { bytes },
}
}
pub fn to_le_bytes(&self) -> [u8; 4] {
let mut bytes = self.to_ne_bytes();
if cfg!(target_endian = "big") {
bytes.reverse();
}
bytes
}
pub fn to_be_bytes(&self) -> [u8; 4] {
let mut bytes = self.to_ne_bytes();
if cfg!(target_endian = "little") {
bytes.reverse();
}
bytes
}
pub fn to_ne_bytes(&self) -> [u8; 4] {
self.inner.bytes
}
pub fn exponent(&self) -> i32 {
unsafe { decnumber_sys::decSingleGetExponent(&self.inner) }
}
}
impl Default for Decimal32 {
fn default() -> Decimal32 {
Decimal32::ZERO
}
}
impl fmt::Debug for Decimal32 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl fmt::Display for Decimal32 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut buf = MaybeUninit::<[c_char; decnumber_sys::DECDOUBLE_String]>::uninit();
let c_str = unsafe {
if f.alternate() {
decnumber_sys::decSingleToEngString(&self.inner, buf.as_mut_ptr() as *mut c_char);
} else {
decnumber_sys::decSingleToString(&self.inner, buf.as_mut_ptr() as *mut c_char);
}
CStr::from_ptr(buf.as_ptr() as *const c_char)
};
f.write_str(
c_str
.to_str()
.expect("decSingleToString yields valid UTF-8"),
)
}
}
impl FromStr for Decimal32 {
type Err = ParseDecimalError;
fn from_str(s: &str) -> Result<Decimal32, ParseDecimalError> {
Context::<Decimal32>::default().parse(s)
}
}
impl Default for Context<Decimal32> {
fn default() -> Context<Decimal32> {
let mut ctx = MaybeUninit::<decnumber_sys::decContext>::uninit();
let ctx = unsafe {
decnumber_sys::decContextDefault(ctx.as_mut_ptr(), decnumber_sys::DEC_INIT_DECDOUBLE);
ctx.assume_init()
};
Context {
inner: ctx,
_phantom: PhantomData,
}
}
}
impl Context<Decimal32> {
pub fn parse<S>(&mut self, s: S) -> Result<Decimal32, ParseDecimalError>
where
S: Into<Vec<u8>>,
{
let c_string = CString::new(s).map_err(|_| ParseDecimalError)?;
let mut d = MaybeUninit::<decnumber_sys::decSingle>::uninit();
let d = unsafe {
decnumber_sys::decSingleFromString(d.as_mut_ptr(), c_string.as_ptr(), &mut self.inner);
d.assume_init()
};
if (self.inner.status & decnumber_sys::DEC_Conversion_syntax) != 0 {
Err(ParseDecimalError)
} else {
Ok(Decimal32 { inner: d })
}
}
pub fn from_decimal64(&mut self, d64: Decimal64) -> Decimal32 {
let mut d32 = MaybeUninit::<decnumber_sys::decSingle>::uninit();
let d32 = unsafe {
decnumber_sys::decSingleFromWider(d32.as_mut_ptr(), &d64.inner, &mut self.inner);
d32.assume_init()
};
Decimal32 { inner: d32 }
}
#[cfg(feature = "arbitrary-precision")]
pub fn from_decimal<const N: usize>(&mut self, d: &Decimal<N>) -> Decimal32 {
let mut d32 = MaybeUninit::<decnumber_sys::decSingle>::uninit();
let d32 = unsafe {
decnumber_sys::decimal32FromNumber(d32.as_mut_ptr(), d.as_ptr(), &mut self.inner);
d32.assume_init()
};
Decimal32 { inner: d32 }
}
}