tailwag_orm/queries/filters/
filterable_types.rs1use std::marker::PhantomData;
2
3use uuid::Uuid;
4
5use crate::data_definition::table::Identifier;
6
7use super::Filter;
8
9pub trait Filterable {
10 type FilterType: Default;
11}
12
13trait TypeFilter {}
15macro_rules! typetype {
16 ($item:ty) => {
17 impl TypeFilter for $item {}
19 };
20}
21macro_rules! impl_filter_for {
22 ($item:ty: $base_type:ty, $table_column_fn_name:ident, $param_type_enum:ident, $trait_name:ident $($func_name:ident:$comparison_type:ident),*) => {
23 impl $trait_name for FilterableType<$item> {
24 type Type = $base_type;
25 $(fn $func_name(
26 &self,
27 value: impl Into<<Self as $trait_name>::Type>,
28 ) -> Filter {
29 Filter::$comparison_type(
30 super::FilterComparisonParam::TableColumn(
31 self.column_name.clone(), ),
32 super::FilterComparisonParam::$param_type_enum(value.into()),
33 )
34 }
35 )*
36 }
37
38 };
39 }
40
41impl<T: TypeFilter> TypeFilter for Option<T> {}
42impl<T: TypeFilter> TypeFilter for Vec<T> {}
43macro_rules! impl_numeric_type {
68 ($type:ty: $db_type:ident) => {
69 typetype! {$type}
70 impl_filter_for!($type: $type, new_int, $db_type, FilterEq eq:Equal, ne:NotEqual);
71 impl_filter_for!($type: $type, new_int, $db_type, FilterPartialEq lt:LessThan, lte:LessThanOrEqual, gt:GreaterThan, gte:GreaterThanOrEqual);
72 }
73}
74
75typetype! {Uuid}
77impl_filter_for!(Uuid: uuid::Uuid, new_uuid, Uuid, FilterEq eq:Equal, ne:NotEqual);
78impl_filter_for!(Uuid: uuid::Uuid, new_uuid, Uuid, FilterLike like:Like);
79typetype! {bool}
80impl_filter_for!(bool: bool, new_bool, Bool, FilterEq eq:Equal, ne:NotEqual);
81typetype! {String}
82impl_filter_for!(String: String, new_string, String, FilterEq eq:Equal, ne:NotEqual);
83impl_filter_for!(String: String, new_string, String, FilterPartialEq lt:LessThan, lte:LessThanOrEqual, gt:GreaterThan, gte:GreaterThanOrEqual);
84impl_filter_for!(String: String, new_string, String, FilterLike like:Like);
85impl_numeric_type!(i64: Integer);
86impl_numeric_type!(f64: Float);
87
88typetype! {chrono::NaiveDateTime}
101impl_filter_for!(chrono::NaiveDateTime: chrono::NaiveDateTime, new_timestamp, Timestamp, FilterEq eq:Equal, ne:NotEqual);
102
103#[allow(private_bounds)]
107pub struct FilterableType<T: TypeFilter> {
108 column_name: Identifier,
110 _t: PhantomData<T>,
111}
112
113#[allow(private_bounds)]
114impl<T: TypeFilter> FilterableType<T> {
115 pub fn new(
116 column_name: Identifier,
118 ) -> Self {
119 Self {
120 column_name,
122 _t: PhantomData,
123 }
124 }
125}
126
127pub trait FilterEq {
128 type Type;
129 fn eq(
130 &self,
131 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterEq>::Type>,
132 ) -> Filter;
133 fn ne(
134 &self,
135 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterEq>::Type>,
136 ) -> Filter;
137}
138pub trait FilterPartialEq
139where
140 Self: FilterEq,
141{
142 type Type;
143 fn lt(
144 &self,
145 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterPartialEq>::Type>,
146 ) -> Filter;
147 fn gt(
148 &self,
149 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterPartialEq>::Type>,
150 ) -> Filter;
151 fn lte(
152 &self,
153 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterPartialEq>::Type>,
154 ) -> Filter;
155 fn gte(
156 &self,
157 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterPartialEq>::Type>,
158 ) -> Filter;
159}
160pub trait FilterLike {
161 type Type;
162 fn like(
163 &self,
164 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterLike>::Type>,
165 ) -> Filter;
166}
167pub trait FilterIn {
168 type Type;
169 fn contained_in(
170 &self,
171 t: impl Into<<Self as crate::queries::filters::filterable_types::FilterIn>::Type>,
172 ) -> Filter;
173}
174
175