1use facet::Facet;
4use rust_decimal::Decimal;
5use xur_runtime::{Registry, XurStr};
6
7#[derive(Clone, Copy, Facet)]
9#[facet(opaque)]
10pub struct XurDecimal(pub Decimal);
11
12#[derive(Clone, Copy, Facet)]
15#[repr(C)]
16pub enum Error {
17 OutOfRange,
20 Parse,
22}
23
24impl std::fmt::Display for Error {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 f.write_str(match self {
27 Error::OutOfRange => "value out of range for the target type",
28 Error::Parse => "invalid decimal string",
29 })
30 }
31}
32
33impl std::fmt::Display for XurDecimal {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 write!(f, "{}", self.0)
36 }
37}
38
39impl XurDecimal {
40 pub fn to_dotnet_regs(&self) -> [u64; 2] {
47 let bytes = self.0.serialize();
48 let flags = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
50 let lo = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]);
51 let mid = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]);
52 let hi = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]);
53 let reg0 = (flags as u64) | ((hi as u64) << 32);
55 let reg1 = (lo as u64) | ((mid as u64) << 32);
56 [reg0, reg1]
57 }
58
59 pub fn from_dotnet_regs(regs: [u64; 2]) -> Self {
61 let flags = regs[0] as u32;
62 let hi = (regs[0] >> 32) as u32;
63 let lo = regs[1] as u32;
64 let mid = (regs[1] >> 32) as u32;
65 let mut bytes = [0u8; 16];
67 bytes[0..4].copy_from_slice(&flags.to_le_bytes());
68 bytes[4..8].copy_from_slice(&lo.to_le_bytes());
69 bytes[8..12].copy_from_slice(&mid.to_le_bytes());
70 bytes[12..16].copy_from_slice(&hi.to_le_bytes());
71 XurDecimal(Decimal::deserialize(bytes))
72 }
73}
74
75pub(crate) fn register(registry: &Registry) {
76 let mut m = registry.module("std::decimal");
77
78 m.ty::<Error>("Error");
79
80 m.ty::<XurDecimal>("Decimal")
81 .method("scale", |d: &XurDecimal| -> i64 { d.0.scale() as i64 })
82 .method("is_sign_negative", |d: &XurDecimal| -> bool {
83 d.0.is_sign_negative()
84 })
85 .method("is_sign_positive", |d: &XurDecimal| -> bool {
86 d.0.is_sign_positive()
87 })
88 .method("abs", |d: &XurDecimal| -> XurDecimal {
89 XurDecimal(d.0.abs())
90 })
91 .method("floor", |d: &XurDecimal| -> XurDecimal {
92 XurDecimal(d.0.floor())
93 })
94 .method("ceil", |d: &XurDecimal| -> XurDecimal {
95 XurDecimal(d.0.ceil())
96 })
97 .method("round", |d: &XurDecimal| -> XurDecimal {
98 XurDecimal(d.0.round())
99 })
100 .named_method(
101 "round_dp",
102 &["dp"],
103 |d: &XurDecimal, dp: i64| -> XurDecimal { XurDecimal(d.0.round_dp(dp as u32)) },
104 )
105 .method("to_int", |d: &XurDecimal| -> Result<i64, Error> {
106 use rust_decimal::prelude::ToPrimitive;
107 d.0.to_i64().ok_or(Error::OutOfRange)
108 })
109 .method("to_float", |d: &XurDecimal| -> Result<f64, Error> {
110 use rust_decimal::prelude::ToPrimitive;
111 d.0.to_f64().ok_or(Error::OutOfRange)
112 });
113
114 m.named_function("from_int", &["value"], |value: i64| -> XurDecimal {
115 XurDecimal(Decimal::from(value))
116 });
117 m.named_function(
118 "from_float",
119 &["value"],
120 |value: f64| -> Result<XurDecimal, Error> {
121 Decimal::try_from(value)
122 .map(XurDecimal)
123 .map_err(|_| Error::OutOfRange)
124 },
125 );
126 m.named_function(
127 "from_str",
128 &["value"],
129 |value: XurStr| -> Result<XurDecimal, Error> {
130 value
131 .parse::<Decimal>()
132 .map(XurDecimal)
133 .map_err(|_| Error::Parse)
134 },
135 );
136}