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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#[derive(Debug, PartialEq)]
pub enum SearchParseError {
    Kind(String),
    Term(String),
}

impl std::fmt::Display for SearchParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            SearchParseError::Kind(s) => write!(f, "Invalid search kind: {}", s),
            SearchParseError::Term(s) => write!(f, "Invalid search term: {}", s),
        }
    }
}

impl std::error::Error for SearchParseError {}

/// Search for a field that cannot be null
///
/// Use a `NullableSearch<T>` when a field could be null instead of `Search<Option<T>>`
#[derive(Debug, PartialEq)]
pub enum Search<T> {
    /// Field partially matches
    Partial(T),

    /// Field fully matches
    Exact(T),

    /// Do not search by this field
    NoSearch,
}

impl<T: std::str::FromStr> Search<T> {
    pub fn from_query(query: &str) -> Result<Search<T>, SearchParseError> {
        let mut query_iter = query.split(',');

        let kind = query_iter.next().map(|s| s.trim());
        let term = query_iter.next().map(|s| s.trim());

        match (kind, term) {
            (Some("partial"), Some(s)) => s
                .trim()
                .parse()
                .map(|p| Search::Partial(p))
                .map_err(|_| SearchParseError::Term(s.to_owned())),
            (Some("exact"), Some(s)) => s
                .trim()
                .parse()
                .map(|p| Search::Exact(p))
                .map_err(|_| SearchParseError::Term(s.to_owned())),
            (Some("partial"), None) => Err(SearchParseError::Term("".to_owned())),
            (Some("exact"), None) => Err(SearchParseError::Term("".to_owned())),
            (Some(k), Some(_)) => Err(SearchParseError::Kind(k.to_owned())),
            (Some(k), None) => Err(SearchParseError::Kind(k.to_owned())),
            (None, Some(_)) => Err(SearchParseError::Kind("".to_owned())),
            (None, None) => Err(SearchParseError::Kind("".to_owned())),
        }
    }
}

#[test]
fn parse_search_partial_search_works() {
    let s = Search::from_query(" partial , hello ");
    assert_eq!(s, Ok(Search::Partial("hello".to_owned())));
}

#[test]
fn parse_search_exact_search_works() {
    let s = Search::from_query(" exact, hello ");
    assert_eq!(s, Ok(Search::Exact("hello".to_owned())));
}

#[test]
fn parse_search_invalid_kind_with_term_fails() {
    let s: Result<Search<String>, _> = Search::from_query("hello, bye");
    assert_eq!(s, Err(SearchParseError::Kind("hello".to_owned())));
}

#[test]
fn parse_search_no_kind_with_term_fails() {
    let s: Result<Search<String>, _> = Search::from_query(", bye");
    assert_eq!(s, Err(SearchParseError::Kind("".to_owned())));
}

#[test]
fn parse_search_partial_with_no_term_fails() {
    let s: Result<Search<String>, _> = Search::from_query(" partial");
    assert_eq!(s, Err(SearchParseError::Term("".to_owned())));
}

#[test]
fn parse_search_exact_with_no_term_fails() {
    let s: Result<Search<String>, _> = Search::from_query(" exact");
    assert_eq!(s, Err(SearchParseError::Term("".to_owned())));
}

#[test]
fn parse_search_invalid_with_no_term_fails() {
    let s: Result<Search<String>, _> = Search::from_query("hello");
    assert_eq!(s, Err(SearchParseError::Kind("hello".to_owned())));
}

#[test]
fn parse_search_empty_string_fails() {
    let s: Result<Search<String>, _> = Search::from_query("");
    assert_eq!(s, Err(SearchParseError::Kind("".to_owned())));
}

/// Search fo a field that can be null
///
/// This could be done as a `Search<Option>`, but then the
/// cases for Any and None are not entirely clear.
///
/// For example, this would make some sense:
///
/// `PartialSearch(Some(t))` -> Field is not null and partially matches (`PartialSearch(T)`)
/// `ExactSearch(Some(t))` -> Field is not null and fully matches (`ExactSearch(T)`)
/// `PartialSearch(None)` -> Field is not null (`Some`)
/// `ExactSearch(None)` -> Field is null (`None`)
/// `NoSearch` -> Do not search by this field (`NoSearch`)
///
/// but it is not immediatly clear what the `PartialSearch(None)` and `ExactSearch(None)`
/// correspond to, and could be confusing and subjective. Instead, we use this enum.
///
#[derive(Debug, PartialEq)]
pub enum NullableSearch<T> {
    /// Field is not null and partially matches
    Partial(T),

    /// Field is not null and exactly matches
    Exact(T),

    /// Field is not null
    /// (`Some` matches Rust terminology better than `NonNull` or similar)
    Some,

    /// Field is null
    /// (`None` matches Rust terminology better than `Null` or similar)
    None,

    /// Do not search by this field
    NoSearch,
}

impl<T: std::str::FromStr> NullableSearch<T> {
    pub fn from_query(query: &str) -> Result<NullableSearch<T>, SearchParseError> {
        let mut query_iter = query.split(',');

        let kind = query_iter.next().map(|s| s.trim());
        let term = query_iter.next().map(|s| s.trim());

        match (kind, term) {
            (Some("partial"), Some(s)) => s
                .trim()
                .parse()
                .map(|p| NullableSearch::Partial(p))
                .map_err(|_| SearchParseError::Term(s.to_owned())),
            (Some("exact"), Some(s)) => s
                .trim()
                .parse()
                .map(|p| NullableSearch::Exact(p))
                .map_err(|_| SearchParseError::Term(s.to_owned())),

            (Some("some"), None) => Ok(NullableSearch::Some),
            (Some("none"), None) => Ok(NullableSearch::None),

            (Some("partial"), None) => Err(SearchParseError::Term("".to_owned())),
            (Some("exact"), None) => Err(SearchParseError::Term("".to_owned())),

            (Some("some"), Some(s)) => Err(SearchParseError::Term(s.to_owned())),
            (Some("none"), Some(s)) => Err(SearchParseError::Term(s.to_owned())),

            (Some(k), Some(_)) => Err(SearchParseError::Kind(k.to_owned())),
            (Some(k), None) => Err(SearchParseError::Kind(k.to_owned())),

            (None, Some(_)) => Err(SearchParseError::Kind("".to_owned())),
            (None, None) => Err(SearchParseError::Kind("".to_owned())),
        }
    }
}

#[test]
fn parse_nullable_search_partial_search_works() {
    let s = NullableSearch::from_query(" partial , hello ");
    assert_eq!(s, Ok(NullableSearch::Partial("hello".to_owned())));
}

#[test]
fn parse_nullable_search_exact_search_works() {
    let s = NullableSearch::from_query(" exact, hello ");
    assert_eq!(s, Ok(NullableSearch::Exact("hello".to_owned())));
}

#[test]
fn parse_nullable_search_some_works() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query(" some ");
    assert_eq!(s, Ok(NullableSearch::Some));
}

#[test]
fn parse_nullable_search_none_works() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query(" none ");
    assert_eq!(s, Ok(NullableSearch::None));
}

#[test]
fn parse_nullable_search_some_with_term_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query(" some, hello");
    assert_eq!(s, Err(SearchParseError::Term("hello".to_owned())));
}

#[test]
fn parse_nullable_search_none_with_term_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query(" none, hello");
    assert_eq!(s, Err(SearchParseError::Term("hello".to_owned())));
}

#[test]
fn parse_nullable_search_invalid_kind_with_term_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query("hello, bye");
    assert_eq!(s, Err(SearchParseError::Kind("hello".to_owned())));
}

#[test]
fn parse_nullable_search_no_kind_with_term_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query(", bye");
    assert_eq!(s, Err(SearchParseError::Kind("".to_owned())));
}

#[test]
fn parse_nullable_search_partial_with_no_term_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query(" partial");
    assert_eq!(s, Err(SearchParseError::Term("".to_owned())));
}

#[test]
fn parse_nullable_search_exact_with_no_term_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query(" exact");
    assert_eq!(s, Err(SearchParseError::Term("".to_owned())));
}

#[test]
fn parse_nullable_search_invalid_with_no_term_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query("hello");
    assert_eq!(s, Err(SearchParseError::Kind("hello".to_owned())));
}

#[test]
fn parse_nullable_search_empty_string_fails() {
    let s: Result<NullableSearch<String>, _> = NullableSearch::from_query("");
    assert_eq!(s, Err(SearchParseError::Kind("".to_owned())));
}