icydb_base/types/
num.rs

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