icydb 0.180.37

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
use crate::{design::prelude::*, traits::Validator};

///
/// MaxDecimalPlaces
///
/// Enforces an upper bound on fractional precision for `Decimal` values.
/// Values with a larger scale than `target` are rejected.
///

#[validator]
pub struct MaxDecimalPlaces {
    target: u32,
}

impl MaxDecimalPlaces {
    /// Create a new validator with the given maximum number of decimal places.
    pub fn new(target: impl TryInto<u32>) -> Self {
        Self {
            target: target.try_into().unwrap_or_default(),
        }
    }
}

impl Validator<Decimal> for MaxDecimalPlaces {
    fn validate(&self, n: &Decimal, ctx: &mut dyn VisitorContext) {
        if n.scale() > self.target {
            ctx.issue(Issue::DecimalScaleMax {
                actual: n.scale(),
                max: self.target,
            });
        }
    }
}