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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use google_api_proto::google::firestore::v1::structured_query::{self, field_filter, unary_filter};

use crate::error::Result;
use crate::{Filter, IntoValue, Order};

/// A Firestore Field Path.
///
/// <https://firebase.google.com/docs/firestore/quotas#collections_documents_and_fields>
///
/// > - Must separate field names with a single period (`.`)
/// > - May be passed as a dot-delimited (`.`) string of segments where each segment is either a simple field name or a quoted field name (defined below).
/// >
/// > A simple field name is one where all of the following are true:
/// >
/// > - Contains only the characters `a-z`, `A-Z`, `0-9`, and underscore (`_`)
/// > - Does not start with `0-9`
/// >
/// > A quoted field name starts and ends with the backtick character (`` ` ``). For example, `` foo.`x&y` `` refers to the `x&y` field nested under the `foo` field. To construct a field name with the backtick character, escape the backtick character with the backslash character (`\`). For convenience, you can avoid quoted field names by passing the field path as a FieldPath object (for example, see JavaScript FieldPath).
///
/// # Examples
///
/// ```rust
/// use firestore_structured_query::FieldPath;
/// use google_api_proto::google::firestore::v1::structured_query;
///
/// let field_path1 = FieldPath::raw("field1");
/// assert_eq!(
///     structured_query::FieldReference::from(field_path1),
///     structured_query::FieldReference {
///         field_path: "field1".to_string(),
///     }
/// );
/// ```
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct FieldPath {
    field_names: Vec<String>,
}

impl FieldPath {
    /// Creates a new field path without escaping.
    pub fn raw<S>(field_path: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            field_names: vec![field_path.into()],
        }
    }
}

// for Filter
impl FieldPath {
    pub fn array_contains<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::ArrayContains, value)
    }

    pub fn array_contains_any<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(
            self.clone(),
            field_filter::Operator::ArrayContainsAny,
            value,
        )
    }

    pub fn equal<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::Equal, value)
    }

    pub fn greater_than<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::GreaterThan, value)
    }

    pub fn greater_than_or_equal<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(
            self.clone(),
            field_filter::Operator::GreaterThanOrEqual,
            value,
        )
    }

    pub fn r#in<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::In, value)
    }

    pub fn is_nan(&self) -> Result<Filter> {
        Filter::unary(self.clone(), unary_filter::Operator::IsNan)
    }

    pub fn is_not_nan(&self) -> Result<Filter> {
        Filter::unary(self.clone(), unary_filter::Operator::IsNotNan)
    }

    pub fn is_not_null(&self) -> Result<Filter> {
        Filter::unary(self.clone(), unary_filter::Operator::IsNotNull)
    }

    pub fn is_null(&self) -> Result<Filter> {
        Filter::unary(self.clone(), unary_filter::Operator::IsNull)
    }

    pub fn less_than<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::LessThan, value)
    }

    pub fn less_than_or_equal<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::LessThanOrEqual, value)
    }

    pub fn not_equal<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::NotEqual, value)
    }

    pub fn not_in<T>(&self, value: T) -> Result<Filter>
    where
        T: IntoValue,
    {
        Filter::field(self.clone(), field_filter::Operator::NotIn, value)
    }
}

// for Order
impl FieldPath {
    pub fn ascending(&self) -> Order {
        Order::new(self.clone(), structured_query::Direction::Ascending)
    }

    pub fn descending(&self) -> Order {
        Order::new(self.clone(), structured_query::Direction::Descending)
    }
}

impl std::convert::From<FieldPath> for structured_query::FieldReference {
    fn from(field_path: FieldPath) -> Self {
        structured_query::FieldReference {
            field_path: field_path.field_names.join("."),
        }
    }
}