rorm/fields/traits/mod.rs
1//! Traits defining types which can be used as fields.
2
3use crate::conditions::Value;
4use crate::internal::array_utils::Array;
5use crate::internal::field::decoder::FieldDecoder;
6use crate::internal::field::modifier::{AnnotationsModifier, CheckModifier, ColumnsFromName};
7use crate::internal::field::Field;
8use crate::internal::imr;
9
10pub mod cmp;
11
12pub use cmp::*;
13
14/// Base trait for types which are allowed as fields in models
15pub trait FieldType: 'static {
16 /// Array with length specific to the field type
17 type Columns<T>: Array<Item = T>;
18
19 /// Construct an array of [`Value`] representing `self` in the database via ownership
20 fn into_values(self) -> Self::Columns<Value<'static>>;
21
22 /// Construct an array of [`Value`] representing `self` in the database via borrowing
23 fn as_values(&self) -> Self::Columns<Value<'_>>;
24
25 /// Construct an array of [`imr::Field`] representing this type
26 fn get_imr<F: Field<Type = Self>>() -> Self::Columns<imr::Field>;
27
28 /// [`FieldDecoder`] to use for fields of this type
29 type Decoder: FieldDecoder<Result = Self>;
30
31 /// `const fn<F: Field>() -> Option<Annotations>`
32 /// to allow modifying the a field's annotations which is of this type
33 ///
34 /// For example can be used to set `nullable` implicitly for `Option<_>`.
35 type AnnotationsModifier<F: Field<Type = Self>>: AnnotationsModifier<F>;
36
37 /// `const fn<F: Field>() -> Result<(), &'static str>`
38 /// to allow custom compile time checks.
39 ///
40 /// For example can be used to ensure `String` has a `max_lenght`.
41 type CheckModifier<F: Field<Type = Self>>: CheckModifier<F>;
42
43 /// `const fn<F: Field>() -> Self::Columns<&'static str>`
44 type ColumnsFromName<F: Field<Type = Self>>: ColumnsFromName<F>;
45}