Skip to main content

icydb_model/base/types/
num.rs

1//! Module: base::types::num
2//!
3//! Responsibility: base domain type declarations.
4//! Does not own: runtime storage, query execution, or validator implementation internals.
5//! Boundary: declares macro-modeled domain wrappers and records for downstream schemas.
6
7use crate::prelude::*;
8
9///
10/// Degrees (°)
11///
12
13#[newtype(
14    item(prim = "Nat16"),
15    ty(rule(name = "range", numeric_range_inclusive(min = 0, max = 360)))
16)]
17pub struct Degrees {}
18
19///
20/// Percent
21///
22/// basic percentage as an integer
23///
24
25#[newtype(
26    item(prim = "Nat8"),
27    ty(rule(name = "range", numeric_range_inclusive(min = 0, max = 100)))
28)]
29pub struct Percent {}
30
31///
32/// PercentModifier
33///
34
35#[newtype(
36    item(prim = "Nat16"),
37    ty(rule(name = "range", numeric_range_inclusive(min = 0, max = 10_000)))
38)]
39pub struct PercentModifier {}
40
41///
42/// DecimalRange
43///
44
45#[record(
46    fields(
47        field(name = "min", value(item(prim = "Decimal", scale = 18))),
48        field(name = "max", value(item(prim = "Decimal", scale = 18))),
49    ),
50    traits(remove(ValidateCustom))
51)]
52pub struct DecimalRange {}
53
54impl DecimalRange {
55    #[must_use]
56    pub const fn new(min: Decimal, max: Decimal) -> Self {
57        Self { min, max }
58    }
59}
60
61impl ValidateCustom for DecimalRange {
62    fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
63        let validator = base::validator::num::Lte::new(self.max);
64
65        validator.validate(&self.min, ctx);
66    }
67}
68
69///
70/// DurationRange
71///
72
73#[record(
74    fields(
75        field(name = "min", value(item(prim = "Duration"))),
76        field(name = "max", value(item(prim = "Duration"))),
77    ),
78    traits(remove(ValidateCustom))
79)]
80pub struct DurationRange {}
81
82impl DurationRange {
83    #[must_use]
84    pub const fn new(min: Duration, max: Duration) -> Self {
85        Self { min, max }
86    }
87}
88
89impl ValidateCustom for DurationRange {
90    fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
91        let validator = base::validator::num::Lte::new(self.max);
92
93        validator.validate(&self.min, ctx);
94    }
95}
96
97///
98/// Int32Range
99///
100
101#[record(
102    fields(
103        field(name = "min", value(item(prim = "Int32"))),
104        field(name = "max", value(item(prim = "Int32"))),
105    ),
106    traits(remove(ValidateCustom))
107)]
108pub struct Int32Range {}
109
110impl Int32Range {
111    #[must_use]
112    pub const fn new(min: i32, max: i32) -> Self {
113        Self { min, max }
114    }
115}
116
117impl ValidateCustom for Int32Range {
118    fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
119        let validator = base::validator::num::Lte::new(self.max);
120
121        validator.validate(&self.min, ctx);
122    }
123}
124
125///
126/// Nat32Range
127///
128
129#[record(
130    fields(
131        field(name = "min", value(item(prim = "Nat32"))),
132        field(name = "max", value(item(prim = "Nat32"))),
133    ),
134    traits(remove(ValidateCustom))
135)]
136pub struct Nat32Range {}
137
138impl Nat32Range {
139    #[must_use]
140    pub const fn new(min: u32, max: u32) -> Self {
141        Self { min, max }
142    }
143}
144
145impl ValidateCustom for Nat32Range {
146    fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
147        let validator = base::validator::num::Lte::new(self.max);
148
149        validator.validate(&self.min, ctx);
150    }
151}