1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{Id, Predicate, TypedPredicate};

/// Represents some filter to apply against an ent when searching through
/// a database
#[derive(Clone, Debug)]
pub enum Filter {
    /// Filters by the ent's id
    Id(TypedPredicate<Id>),

    /// Filters by the ent's type
    Type(TypedPredicate<String>),

    /// Filters by the ent's creation timestamp
    Created(TypedPredicate<u64>),

    /// Filters by the ent's last updated timestamp
    LastUpdated(TypedPredicate<u64>),

    /// Filters by an ent's field
    Field(String, Predicate),

    /// Filters by an ent connected by an edge; not the same as
    /// [`Filter::IntoEdge`], which converts an ent to its edge's ents
    Edge(String, Box<Filter>),

    /// **(Special case)** Filters by converting an ent into the ents on its edge
    IntoEdge(String),
}

impl Filter {
    pub fn where_id<P: Into<TypedPredicate<Id>>>(p: P) -> Self {
        Self::Id(p.into())
    }

    pub fn where_type<P: Into<TypedPredicate<String>>>(p: P) -> Self {
        Self::Type(p.into())
    }

    pub fn where_created<P: Into<TypedPredicate<u64>>>(p: P) -> Self {
        Self::Created(p.into())
    }

    pub fn where_last_updated<P: Into<TypedPredicate<u64>>>(p: P) -> Self {
        Self::LastUpdated(p.into())
    }

    pub fn where_field<S: Into<String>, P: Into<Predicate>>(name: S, p: P) -> Self {
        Self::Field(name.into(), p.into())
    }

    pub fn where_edge<S: Into<String>, F: Into<Filter>>(name: S, filter: F) -> Self {
        Self::Edge(name.into(), Box::new(filter.into()))
    }

    pub fn where_into_edge<S: Into<String>>(name: S) -> Self {
        Self::IntoEdge(name.into())
    }
}