refactor(accounting): delegate admin decimal ops to core Decimal

Core Decimal gains canonical() and checked_div_u64(); gateway-admin
DecimalAmount now delegates arithmetic to core instead of reimplementing
scaled parsing, removing the duplicate scaled_amount logic.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
zyycn
2026-08-02 00:31:43 +08:00
co-authored by Claude Fable 5
parent db90f0631a
commit 9768921ff7
3 changed files with 50 additions and 27 deletions
@@ -204,42 +204,22 @@ impl DecimalAmount {
/// 精确相加两个 `numeric(20,10)` 金额。
#[must_use]
pub fn checked_add(&self, other: &Self) -> Option<Self> {
let sum = scaled_amount(&self.0)?.checked_add(scaled_amount(&other.0)?)?;
Self::from_scaled(sum)
Self::from_decimal(self.to_decimal()?.checked_add(other.to_decimal()?)?)
}
/// 将金额按非零请求数均分,保留最多十位小数。
#[must_use]
pub fn checked_div_u64(&self, divisor: u64) -> Option<Self> {
let divisor = u128::from(divisor);
(divisor != 0)
.then(|| scaled_amount(&self.0)?.checked_div(divisor))
.flatten()
.and_then(Self::from_scaled)
Self::from_decimal(self.to_decimal()?.checked_div_u64(divisor)?)
}
fn from_scaled(value: u128) -> Option<Self> {
if value >= 10_u128.pow(20) {
return None;
}
let whole = value / 10_u128.pow(10);
let fraction = value % 10_u128.pow(10);
let value = if fraction == 0 {
whole.to_string()
} else {
format!("{whole}.{fraction:010}")
.trim_end_matches('0')
.to_owned()
};
Some(Self(value))
fn to_decimal(&self) -> Option<gateway_core::accounting::Decimal> {
self.0.parse().ok()
}
}
fn scaled_amount(value: &str) -> Option<u128> {
let (whole, fraction) = value.split_once('.').unwrap_or((value, ""));
let whole = whole.parse::<u128>().ok()?;
let fraction = format!("{fraction:0<10}").parse::<u128>().ok()?;
whole.checked_mul(10_u128.pow(10))?.checked_add(fraction)
fn from_decimal(value: gateway_core::accounting::Decimal) -> Option<Self> {
Some(Self(value.canonical()))
}
}
impl FromStr for DecimalAmount {
@@ -39,6 +39,30 @@ impl Decimal {
.filter(|value| *value <= MAX_SCALED_DECIMAL)
.map(Self)
}
/// 除以非零整数,保留最多十位小数。
#[must_use]
pub fn checked_div_u64(self, divisor: u64) -> Option<Self> {
let divisor = u128::from(divisor);
(divisor != 0)
.then(|| self.0.checked_div(divisor))
.flatten()
.and_then(|value| Self::from_scaled(value).ok())
}
/// 去尾零的 canonical 字符串,用于 wire 序列化。
#[must_use]
pub fn canonical(self) -> String {
let integer = self.0 / DECIMAL_SCALE;
let fraction = self.0 % DECIMAL_SCALE;
if fraction == 0 {
integer.to_string()
} else {
let fraction = format!("{fraction:010}");
let trimmed = fraction.trim_end_matches('0');
format!("{integer}.{trimmed}")
}
}
}
impl FromStr for Decimal {
@@ -12,6 +12,25 @@ fn decimal_should_render_with_database_scale() {
assert_eq!(decimal.to_string(), "12.3400000000");
}
#[test]
fn decimal_canonical_should_strip_trailing_zeroes() {
assert_eq!(Decimal::from_str("12.34").unwrap().canonical(), "12.34");
assert_eq!(Decimal::from_str("12").unwrap().canonical(), "12");
assert_eq!(Decimal::from_str("0").unwrap().canonical(), "0");
assert_eq!(
Decimal::from_str("1.0000000000").unwrap().canonical(),
"1"
);
}
#[test]
fn decimal_div_u64_should_keep_up_to_ten_fraction_digits() {
let decimal = Decimal::from_str("10").unwrap();
let third = decimal.checked_div_u64(3).unwrap();
assert_eq!(third.canonical(), "3.3333333333");
assert_eq!(decimal.checked_div_u64(0), None);
}
#[test]
fn provider_usd_ticks_should_map_exactly_to_decimal_scale() {
let estimate = ProviderReportedCost::from_usd_ticks(12_345_678_901)