icydb_model/base/types/
num.rs1use crate::prelude::*;
8
9#[newtype(
14 primitive = "Nat16",
15 item(prim = "Nat16"),
16 ty(rule(name = "range", kind = "numeric_range_inclusive", args(0, 360)))
17)]
18pub struct Degrees {}
19
20#[newtype(
27 primitive = "Nat8",
28 item(prim = "Nat8"),
29 ty(rule(name = "range", kind = "numeric_range_inclusive", args(0, 100)))
30)]
31pub struct Percent {}
32
33#[newtype(
38 primitive = "Nat16",
39 item(prim = "Nat16"),
40 ty(rule(name = "range", kind = "numeric_range_inclusive", args(0, 10_000)))
41)]
42pub struct PercentModifier {}
43
44#[record(
49 fields(
50 field(name = "min", value(item(prim = "Decimal", scale = 18))),
51 field(name = "max", value(item(prim = "Decimal", scale = 18))),
52 ),
53 traits(remove(ValidateCustom))
54)]
55pub struct DecimalRange {}
56
57impl DecimalRange {
58 #[must_use]
59 pub const fn new(min: Decimal, max: Decimal) -> Self {
60 Self { min, max }
61 }
62}
63
64impl ValidateCustom for DecimalRange {
65 fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
66 let validator = base::validator::num::Lte::new(self.max);
67
68 validator.validate(&self.min, ctx);
69 }
70}
71
72#[record(
77 fields(
78 field(name = "min", value(item(prim = "Duration"))),
79 field(name = "max", value(item(prim = "Duration"))),
80 ),
81 traits(remove(ValidateCustom))
82)]
83pub struct DurationRange {}
84
85impl DurationRange {
86 #[must_use]
87 pub const fn new(min: Duration, max: Duration) -> Self {
88 Self { min, max }
89 }
90}
91
92impl ValidateCustom for DurationRange {
93 fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
94 let validator = base::validator::num::Lte::new(self.max);
95
96 validator.validate(&self.min, ctx);
97 }
98}
99
100#[record(
105 fields(
106 field(name = "min", value(item(prim = "Int32"))),
107 field(name = "max", value(item(prim = "Int32"))),
108 ),
109 traits(remove(ValidateCustom))
110)]
111pub struct Int32Range {}
112
113impl Int32Range {
114 #[must_use]
115 pub const fn new(min: i32, max: i32) -> Self {
116 Self { min, max }
117 }
118}
119
120impl ValidateCustom for Int32Range {
121 fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
122 let validator = base::validator::num::Lte::new(self.max);
123
124 validator.validate(&self.min, ctx);
125 }
126}
127
128#[record(
133 fields(
134 field(name = "min", value(item(prim = "Nat32"))),
135 field(name = "max", value(item(prim = "Nat32"))),
136 ),
137 traits(remove(ValidateCustom))
138)]
139pub struct Nat32Range {}
140
141impl Nat32Range {
142 #[must_use]
143 pub const fn new(min: u32, max: u32) -> Self {
144 Self { min, max }
145 }
146}
147
148impl ValidateCustom for Nat32Range {
149 fn validate_custom(&self, ctx: &mut dyn VisitorContext) {
150 let validator = base::validator::num::Lte::new(self.max);
151
152 validator.validate(&self.min, ctx);
153 }
154}