Skip to main content

substreams_database_change/numeric/
traits.rs

1use std::cmp::Ordering;
2use substreams::scalar::BigDecimal;
3
4/// Trait for types that can be converted to BigDecimal
5///
6/// This trait provides a way to convert numeric types to BigDecimal for use in
7/// database operations. It's used as a base trait for numeric operations.
8pub trait ToBigDecimal {
9    /// Convert self to a BigDecimal
10    ///
11    /// For numeric types, this should be a lossless conversion.
12    /// For string types, this may parse and panic if the string is not a valid number.
13    fn to_big_decimal(&self) -> BigDecimal;
14}
15
16/// Trait for types that can be added to a BigDecimal in-place
17///
18/// This trait enables efficient accumulation by mutating a BigDecimal directly
19/// instead of creating intermediate allocations. Types implementing this trait
20/// can be used with the `add()` and `sub()` methods on database rows.
21///
22/// This trait extends `ToBigDecimal` to allow conversion to BigDecimal when needed.
23pub trait NumericAddable: ToBigDecimal {
24    /// Add self to the target BigDecimal, mutating it directly
25    ///
26    /// This enables efficient accumulation without intermediate allocations.
27    /// For example: `100i64.add_assign_to(&mut target)` will add 100 to target.
28    fn add_assign_to(&self, target: &mut BigDecimal);
29
30    /// Subtract self from the target BigDecimal, mutating it directly
31    ///
32    /// Equivalent to: `*target -= self`
33    /// For example: `50i64.sub_assign_from(&mut target)` will subtract 50 from target.
34    fn sub_assign_from(&self, target: &mut BigDecimal);
35}
36
37/// Trait for types that can be compared against a BigDecimal for min/max operations
38///
39/// This trait leverages BigDecimal's native PartialOrd implementations for primitive
40/// types, enabling zero-allocation comparisons for integers.
41pub trait NumericComparable: ToBigDecimal {
42    /// Compare self against a BigDecimal value
43    ///
44    /// For primitive integers, this uses BigDecimal's native PartialOrd implementation
45    /// which requires no allocation. For other types, conversion may be needed.
46    fn cmp_to_big_decimal(&self, other: &BigDecimal) -> Ordering;
47}