json_predicate/predicate/first_order/
mod.rs

1use crate::Predicate;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5use self::{
6    contains::Contains, defined::Defined, end::End, less::Less, matches::Matches, more::More,
7    r#in::In, r#type::Type, start::Start, test::Test, undefined::Undefined,
8};
9
10use super::{context::PredicateContext, error::PredicateError, PredicateImpl};
11
12pub mod contains;
13pub mod defined;
14pub mod end;
15pub mod r#in;
16pub mod less;
17pub mod matches;
18pub mod more;
19pub mod start;
20pub mod test;
21pub mod r#type;
22pub mod undefined;
23
24#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
25#[serde(untagged)]
26pub enum FirstOrder {
27    Contains(Contains),
28    Defined(Defined),
29    Undefined(Undefined),
30    Start(Start),
31    End(End),
32    Type(Type),
33    In(In),
34    Test(Test),
35    Matches(Matches),
36    Less(Less),
37    More(More),
38    // Contained,
39    // Intersects,
40}
41
42impl PredicateImpl for FirstOrder {
43    fn evaluate(&self, data: &Value, ctx: PredicateContext) -> Result<bool, PredicateError> {
44        match self {
45            FirstOrder::Contains(contains) => contains.evaluate(data, ctx),
46            FirstOrder::Defined(defined) => defined.evaluate(data, ctx),
47            FirstOrder::Undefined(undefined) => undefined.evaluate(data, ctx),
48            FirstOrder::Start(start) => start.evaluate(data, ctx),
49            FirstOrder::End(end) => end.evaluate(data, ctx),
50            FirstOrder::Type(ty) => ty.evaluate(data, ctx),
51            FirstOrder::In(ty) => ty.evaluate(data, ctx),
52            FirstOrder::Test(ty) => ty.evaluate(data, ctx),
53            FirstOrder::Matches(ty) => ty.evaluate(data, ctx),
54            FirstOrder::Less(ty) => ty.evaluate(data, ctx),
55            FirstOrder::More(ty) => ty.evaluate(data, ctx),
56        }
57    }
58}
59
60impl From<FirstOrder> for Predicate {
61    fn from(value: FirstOrder) -> Self {
62        Predicate::FirstOrder(value)
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use serde::Deserialize;
69
70    use crate::predicate::first_order::FirstOrder;
71
72    #[test]
73    fn deser_test() {
74        let contains_expect = serde_json::json!({
75             "op": "contains",
76             "path": "/a/b",
77             "value": " is a "
78        });
79
80        let deser = FirstOrder::deserialize(contains_expect).unwrap();
81
82        insta::assert_debug_snapshot!(deser, @r###"
83        Contains(
84            Contains {
85                path: Some(
86                    JSONPath(
87                        JsonPointer {
88                            ref_toks: [
89                                "a",
90                                "b",
91                            ],
92                            _phantom: PhantomData<alloc::string::String>,
93                        },
94                    ),
95                ),
96                ignore_case: false,
97                value: String(
98                    " is a ",
99                ),
100            },
101        )
102        "###);
103    }
104}