use facet::Facet;
use rust_decimal::Decimal;
use xur_runtime::{Registry, XurStr};
#[derive(Clone, Copy, Facet)]
#[facet(opaque)]
pub struct XurDecimal(pub Decimal);
#[derive(Clone, Copy, Facet)]
#[repr(C)]
pub enum Error {
OutOfRange,
Parse,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Error::OutOfRange => "value out of range for the target type",
Error::Parse => "invalid decimal string",
})
}
}
impl std::fmt::Display for XurDecimal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl XurDecimal {
pub fn to_dotnet_regs(&self) -> [u64; 2] {
let bytes = self.0.serialize();
let flags = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
let lo = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
let mid = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]);
let hi = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]);
let reg0 = (flags as u64) | ((hi as u64) << 32);
let reg1 = (lo as u64) | ((mid as u64) << 32);
[reg0, reg1]
}
pub fn from_dotnet_regs(regs: [u64; 2]) -> Self {
let flags = regs[0] as u32;
let hi = (regs[0] >> 32) as u32;
let lo = regs[1] as u32;
let mid = (regs[1] >> 32) as u32;
let mut bytes = [0u8; 16];
bytes[0..4].copy_from_slice(&flags.to_le_bytes());
bytes[4..8].copy_from_slice(&lo.to_le_bytes());
bytes[8..12].copy_from_slice(&mid.to_le_bytes());
bytes[12..16].copy_from_slice(&hi.to_le_bytes());
XurDecimal(Decimal::deserialize(bytes))
}
}
pub(crate) fn register(registry: &Registry) {
let mut m = registry.module("std::decimal");
m.ty::<Error>("Error");
m.ty::<XurDecimal>("Decimal")
.method("scale", |d: &XurDecimal| -> i64 { d.0.scale() as i64 })
.method("is_sign_negative", |d: &XurDecimal| -> bool {
d.0.is_sign_negative()
})
.method("is_sign_positive", |d: &XurDecimal| -> bool {
d.0.is_sign_positive()
})
.method("abs", |d: &XurDecimal| -> XurDecimal {
XurDecimal(d.0.abs())
})
.method("floor", |d: &XurDecimal| -> XurDecimal {
XurDecimal(d.0.floor())
})
.method("ceil", |d: &XurDecimal| -> XurDecimal {
XurDecimal(d.0.ceil())
})
.method("round", |d: &XurDecimal| -> XurDecimal {
XurDecimal(d.0.round())
})
.named_method(
"round_dp",
&["dp"],
|d: &XurDecimal, dp: i64| -> XurDecimal { XurDecimal(d.0.round_dp(dp as u32)) },
)
.method("to_int", |d: &XurDecimal| -> Result<i64, Error> {
use rust_decimal::prelude::ToPrimitive;
d.0.to_i64().ok_or(Error::OutOfRange)
})
.method("to_float", |d: &XurDecimal| -> Result<f64, Error> {
use rust_decimal::prelude::ToPrimitive;
d.0.to_f64().ok_or(Error::OutOfRange)
});
m.named_function("from_int", &["value"], |value: i64| -> XurDecimal {
XurDecimal(Decimal::from(value))
});
m.named_function(
"from_float",
&["value"],
|value: f64| -> Result<XurDecimal, Error> {
Decimal::try_from(value)
.map(XurDecimal)
.map_err(|_| Error::OutOfRange)
},
);
m.named_function(
"from_str",
&["value"],
|value: XurStr| -> Result<XurDecimal, Error> {
value
.parse::<Decimal>()
.map(XurDecimal)
.map_err(|_| Error::Parse)
},
);
}