search_core/cost/
decimal.rs1use core::fmt;
4use core::ops::Add;
5
6use super::Cost;
7
8mod parse;
9
10pub use parse::ParseDecimalError;
11
12#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
51pub struct Decimal<const K: u32>(u64);
52
53impl<const K: u32> Decimal<K> {
54 pub const SCALE: u64 = {
58 assert!(K <= 19, "Decimal supports at most 19 decimal places");
59 10_u64.pow(K)
60 };
61
62 #[must_use]
64 pub const fn from_raw(raw: u64) -> Self {
65 let _ = Self::SCALE;
66 Self(raw)
67 }
68
69 #[must_use]
71 pub const fn from_int(whole: u64) -> Option<Self> {
72 match whole.checked_mul(Self::SCALE) {
73 Some(raw) => Some(Self(raw)),
74 None => None,
75 }
76 }
77
78 #[must_use]
80 pub const fn raw(self) -> u64 {
81 self.0
82 }
83}
84
85impl<const K: u32> Add for Decimal<K> {
86 type Output = Self;
87
88 fn add(self, other: Self) -> Self {
89 Self(self.0 + other.0)
90 }
91}
92
93impl<const K: u32> Cost for Decimal<K> {
94 const ZERO: Self = Self::from_raw(0);
95
96 fn saturating_add(self, other: Self) -> Self {
97 Self(self.0.saturating_add(other.0))
98 }
99
100 fn saturating_times(self, factor: u32) -> Self {
101 Self(self.0.saturating_mul(u64::from(factor)))
102 }
103}
104
105impl<const K: u32> fmt::Display for Decimal<K> {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 let whole = self.0 / Self::SCALE;
108 if K == 0 {
109 return write!(f, "{whole}");
110 }
111
112 let fraction = self.0 % Self::SCALE;
113 let digits = K as usize;
114
115 write!(f, "{whole}.{fraction:0digits$}")
116 }
117}
118
119impl<const K: u32> fmt::Debug for Decimal<K> {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 fmt::Display::fmt(self, f)
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 extern crate std;
130
131 use std::string::ToString;
132
133 use super::{Cost, Decimal};
134 use proptest::prelude::*;
135
136 #[test]
138 fn display_pads_fraction() {
139 assert_eq!(Decimal::<2>::from_raw(5).to_string(), "0.05");
140 assert_eq!(Decimal::<3>::from_raw(12_000).to_string(), "12.000");
141 assert_eq!(format_debug(Decimal::<1>::from_raw(15)), "1.5");
142 }
143
144 #[test]
146 fn display_without_fraction() {
147 assert_eq!(Decimal::<0>::from_raw(42).to_string(), "42");
148 assert_eq!(Decimal::<0>::SCALE, 1);
149 }
150
151 #[test]
153 fn display_largest_scale() {
154 assert_eq!(
155 Decimal::<19>::from_raw(u64::MAX).to_string(),
156 "1.8446744073709551615"
157 );
158 assert_eq!(Decimal::<19>::SCALE, 10_000_000_000_000_000_000);
159 }
160
161 #[test]
163 fn from_int_checks_overflow() {
164 assert_eq!(Decimal::<3>::from_int(7).map(Decimal::raw), Some(7_000));
165 assert_eq!(
166 Decimal::<19>::from_int(1).map(Decimal::raw),
167 Some(Decimal::<19>::SCALE)
168 );
169 assert_eq!(Decimal::<19>::from_int(2), None);
170 }
171
172 #[test]
174 fn saturating_operations_clamp() {
175 let max = Decimal::<2>::from_raw(u64::MAX);
176 assert_eq!(max.saturating_add(Decimal::from_raw(1)), max);
177 assert_eq!(max.saturating_times(2), max);
178 assert_eq!(max.saturating_times(0), Decimal::ZERO);
179 }
180
181 fn format_debug(value: Decimal<1>) -> std::string::String {
183 std::format!("{value:?}")
184 }
185
186 proptest! {
187 #[test]
189 fn matches_raw_arithmetic(a in 0..u64::MAX / 2, b in 0..u64::MAX / 2) {
190 let (x, y) = (Decimal::<4>::from_raw(a), Decimal::<4>::from_raw(b));
191 prop_assert_eq!((x + y).raw(), a + b);
192 prop_assert_eq!(x.cmp(&y), a.cmp(&b));
193 }
194
195 #[test]
197 fn display_round_trips(raw: u64) {
198 let text = Decimal::<6>::from_raw(raw).to_string();
199 let (whole, fraction) = text.split_once('.').expect("K > 0 prints a point");
200 prop_assert_eq!(fraction.len(), 6);
201 let parsed = whole.parse::<u64>().unwrap() * 1_000_000 + fraction.parse::<u64>().unwrap();
202 prop_assert_eq!(parsed, raw);
203 }
204 }
205}