Dear people,
I'm trying to add in my app a new filtering system.
Here the code in the Rust Playground.
I'm trying to use enums like this:
pub struct ProductFilter {
pub id: Option<IdFilter>,
pub created_at: Option<DateTimeFilter>,
pub created_by: Option<IdFilter>,
pub updated_at: Option<Nullable<DateTimeFilter>>,
pub updated_by: Option<Nullable<IdFilter>>,
pub name: Option<StringFilter>,
pub price: Option<IntFilter>,
pub and: Option<Vec<ProductFilter>>,
pub or: Option<Vec<ProductFilter>>,
pub not: Option<Box<ProductFilter>>,
}
-
I don't know if creating
IdFilter
,DateTimeFilter
,StringFilter
etc. enums is a great idea; but I think is a pratical way to have precise, compile-time types correctness;pub enum StringFilter { Eq(String), Neq(String), In(Vec<String>), Contains(String), StartsWith(String), EndsWith(String), }
-
Some field can be Nullable and I need a way to express that in the DB query, so I wrote:
pub enum Nullable<T> { Filter(T), IsNull(bool), IsNotNull(bool), }
I'm not very proud of it, but nothing else came to mind. What do you recommend? I thought about having nested
Option<>
but I need to be able to expressIS NULL
andIS NOT NULL
and I could end up withOption<Option<Option<>>>
: not very practical.
Looking forward to hearing your advice!