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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::{filter::Filter, CompositeCompare, CompositeFieldRef};
use prisma_value::PrismaValue;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CompositeFilter {
    /// Starting field of the Composite traversal.
    pub field: CompositeFieldRef,

    // /// Filter the composite need to fulfill.
    // pub nested_filter: Box<Filter>,
    /// Condition the composite field filter uses.
    pub condition: Box<CompositeCondition>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum CompositeCondition {
    /// Every composite in the list needs to fulfill a condition.
    Every(Filter),

    /// One or more composite in a list needs to fulfill a condition.
    Some(Filter),

    /// No composite in a list must fulfill a condition.
    None(Filter),

    /// Checks whether or not the composite list is empty.
    Empty(bool),

    /// Entire composite equals the given value. Can be for lists or single composites.
    Equals(PrismaValue),

    /// To-one composite only - the composite must fulfill the filter.
    Is(Filter),

    /// To-one composite only - the composite must not fulfill the filter.
    IsNot(Filter),

    /// Checks whether or not the composite field exists (is `undefined` or not)
    IsSet(bool),
}

impl CompositeCompare for CompositeFieldRef {
    fn every<T>(&self, filter: T) -> Filter
    where
        T: Into<Filter>,
    {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::Every(filter.into())),
        }
        .into()
    }

    fn some<T>(&self, filter: T) -> Filter
    where
        T: Into<Filter>,
    {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::Some(filter.into())),
        }
        .into()
    }

    fn none<T>(&self, filter: T) -> Filter
    where
        T: Into<Filter>,
    {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::None(filter.into())),
        }
        .into()
    }

    fn is<T>(&self, filter: T) -> Filter
    where
        T: Into<Filter>,
    {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::Is(filter.into())),
        }
        .into()
    }

    fn is_not<T>(&self, filter: T) -> Filter
    where
        T: Into<Filter>,
    {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::IsNot(filter.into())),
        }
        .into()
    }

    fn is_empty(&self, b: bool) -> Filter {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::Empty(b)),
        }
        .into()
    }

    fn is_set(&self, b: bool) -> Filter {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::IsSet(b)),
        }
        .into()
    }

    fn equals(&self, val: PrismaValue) -> Filter {
        CompositeFilter {
            field: self.clone(),
            condition: Box::new(CompositeCondition::Equals(val)),
        }
        .into()
    }
}