entity/ent/query/
filter.rs

1use crate::{Id, Predicate, TypedPredicate};
2
3/// Represents some filter to apply against an ent when searching through
4/// a database
5#[derive(Clone, Debug)]
6pub enum Filter {
7    /// Filters by the ent's id
8    Id(TypedPredicate<Id>),
9
10    /// Filters by the ent's type
11    Type(TypedPredicate<String>),
12
13    /// Filters by the ent's creation timestamp
14    Created(TypedPredicate<u64>),
15
16    /// Filters by the ent's last updated timestamp
17    LastUpdated(TypedPredicate<u64>),
18
19    /// Filters by an ent's field
20    Field(String, Predicate),
21
22    /// Filters by an ent connected by an edge; not the same as
23    /// [`Filter::IntoEdge`], which converts an ent to its edge's ents
24    Edge(String, Box<Filter>),
25
26    /// **(Special case)** Filters by converting an ent into the ents on its edge
27    IntoEdge(String),
28}
29
30impl Filter {
31    pub fn where_id<P: Into<TypedPredicate<Id>>>(p: P) -> Self {
32        Self::Id(p.into())
33    }
34
35    pub fn where_type<P: Into<TypedPredicate<String>>>(p: P) -> Self {
36        Self::Type(p.into())
37    }
38
39    pub fn where_created<P: Into<TypedPredicate<u64>>>(p: P) -> Self {
40        Self::Created(p.into())
41    }
42
43    pub fn where_last_updated<P: Into<TypedPredicate<u64>>>(p: P) -> Self {
44        Self::LastUpdated(p.into())
45    }
46
47    pub fn where_field<S: Into<String>, P: Into<Predicate>>(name: S, p: P) -> Self {
48        Self::Field(name.into(), p.into())
49    }
50
51    pub fn where_edge<S: Into<String>, F: Into<Filter>>(name: S, filter: F) -> Self {
52        Self::Edge(name.into(), Box::new(filter.into()))
53    }
54
55    pub fn where_into_edge<S: Into<String>>(name: S) -> Self {
56        Self::IntoEdge(name.into())
57    }
58}