icydb_model/base/validator/decimal.rs
1//! Module: base::validator::decimal
2//!
3//! Responsibility: base validator definitions.
4//! Does not own: normalization policy, persistence, or schema mutation semantics.
5//! Boundary: reports typed visitor issues for facade schema values.
6
7use crate::{prelude::*, visitor::Validator};
8
9///
10/// MaxDecimalPlaces
11///
12/// Enforces an upper bound on fractional precision for `Decimal` values.
13/// Values with a larger scale than `target` are rejected.
14///
15
16#[validator]
17pub struct MaxDecimalPlaces {
18 target: u32,
19}
20
21impl MaxDecimalPlaces {
22 /// Create a new validator with the given maximum number of decimal places.
23 pub fn new(target: impl TryInto<u32>) -> Self {
24 Self {
25 target: target.try_into().unwrap_or_default(),
26 }
27 }
28}
29
30impl Validator<Decimal> for MaxDecimalPlaces {
31 fn validate(&self, n: &Decimal, ctx: &mut dyn VisitorContext) {
32 if n.scale() > self.target {
33 ctx.issue(format!(
34 "decimal scale {} must be at most {}",
35 n.scale(),
36 self.target
37 ));
38 }
39 }
40}