From 9768921ff7dbf89d6b99213ebe9aecf2a4c2d8e8 Mon Sep 17 00:00:00 2001 From: zyycn Date: Sun, 2 Aug 2026 00:26:55 +0800 Subject: [PATCH] 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 --- .../gateway-admin/src/model/observability.rs | 34 ++++--------------- .../crates/gateway-core/src/accounting/mod.rs | 24 +++++++++++++ .../gateway-core/tests/accounting/mod.rs | 19 +++++++++++ 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/backend/crates/gateway-admin/src/model/observability.rs b/backend/crates/gateway-admin/src/model/observability.rs index 60a4e8f0b..414868d05 100644 --- a/backend/crates/gateway-admin/src/model/observability.rs +++ b/backend/crates/gateway-admin/src/model/observability.rs @@ -204,42 +204,22 @@ impl DecimalAmount { /// 精确相加两个 `numeric(20,10)` 金额。 #[must_use] pub fn checked_add(&self, other: &Self) -> Option { - 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 { - 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 { - 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 { + self.0.parse().ok() } -} -fn scaled_amount(value: &str) -> Option { - let (whole, fraction) = value.split_once('.').unwrap_or((value, "")); - let whole = whole.parse::().ok()?; - let fraction = format!("{fraction:0<10}").parse::().ok()?; - whole.checked_mul(10_u128.pow(10))?.checked_add(fraction) + fn from_decimal(value: gateway_core::accounting::Decimal) -> Option { + Some(Self(value.canonical())) + } } impl FromStr for DecimalAmount { diff --git a/backend/crates/gateway-core/src/accounting/mod.rs b/backend/crates/gateway-core/src/accounting/mod.rs index caaae04ae..f69f75193 100644 --- a/backend/crates/gateway-core/src/accounting/mod.rs +++ b/backend/crates/gateway-core/src/accounting/mod.rs @@ -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 { + 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 { diff --git a/backend/crates/gateway-core/tests/accounting/mod.rs b/backend/crates/gateway-core/tests/accounting/mod.rs index e0865fb7a..def9fa448 100644 --- a/backend/crates/gateway-core/tests/accounting/mod.rs +++ b/backend/crates/gateway-core/tests/accounting/mod.rs @@ -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)