hemoglobin-search 0.1.1

Hemoglobin search utilities
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use chumsky::{
    IterParser, Parser,
    error::Rich,
    extra,
    prelude::{any, choice, end, just, recursive},
    text::ident,
};
use hemoglobin::{
    cards::{
        kins::{Kin, KinComparison},
        properties::{Array, Number, Text},
    },
    numbers::Comparison,
};
use regex::Regex;

use super::{Errors, Ordering, Query, QueryRestriction, Sort, TextComparison};

/// # Errors
/// When `str` is not a valid property query name
pub fn get_property_from_name(str: &str) -> Result<Properties, Errors> {
    match str {
        "id" => Ok(Properties::StringProperty(Text::Id)),
        "name" | "n" => Ok(Properties::StringProperty(Text::Name)),
        "flavortext" | "flavor" | "ft" => Ok(Properties::StringProperty(Text::FlavorText)),
        "description" | "desc" | "de" => Ok(Properties::StringProperty(Text::Description)),
        "type" | "t" => Ok(Properties::StringProperty(Text::Type)),
        "cost" | "c" => Ok(Properties::NumProperty(Number::Cost)),
        "health" | "h" | "hp" => Ok(Properties::NumProperty(Number::Health)),
        "power" | "strength" | "damage" | "p" | "dmg" | "str" => {
            Ok(Properties::NumProperty(Number::Power))
        }
        "defense" | "defence" | "def" | "d" => Ok(Properties::NumProperty(Number::Defense)),
        "kin" | "k" => Ok(Properties::Kin),
        "function" | "fun" | "fn" | "f" => Ok(Properties::ArrayProperty(Array::Functions)),
        "keyword" | "kw" => Ok(Properties::Keywords),
        "sort" | "so" => Ok(Properties::Sort(Ordering::Ascending)),
        "sortd" | "sod" => Ok(Properties::Sort(Ordering::Descending)),
        _ => Err(Errors::UnknownStringParam(str.to_owned())),
    }
}

#[derive(Clone, Copy)]
pub enum Properties {
    NumProperty(Number),
    StringProperty(Text),
    ArrayProperty(Array),
    Sort(Ordering),
    Kin,
    Keywords,
}

enum TextComparable {
    String(String),
    Regex(Regex),
}

/// Parses a query.
/// # Errors
/// If the parser fails.
pub fn parse_query(string: &str) -> Result<Query, Vec<Rich<'_, char>>> {
    let parser = make_query_parser();
    parser.parse(string).into_result()
}

#[allow(clippy::too_many_lines)]
pub fn make_query_parser<'a>() -> impl Parser<'a, &'a str, Query, extra::Err<Rich<'a, char>>> + 'a {
    let word = any()
        .filter(|c: &char| !c.is_whitespace())
        .labelled("not whitespace")
        .repeated()
        .at_least(1)
        .collect::<String>()
        .labelled("ident");

    let quoted_word = |delim: char| {
        any()
            .filter(move |c: &char| *c != delim)
            .repeated()
            .collect::<String>()
            .labelled(format!("string wrapped in {delim}"))
    };

    let keyword = |mat: &'static str| {
        ident()
            .try_map(move |kw, span| {
                if mat == kw {
                    Ok(())
                } else {
                    Err(Rich::custom(span, format!("Expected {kw}")))
                }
            })
            .labelled(format!("`{mat}`"))
    };

    let name_property_name = choice((keyword("name"), keyword("n"))).to(Text::Name);
    let desc_property_name =
        choice((keyword("description"), keyword("desc"), keyword("d"))).to(Text::Description);
    let flavor_property_name =
        choice((keyword("flavortext"), keyword("flavor"), keyword("ft"))).to(Text::FlavorText);
    let id_property_name = keyword("id").to(Text::Id);
    let type_property_name = choice((keyword("type"), keyword("t"))).to(Text::Type);

    let text_property_name = choice((
        name_property_name,
        desc_property_name,
        flavor_property_name,
        id_property_name,
        type_property_name,
    ))
    .padded();

    let cost_property_name = choice((keyword("cost"), keyword("c"))).to(Number::Cost);
    let flip_cost_property_name = choice((keyword("flip"), keyword("f"))).to(Number::FlipCost);
    let power_property_name = choice((keyword("power"), keyword("p"))).to(Number::Power);
    let def_property_name =
        choice((keyword("defense"), keyword("def"), keyword("d"))).to(Number::Defense);
    let health_property_name =
        choice((keyword("health"), keyword("hp"), keyword("h"))).to(Number::Health);

    let num_property_name = choice((
        cost_property_name,
        flip_cost_property_name,
        power_property_name,
        def_property_name,
        health_property_name,
    ));

    let number = any()
        .filter(|c: &char| c.is_numeric())
        .repeated()
        .at_least(1)
        .collect::<String>()
        .try_map(|x, span| {
            x.parse()
                .map_err(|x| Rich::custom(span, format!("Not a number: {x}")))
        })
        .labelled("number");

    let regex_text = quoted_word('/')
        .try_map(|x, span| {
            Regex::new(x.as_str())
                .map_err(|x| Rich::custom(span, format!("Not a valid regex: {x}")))
        })
        .delimited_by(just('/'), just('/'))
        .labelled("regex expression");

    let quoted_text = quoted_word('"')
        .delimited_by(just('"'), just('"'))
        .labelled("quoted text");

    let expr = recursive(|expr| {
        let group = expr
            .clone()
            .repeated()
            .collect()
            .delimited_by(just('(').padded(), just(')').padded())
            .labelled("subquery");

        let group_restriction = group
            .clone()
            .map(|x| QueryRestriction::Group(query_from_restrictions(x)));

        let group_query = group.clone().map(query_from_restrictions);

        // Num Properties
        let num_comparison_symbol = choice((
            just("!=").to(NumberComparisonSymbol::NotEqual),
            just(">=").to(NumberComparisonSymbol::GreaterThanOrEqual),
            just("<=").to(NumberComparisonSymbol::LessThanOrEqual),
            just('>').to(NumberComparisonSymbol::GreaterThan),
            just('<').to(NumberComparisonSymbol::LessThan),
            just('=').to(NumberComparisonSymbol::Equal),
        ))
        .labelled("comparison operator");

        let num_comparison = num_comparison_symbol
            .padded()
            .then(number.padded())
            .map(|(comparison, number)| match comparison {
                NumberComparisonSymbol::GreaterThan => Comparison::GreaterThan(number),
                NumberComparisonSymbol::LessThan => Comparison::LowerThan(number),
                NumberComparisonSymbol::GreaterThanOrEqual => {
                    Comparison::GreaterThanOrEqual(number)
                }
                NumberComparisonSymbol::LessThanOrEqual => Comparison::LowerThanOrEqual(number),
                NumberComparisonSymbol::Equal => Comparison::Equal(number),
                NumberComparisonSymbol::NotEqual => Comparison::NotEqual(number),
            })
            .padded();

        let num_property = num_property_name
            .clone()
            .padded()
            .then(num_comparison)
            .map(|(property, cost)| QueryRestriction::NumberComparison(property, cost));

        // Text Properties
        let text_comparison_symbol = choice((
            just('=').to(TextComparisonSymbol::Equals),
            just(':').to(TextComparisonSymbol::Contains),
        ))
        .padded();

        let text_comparable = choice((
            quoted_text.map(TextComparable::String),
            regex_text.map(TextComparable::Regex),
            word.map(TextComparable::String),
        ))
        .padded();

        let text_comparison = text_comparison_symbol
            .then(text_comparable)
            .map(|(symbol, text)| match text {
                TextComparable::String(string) => match symbol {
                    TextComparisonSymbol::Contains => TextComparison::Contains(string),
                    TextComparisonSymbol::Equals => TextComparison::EqualTo(string),
                },
                TextComparable::Regex(regex) => TextComparison::HasMatch(regex),
            })
            .padded();

        let text_property = text_property_name
            .clone()
            .then(text_comparison.clone())
            .map(|(property, comparison)| QueryRestriction::TextComparison(property, comparison))
            .padded();

        // Kins
        let kin_property_name = choice((keyword("kins"), keyword("k"))).to(Properties::Kin);

        let kin_comparison = text_comparison.clone().map(|x| match x {
            TextComparison::Contains(string) => match Kin::from_string(&string) {
                Some(kin) => KinComparison::Similar(kin),
                None => KinComparison::TextContains(string),
            },
            TextComparison::EqualTo(string) => match Kin::from_string(&string) {
                Some(kin) => KinComparison::Equal(kin),
                None => KinComparison::TextEqual(string),
            },
            TextComparison::HasMatch(regex) => KinComparison::RegexMatch(regex),
        });

        let kin_property = kin_property_name
            .padded()
            .ignore_then(kin_comparison.clone())
            .map(QueryRestriction::KinComparison);

        // Keywords
        let kws_property_name = choice((keyword("keyword"), keyword("kw")));

        let kw_property = kws_property_name
            .ignore_then(text_comparison)
            .map(QueryRestriction::HasKw);

        // Devours
        let devours_property_name = choice((keyword("devours"), keyword("dev"))).to(Text::Name);

        let null_comparison_symbol = choice((just('=').to(()), just(':').to(()))).padded();

        let devours_property = devours_property_name
            .ignore_then(null_comparison_symbol)
            .ignore_then(group_query.clone())
            .map(QueryRestriction::Devours)
            .padded();

        // Devoured by
        let devouredby_property_name =
            choice((keyword("devouredby"), keyword("deby"), keyword("dby"))).to(Text::Name);

        let devouredby_property = devouredby_property_name
            .ignore_then(null_comparison_symbol)
            .ignore_then(group_query.clone())
            .map(QueryRestriction::DevouredBy)
            .padded();

        // Fuzzy
        let fuzzy = word
            .filter(|x| x != "XOR" && x != "OR" && x != "SORT")
            .map(QueryRestriction::Fuzzy)
            .labelled("basic query word");

        // Atom
        let atom = choice((
            num_property,
            text_property,
            devours_property,
            devouredby_property,
            kin_property,
            kw_property,
            fuzzy,
        ))
        .padded();

        let atom = atom.or(group_restriction.clone());

        let uniop = choice((
            just('-').to(QueryOp::Not),
            just('!').to(QueryOp::LenientNot),
        ));

        let atom = uniop
            .padded()
            .repeated()
            .foldr(atom, |op, atom| match op {
                QueryOp::Not => QueryRestriction::Not(query_from_restrictions(vec![atom])),
                QueryOp::LenientNot => {
                    QueryRestriction::LenientNot(query_from_restrictions(vec![atom]))
                }
            })
            .labelled("search atom");

        let operation = choice((
            keyword("OR").to(QueryBinOp::Or),
            keyword("XOR").to(QueryBinOp::Xor),
        ));

        atom.then(operation.then(expr).or_not()).map(
            |(first, op): (QueryRestriction, Option<(QueryBinOp, QueryRestriction)>)| match op {
                None => first,
                Some((op, right)) => match op {
                    QueryBinOp::Or => QueryRestriction::Or(
                        query_from_restrictions(vec![first]),
                        query_from_restrictions(vec![right]),
                    ),
                    QueryBinOp::Xor => QueryRestriction::Xor(
                        query_from_restrictions(vec![first]),
                        query_from_restrictions(vec![right]),
                    ),
                },
            },
        )
    });

    let order = choice((
        keyword("ascending").to(Ordering::Ascending),
        keyword("descending").to(Ordering::Descending),
    ))
    .labelled("ascending or descending");

    let sort_type = choice((
        text_property_name.map(Sortable::Text),
        num_property_name.map(Sortable::Num),
    ))
    .labelled("sortable trait");

    let order =
        sort_type
            .padded()
            .then(order)
            .map(|(sort, order): (Sortable, Ordering)| match sort {
                Sortable::Text(text) => Sort::Alphabet(text, order),
                Sortable::Num(number) => Sort::Numeric(number, order),
            });

    let sort = keyword("SORT")
        .ignore_then(order)
        .labelled("sorting method")
        .or_not()
        .labelled("sorting clause or lack thereof")
        .map(|x| x.map_or(Sort::Fuzzy, |x| x));

    expr.padded()
        .repeated()
        .collect()
        .map(query_from_restrictions)
        .then(sort.padded())
        .map(|(mut query, sort)| {
            query.sort = sort;
            query
        })
        .then_ignore(end())
}

fn query_from_restrictions(restrictions: Vec<QueryRestriction>) -> Query {
    let mut name = String::new();

    for restriction in &restrictions {
        if let QueryRestriction::Fuzzy(a) = restriction {
            name += a;
            name += " ";
        }
    }

    let sort = if name.is_empty() {
        Sort::Alphabet(Text::Name, Ordering::Ascending)
    } else {
        Sort::Fuzzy
    };

    Query {
        name: name.trim().to_string(),
        restrictions,
        sort,
    }
}

#[derive(Clone)]
enum TextComparisonSymbol {
    Contains,
    Equals,
}

#[derive(Clone)]
enum NumberComparisonSymbol {
    GreaterThan,
    LessThan,
    GreaterThanOrEqual,
    LessThanOrEqual,
    Equal,
    NotEqual,
}

#[derive(Clone)]
enum QueryOp {
    Not,
    LenientNot,
}

#[derive(Clone)]
enum QueryBinOp {
    Or,
    Xor,
}

enum Sortable {
    Text(Text),
    Num(Number),
}