1#[macro_use]
2mod macros;
3mod sanitize;
4mod validate;
5mod view;
6mod visitable;
7
8pub use sanitize::*;
9pub use validate::*;
10pub use view::*;
11pub use visitable::*;
12
13pub use canic_cdk::structures::storable::Storable;
16pub use num_traits::{FromPrimitive as NumFromPrimitive, NumCast, ToPrimitive as NumToPrimitive};
17pub use serde::{Deserialize, Serialize, de::DeserializeOwned};
18pub use std::{
19 cmp::{Eq, Ordering, PartialEq},
20 convert::{AsRef, From, Into},
21 default::Default,
22 fmt::{Debug, Display},
23 hash::Hash,
24 iter::IntoIterator,
25 ops::{Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Sub, SubAssign},
26 str::FromStr,
27};
28
29use crate::{
30 IndexSpec, Key, Value,
31 db::primitives::{
32 BoolEqualityFilterKind, BoolListFilterKind, FilterKind, Int64RangeFilterKind,
33 IntListFilterKind, Nat64RangeFilterKind, NatListFilterKind, TextFilterKind,
34 TextListFilterKind,
35 },
36};
37
38pub trait Kind: Path + 'static {}
48
49impl<T> Kind for T where T: Path + 'static {}
50
51pub trait CanisterKind: Kind {}
56
57pub trait EntityKind: Kind + TypeKind + FieldValues {
62 type PrimaryKey: Copy + Into<Key>;
63 type Store: StoreKind;
64 type Canister: CanisterKind; const ENTITY_ID: u64;
67 const PRIMARY_KEY: &'static str;
68 const FIELDS: &'static [&'static str];
69 const INDEXES: &'static [&'static IndexSpec];
70
71 fn key(&self) -> Key;
72 fn primary_key(&self) -> Self::PrimaryKey;
73 fn set_primary_key(&mut self, key: Self::PrimaryKey);
74}
75
76pub trait StoreKind: Kind {
81 type Canister: CanisterKind;
82}
83
84pub trait TypeKind:
94 Kind
95 + View
96 + Clone
97 + Default
98 + Serialize
99 + DeserializeOwned
100 + Sanitize
101 + Validate
102 + Visitable
103 + PartialEq
104{
105}
106
107impl<T> TypeKind for T where
108 T: Kind
109 + View
110 + Clone
111 + Default
112 + DeserializeOwned
113 + PartialEq
114 + Serialize
115 + Sanitize
116 + Validate
117 + Visitable
118{
119}
120
121pub trait FieldValues {
130 fn get_value(&self, field: &str) -> Option<Value>;
131}
132
133pub trait FieldValue {
138 fn to_value(&self) -> Value {
139 Value::Unsupported
140 }
141}
142
143impl FieldValue for &str {
144 fn to_value(&self) -> Value {
145 Value::Text((*self).to_string())
146 }
147}
148
149impl FieldValue for String {
150 fn to_value(&self) -> Value {
151 Value::Text(self.clone())
152 }
153}
154
155impl<T: FieldValue + Clone> FieldValue for &T {
156 fn to_value(&self) -> Value {
157 (*self).clone().to_value()
158 }
159}
160
161impl<T: FieldValue> FieldValue for Option<T> {
162 fn to_value(&self) -> Value {
163 match self {
164 Some(v) => v.to_value(),
165 None => Value::None,
166 }
167 }
168}
169
170impl<T: FieldValue> FieldValue for Vec<T> {
171 fn to_value(&self) -> Value {
172 Value::List(self.iter().map(FieldValue::to_value).collect())
173 }
174}
175
176impl<T: FieldValue> FieldValue for Box<T> {
177 fn to_value(&self) -> Value {
178 (**self).to_value()
179 }
180}
181
182#[macro_export]
184macro_rules! impl_field_value {
185 ( $( $type:ty => $variant:ident ),* $(,)? ) => {
186 $(
187 impl FieldValue for $type {
188 fn to_value(&self) -> Value {
189 Value::$variant((*self).into())
190 }
191 }
192 )*
193 };
194}
195
196impl_field_value!(
197 i8 => Int,
198 i16 => Int,
199 i32 => Int,
200 i64 => Int,
201 u8 => Uint,
202 u16 => Uint,
203 u32 => Uint,
204 u64 => Uint,
205 bool => Bool,
206);
207
208pub trait Filterable {
213 type Filter: FilterKind;
214 type ListFilter: FilterKind;
215}
216
217macro_rules! impl_filterable {
218 ( $( $type:ty => $filter:path, $list_filter:path );* $(;)? ) => {
220 $(
221 impl Filterable for $type {
222 type Filter = $filter;
223 type ListFilter = $list_filter;
224 }
225 )*
226 };
227}
228
229impl_filterable! {
230 bool => BoolEqualityFilterKind, BoolListFilterKind;
231 i8 => Int64RangeFilterKind, IntListFilterKind;
232 i16 => Int64RangeFilterKind, IntListFilterKind;
233 i32 => Int64RangeFilterKind, IntListFilterKind;
234 i64 => Int64RangeFilterKind, IntListFilterKind;
235
236 u8 => Nat64RangeFilterKind, NatListFilterKind;
237 u16 => Nat64RangeFilterKind, NatListFilterKind;
238 u32 => Nat64RangeFilterKind, NatListFilterKind;
239 u64 => Nat64RangeFilterKind, NatListFilterKind;
240
241 String => TextFilterKind, TextListFilterKind;
242}
243
244pub trait FromKey: Copy {
251 fn try_from_key(key: Key) -> Option<Self>;
252}
253
254#[macro_export]
255macro_rules! impl_from_key_int {
256 ( $( $ty:ty ),* $(,)? ) => {
257 $(
258 impl FromKey for $ty {
259 fn try_from_key(key: Key) -> Option<Self> {
260 match key {
261 Key::Int(v) => Self::try_from(v).ok(),
262 _ => None,
263 }
264 }
265 }
266 )*
267 };
268}
269
270#[macro_export]
271macro_rules! impl_from_key_uint {
272 ( $( $ty:ty ),* $(,)? ) => {
273 $(
274 impl FromKey for $ty {
275 fn try_from_key(key: Key) -> Option<Self> {
276 match key {
277 Key::Uint(v) => Self::try_from(v).ok(),
278 _ => None,
279 }
280 }
281 }
282 )*
283 };
284}
285
286impl_from_key_int!(i8, i16, i32, i64);
287impl_from_key_uint!(u8, u16, u32, u64);
288
289pub trait Inner<T> {
298 fn inner(&self) -> &T;
299 fn into_inner(self) -> T;
300}
301
302#[macro_export]
304macro_rules! impl_inner {
305 ($($type:ty),*) => {
306 $(
307 impl Inner<$type> for $type {
308 fn inner(&self) -> &$type {
309 &self
310 }
311 fn into_inner(self) -> $type {
312 self
313 }
314 }
315 )*
316 };
317}
318
319impl_inner!(
320 bool, f32, f64, i8, i16, i32, i64, i128, String, u8, u16, u32, u64, u128
321);
322
323pub trait Path {
331 const PATH: &'static str;
332}
333
334pub trait Sanitizer<T: ?Sized> {
340 fn sanitize(&self, value: T) -> T;
342}
343
344pub trait Validator<T: ?Sized> {
350 fn validate(&self, value: &T) -> Result<(), String>;
351}