goat_cli/utils/
expression.rs

1use crate::utils::tax_ranks::TaxRanks;
2use crate::utils::utils::did_you_mean;
3use crate::utils::variable_data::GOAT_VARIABLE_SYNONYMS;
4
5use crate::error::{Error, ErrorKind, Result};
6use regex::Regex;
7use std::collections::{BTreeMap, HashSet};
8use std::fmt;
9use std::sync::LazyLock;
10use tabled::{object::Rows, Modify, Panel, Table, Tabled, Width};
11
12/// Summary functions that can wrap a field in an expression, e.g.
13/// `max(genome_size)`. Mirrors the API's own list (`functions/summaries.js`),
14/// minus its internal `metadata` and `hexbin*` summaries.
15pub const VALID_EXPRESSION_FUNCTIONS: &[&str] =
16    &["min", "max", "count", "length", "sp_count", "range", "value"];
17
18/// Functions which return a count, whatever the type of the field.
19const COUNT_FUNCTIONS: &[&str] = &["count", "length", "sp_count"];
20
21/// Aggregation subset specifiers that may follow a field name with a colon.
22const VALID_SUBSETS: &[&str] = &["direct", "ancestor", "descendant", "estimate"];
23
24/// The type checked against the value of a count function.
25static COUNT_TYPE: TypeOf<'static> = TypeOf::Long;
26
27/// The API splits on `or`, then `and`, case-insensitively and only where
28/// surrounded by whitespace (so values such as `island` are safe).
29static OR_SPLIT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)\s+or\s+").unwrap());
30static AND_SPLIT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)\s+and\s+").unwrap());
31/// `<lhs> <operator> <rhs>`. Two character operators must come first.
32static CLAUSE: LazyLock<Regex> = LazyLock::new(|| {
33    Regex::new(r"^(?P<lhs>[^!<>=]+?)\s*(?P<op>!=|<=|>=|==|<|>|=)\s*(?P<rhs>.*)$").unwrap()
34});
35/// A summary function wrapping a field, e.g. `max(genome_size:direct)`.
36static FUNCTION: LazyLock<Regex> =
37    LazyLock::new(|| Regex::new(r"^(?P<func>[a-z_]+)\(\s*(?P<field>[^()]*?)\s*\)$").unwrap());
38/// A number with a size suffix, e.g. `1G`, which the API does not accept.
39static NUMBER_WITH_SUFFIX: LazyLock<Regex> =
40    LazyLock::new(|| Regex::new(r"(?i)^-?\d+(\.\d+)?\s*[kmgtp]b?$").unwrap());
41
42fn expression_error(message: String) -> Error {
43    Error::new(ErrorKind::Expression(message))
44}
45
46/// Serialize GoaT variables into their types.
47///
48/// See [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/number.html)
49/// for more details.
50#[derive(Tabled)]
51pub enum TypeOf<'a> {
52    /// Signed 64 bit int.
53    Long,
54    /// Signed 16 bit int.
55    Short,
56    /// Float with one decimal place.
57    OneDP,
58    /// Float with two decimal places.
59    TwoDP,
60    /// Signed 32 bit int.
61    Integer,
62    /// A date.
63    Date,
64    /// Half precision 16 bit float.
65    HalfFloat,
66    /// A keyword. If the list of values is not empty, the API rejects
67    /// any other value.
68    Keyword(Vec<&'a str>),
69    /// A keyword with a list of known values, which the API does not
70    /// enforce; other values are allowed but may match nothing.
71    KeywordSuggest(Vec<&'a str>),
72    /// Not usable in expressions (e.g. `geo_point`), so not checked.
73    None,
74}
75
76impl<'a> TypeOf<'a> {
77    /// Check the right hand side of a clause, so `goat-cli` displays
78    /// meaningful help before the query is sent.
79    ///
80    /// `rhs` may be a comma separated list, each value may be negated with a
81    /// leading `!`, and `null` is allowed for any type.
82    fn check(&self, rhs: &str, variable: &str) -> Result<()> {
83        for value in rhs.split(',') {
84            let value = value.trim().trim_start_matches('!').trim();
85            if value.is_empty() {
86                return Err(expression_error(format!(
87                    "missing value for \"{variable}\" in \"{rhs}\"."
88                )));
89            }
90            if value.eq_ignore_ascii_case("null") {
91                continue;
92            }
93            match self {
94                TypeOf::Long
95                | TypeOf::Short
96                | TypeOf::OneDP
97                | TypeOf::TwoDP
98                | TypeOf::Integer
99                | TypeOf::HalfFloat => check_number(value, variable)?,
100                TypeOf::Date => check_date(value, variable)?,
101                TypeOf::Keyword(allowed) if !allowed.is_empty() => {
102                    if let Some(suggestion) = unknown_keyword(value, allowed) {
103                        return Err(expression_error(format!(
104                            "\"{value}\" is not a valid value for \"{variable}\" - did you mean \"{suggestion}\"?"
105                        )));
106                    }
107                }
108                TypeOf::KeywordSuggest(known) => {
109                    if let Some(suggestion) = unknown_keyword(value, known) {
110                        eprintln!(
111                            "warning: \"{value}\" is not a known value for \"{variable}\" (did you mean \"{suggestion}\"?), so may match nothing."
112                        );
113                    }
114                }
115                TypeOf::Keyword(_) | TypeOf::None => (),
116            }
117        }
118        Ok(())
119    }
120}
121
122/// The API accepts anything JavaScript parses as a number, including
123/// scientific notation (`1e9`) and negative numbers.
124fn check_number(value: &str, variable: &str) -> Result<()> {
125    if value
126        .replace('−', "-")
127        .parse::<f64>()
128        .map_or(false, f64::is_finite)
129    {
130        return Ok(());
131    }
132    let hint = if NUMBER_WITH_SUFFIX.is_match(value) {
133        " Size suffixes are not supported; use scientific notation instead, e.g. 1e9."
134    } else {
135        ""
136    };
137    Err(expression_error(format!(
138        "for variable \"{variable}\", \"{value}\" is not a number.{hint}"
139    )))
140}
141
142/// Dates may be given as `yyyy`, `yyyy-mm` or `yyyy-mm-dd`.
143fn check_date(value: &str, variable: &str) -> Result<()> {
144    let digits = |s: &str, n: usize| s.len() == n && s.chars().all(|c| c.is_ascii_digit());
145    let in_range = |s: &str, max: u32| s.parse::<u32>().map_or(false, |v| (1..=max).contains(&v));
146    let valid = match value.split('-').collect::<Vec<_>>().as_slice() {
147        [y] => digits(y, 4),
148        [y, m] => digits(y, 4) && digits(m, 2) && in_range(m, 12),
149        [y, m, d] => {
150            digits(y, 4) && digits(m, 2) && in_range(m, 12) && digits(d, 2) && in_range(d, 31)
151        }
152        _ => false,
153    };
154    if valid {
155        Ok(())
156    } else {
157        Err(expression_error(format!(
158            "for variable \"{variable}\", \"{value}\" is not a date. Use yyyy, yyyy-mm or yyyy-mm-dd."
159        )))
160    }
161}
162
163/// If `value` is not (case-insensitively) one of `allowed`, return the
164/// closest match.
165fn unknown_keyword(value: &str, allowed: &[&str]) -> Option<String> {
166    if allowed.iter().any(|a| a.eq_ignore_ascii_case(value)) {
167        return None;
168    }
169    let possibilities = allowed.iter().map(|a| a.to_string()).collect::<Vec<_>>();
170    Some(did_you_mean(&possibilities, value).unwrap_or_default())
171}
172
173impl<'a> fmt::Display for TypeOf<'a> {
174    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175        match self {
176            // do nothing with None at the moment.
177            TypeOf::None => write!(f, "Please don't use yet! This variable needs fixing."),
178            TypeOf::Long
179            | TypeOf::Short
180            | TypeOf::OneDP
181            | TypeOf::TwoDP
182            | TypeOf::Integer
183            | TypeOf::Date
184            | TypeOf::HalfFloat => write!(f, "!=, <, <=, =, ==, >, >="),
185            TypeOf::Keyword(k) | TypeOf::KeywordSuggest(k) if k.is_empty() => write!(f, ""),
186            TypeOf::Keyword(k) | TypeOf::KeywordSuggest(k) => write!(f, "== {}", k.join(", ")),
187        }
188    }
189}
190
191/// Resolve a field name as the API does: case-insensitively, accepting
192/// synonyms (e.g. `ebp_metric_date`) and `-` in place of `_`.
193pub fn canonical_field(
194    name: &str,
195    reference_data: &BTreeMap<&'static str, Variable<'static>>,
196) -> Option<&'static str> {
197    let lookup = |n: &str| {
198        reference_data
199            .keys()
200            .find(|k| k.eq_ignore_ascii_case(n))
201            .copied()
202    };
203    lookup(name)
204        .or_else(|| lookup(&name.replace('-', "_")))
205        .or_else(|| {
206            GOAT_VARIABLE_SYNONYMS
207                .get(name.to_lowercase().as_str())
208                .and_then(|canonical| lookup(canonical))
209        })
210}
211
212/// Kind of an option alias. Does a
213/// particular variable have a function
214/// associated with it? Usually min/max.
215pub enum Function<'a> {
216    None,
217    Some(Vec<&'a str>),
218}
219
220impl<'a> fmt::Display for Function<'a> {
221    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
222        match self {
223            Function::None => write!(f, ""),
224            Function::Some(fun) => write!(f, "{}", fun.join(", ")),
225        }
226    }
227}
228
229/// The GoaT variable of interest.
230#[derive(Tabled)]
231pub struct Variable<'a> {
232    #[tabled(rename = "Display Name")]
233    pub display_name: &'a str,
234    #[tabled(rename = "Operators/Keywords")]
235    pub type_of: TypeOf<'a>,
236    #[tabled(rename = "Function(s)")]
237    pub functions: Function<'a>,
238}
239
240/// The column headers for `goat-cli search --print-expression`.
241#[derive(Tabled)]
242struct ColHeader(#[tabled(rename = "Expression Name")] &'static str);
243
244/// Print the table of GoaT variable data.
245pub fn print_variable_data(data: &BTreeMap<&'static str, Variable<'static>>) -> Result<()> {
246    // for some space
247    crate::outln!()?;
248    // map the header to a tuple combination
249    // see https://github.com/zhiburt/tabled/blob/master/README.md
250    let table_data = data
251        .iter()
252        .map(|(e, f)| (ColHeader(e), f))
253        .collect::<Vec<(ColHeader, &Variable)>>();
254    // add taxon ranks at end...
255    let footer_data = TaxRanks::init();
256
257    let table_string = Table::new(&table_data)
258        .with(Panel::footer(format!(
259            "NCBI taxon ranks:\n\n{}",
260            footer_data
261        )))
262        .with(Modify::new(Rows::new(1..table_data.len() - 1)).with(Width::wrap(30).keep_words()))
263        // 4 rows
264        .with(Modify::new(Rows::new(table_data.len()..)).with(Width::wrap(30 * 4).keep_words()))
265        .to_string();
266
267    crate::outln!("{}", table_string)?;
268    Ok(())
269}
270
271/// The CLI expression which needs to be parsed.
272///
273/// See `EXPRESSIONS.md` for the syntax.
274#[derive(Debug)]
275pub struct CLIexpression<'a> {
276    pub inner: &'a str,
277}
278
279impl<'a> CLIexpression<'a> {
280    /// Constructor for [`CLIexpression`].
281    pub fn new(string: &'a str) -> Self {
282        Self { inner: string }
283    }
284
285    /// Validate an expression and return it in canonical form, as
286    /// `" AND <clause> AND <clause> OR <clause> ..."`, ready to follow a
287    /// taxon term.
288    ///
289    /// As in the API, `OR` binds more loosely than `AND`, so the result is a
290    /// list of `AND` groups joined by `" OR "`. The taxon and rank must apply
291    /// to every group; [`crate::utils::url::make_goat_urls`] does that.
292    pub fn parse(
293        &mut self,
294        reference_data: &BTreeMap<&'static str, Variable<'static>>,
295        extra_fields: Option<&HashSet<String>>,
296    ) -> Result<String> {
297        let input = self.inner.trim();
298        if input.is_empty() {
299            return Err(expression_error("the expression is empty.".to_string()));
300        }
301        if input.contains("&&") {
302            return Err(expression_error(
303                "use the AND keyword, not &&, between clauses.".to_string(),
304            ));
305        }
306        if input.contains("||") {
307            return Err(expression_error(
308                "use the OR keyword, not ||, between clauses.".to_string(),
309            ));
310        }
311
312        let mut groups = Vec::new();
313        for group in OR_SPLIT.split(input) {
314            let clauses = AND_SPLIT
315                .split(strip_outer_parens(group.trim()))
316                .map(|clause| parse_clause(clause, reference_data, extra_fields))
317                .collect::<Result<Vec<_>>>()?;
318            groups.push(clauses.join(" AND "));
319        }
320        Ok(format!(" AND {}", groups.join(" OR ")))
321    }
322
323    /// Whether any clause names a field that `reference_data` does not know,
324    /// which is when the live field registry is worth loading. Unlike
325    /// [`CLIexpression::parse`], this prints no warnings.
326    pub fn has_unknown_field(
327        &self,
328        reference_data: &BTreeMap<&'static str, Variable<'static>>,
329    ) -> bool {
330        OR_SPLIT
331            .split(self.inner.trim())
332            .flat_map(|group| AND_SPLIT.split(strip_outer_parens(group.trim())))
333            .any(|clause| {
334                let clause = clause.replace(['"', '\''], "");
335                let clause = clause.trim();
336                let lhs = CLAUSE
337                    .captures(clause)
338                    .map_or(clause, |caps| caps.name("lhs").unwrap().as_str());
339                !lhs.trim().to_lowercase().starts_with("tax_")
340                    && resolve_lhs(lhs, reference_data, None).is_err()
341            })
342    }
343}
344
345/// Remove parentheses wrapping a whole `OR` branch, e.g. `(a AND b)`.
346fn strip_outer_parens(group: &str) -> &str {
347    let Some(inner) = group.strip_prefix('(').and_then(|g| g.strip_suffix(')')) else {
348        return group;
349    };
350    // make sure the first `(` closes at the very end, not e.g. `(a) AND (b)`
351    let mut depth = 0i32;
352    for c in inner.chars() {
353        match c {
354            '(' => depth += 1,
355            ')' => depth -= 1,
356            _ => (),
357        }
358        if depth < 0 {
359            return group;
360        }
361    }
362    inner.trim()
363}
364
365/// Validate a single clause, returning it in canonical form.
366fn parse_clause(
367    clause: &str,
368    reference_data: &BTreeMap<&'static str, Variable<'static>>,
369    extra_fields: Option<&HashSet<String>>,
370) -> Result<String> {
371    // quotes are not needed by the API, even for values containing spaces
372    let clause = clause.replace(['"', '\''], "");
373    let clause = clause.trim();
374    if clause.is_empty() {
375        return Err(expression_error(
376            "found an empty clause - check for a doubled or trailing AND/OR.".to_string(),
377        ));
378    }
379    if clause.matches('(').count() != clause.matches(')').count() {
380        return Err(expression_error(format!(
381            "unbalanced parentheses in \"{clause}\". Parentheses may only wrap a whole OR branch, as GoaT does not support nesting: write \"a AND (b OR c)\" as \"a AND b OR a AND c\"."
382        )));
383    }
384    let lower = clause.to_lowercase();
385    if lower.starts_with("tax_rank") {
386        return Err(expression_error(
387            "set tax_rank through --tax-rank <taxon_rank>.".to_string(),
388        ));
389    }
390    if lower.starts_with("tax_") {
391        return Err(expression_error("set tax_name through -t <taxon_name>, tax_tree by -d flag, and tax_lineage by -l flag.".to_string()));
392    }
393
394    let Some(caps) = CLAUSE.captures(clause) else {
395        // a bare field, which means "has a value"
396        if clause.contains(char::is_whitespace) {
397            return Err(expression_error(format!(
398                "\"{clause}\" is not a valid clause. Expected <field> <operator> <value>, e.g. \"long_list = dtol\", or a bare field name."
399            )));
400        }
401        let (lhs, _) = resolve_lhs(clause, reference_data, extra_fields)?;
402        return Ok(lhs);
403    };
404
405    let (lhs, type_of) = resolve_lhs(&caps["lhs"], reference_data, extra_fields)?;
406    let rhs = caps["rhs"].trim();
407    if rhs.is_empty() {
408        return Err(expression_error(format!("missing value in \"{clause}\".")));
409    }
410    if let Some(type_of) = type_of {
411        type_of.check(rhs, &lhs)?;
412    }
413    let values = rhs.split(',').map(str::trim).collect::<Vec<_>>().join(",");
414    Ok(format!("{} {} {}", lhs, &caps["op"], values))
415}
416
417/// Validate the left hand side of a clause: a field, optionally with a
418/// `:subset` and/or wrapped in a summary function. Returns the canonical
419/// form, and the type to check values against (`None` for fields only known
420/// from the live registry, and identifiers such as `taxon_id`).
421fn resolve_lhs<'d>(
422    lhs: &str,
423    reference_data: &'d BTreeMap<&'static str, Variable<'static>>,
424    extra_fields: Option<&HashSet<String>>,
425) -> Result<(String, Option<&'d TypeOf<'static>>)> {
426    let lhs = lhs.trim().to_lowercase();
427    let (function, field) = match FUNCTION.captures(&lhs) {
428        Some(caps) => (Some(caps["func"].to_string()), caps["field"].to_string()),
429        None => (None, lhs.clone()),
430    };
431    if let Some(f) = &function {
432        if !VALID_EXPRESSION_FUNCTIONS.contains(&f.as_str()) {
433            return Err(expression_error(format!(
434                "unknown function \"{f}\" in \"{lhs}\" - valid functions are: {}.",
435                VALID_EXPRESSION_FUNCTIONS.join(", ")
436            )));
437        }
438    }
439
440    let (name, subset) = match field.split_once(':') {
441        Some((name, subset)) => (name.trim(), Some(subset.trim())),
442        None => (field.as_str(), None),
443    };
444    if let Some(subset) = subset {
445        if !VALID_SUBSETS.contains(&subset) {
446            return Err(expression_error(format!(
447                "unknown field subset \":{}\" - valid subsets are: {}",
448                subset,
449                VALID_SUBSETS.join(", ")
450            )));
451        }
452    }
453    if name.is_empty() || name.contains(|c: char| c.is_whitespace() || c == '(' || c == ')') {
454        return Err(expression_error(format!(
455            "\"{lhs}\" is not a valid field name."
456        )));
457    }
458
459    let is_dynamic = extra_fields.map_or(false, |fields| fields.contains(name));
460    let (name, type_of) = match canonical_field(name, reference_data) {
461        Some(canonical) => (canonical.to_string(), Some(&reference_data[canonical].type_of)),
462        // identifiers are matched by the API, e.g. taxon_id = 9606
463        None if is_dynamic || name.ends_with("_id") => (name.to_string(), None),
464        None => {
465            let possibilities = reference_data
466                .keys()
467                .map(|k| k.to_string())
468                .chain(GOAT_VARIABLE_SYNONYMS.keys().map(|k| k.to_string()))
469                .collect::<Vec<_>>();
470            let hint = did_you_mean(&possibilities, name)
471                .map(|s| format!(" - did you mean \"{s}\"?"))
472                .unwrap_or_default();
473            return Err(expression_error(format!(
474                "unknown variable \"{name}\"{hint}"
475            )));
476        }
477    };
478
479    let type_of = match function.as_deref() {
480        Some(f) if COUNT_FUNCTIONS.contains(&f) => Some(&COUNT_TYPE),
481        _ => type_of,
482    };
483    let field = match subset {
484        Some(subset) => format!("{name}:{subset}"),
485        None => name,
486    };
487    let lhs = match function {
488        Some(f) => format!("{f}({field})"),
489        None => field,
490    };
491    Ok((lhs, type_of))
492}
493
494#[cfg(test)]
495mod tests {
496    use crate::utils::variable_data::GOAT_TAXON_VARIABLE_DATA;
497
498    use super::*;
499
500    // The tests need to be able to parse:
501    // bioproject=!PRJEB40665 AND long_list=dtol AND ebp_metric_date AND tax_rank(species)
502    // bioproject%3D!PRJEB40665%20AND%20long_list%3Ddtol%20AND%20ebp_metric_date%20AND%20tax_rank%28species%29
503    #[test]
504    fn test_1() {
505        let expression =
506            "bioproject=!PRJEB40665 AND long_list=dtol AND ebp_metric_date AND tax_rank(species)";
507        let mut cli_exp = CLIexpression::new(expression);
508        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
509
510        assert!(result.is_err())
511    }
512
513    #[test]
514    fn test_1_1() {
515        // =! is not a valid operator; `!` is a value-negation prefix.
516        // bioproject=!PRJEB40665 → operator `=`, value `!PRJEB40665`.
517        let expression =
518            "bioproject=!PRJEB40665 AND long_list=dtol AND ebp_metric_date AND genome_size > 1000";
519        let mut cli_exp = CLIexpression::new(expression);
520        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
521
522        // ebp_metric_date is a synonym of ebp_standard_date
523        assert_eq!(
524            result.unwrap(),
525            " AND bioproject = !PRJEB40665 AND long_list = dtol AND ebp_standard_date AND genome_size > 1000"
526        );
527    }
528
529    #[test]
530    fn test_2() {
531        let expression = "long_list=dtol AND length(long_list)>1";
532        let mut cli_exp = CLIexpression::new(expression);
533        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
534        assert_eq!(
535            result.unwrap(),
536            " AND long_list = dtol AND length(long_list) > 1"
537        );
538    }
539
540    #[test]
541    fn test_3() {
542        let expression = "long_list=dtol AND sequencing_status";
543        let mut cli_exp = CLIexpression::new(expression);
544        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
545        assert_eq!(result.unwrap(), " AND long_list = dtol AND sequencing_status");
546    }
547
548    #[test]
549    fn test_4() {
550        let expression = "genome_size > 1000";
551        let mut cli_exp = CLIexpression::new(expression);
552        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
553        assert_eq!(result.unwrap(), " AND genome_size > 1000");
554    }
555
556    #[test]
557    fn test_4_1() {
558        // we always pad spaces around operators
559        let expression = "genome_size<1000";
560        let mut cli_exp = CLIexpression::new(expression);
561        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
562        assert_eq!(result.unwrap(), " AND genome_size < 1000");
563    }
564
565    #[test]
566    fn test_5() {
567        let expression = "sequencing_status_dtol == published";
568        let mut cli_exp = CLIexpression::new(expression);
569        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
570        assert_eq!(result.unwrap(), " AND sequencing_status_dtol == published");
571    }
572
573    #[test]
574    fn test_6() {
575        let expression = "genome_size > 1000 AND sequencing_status_dtol == published";
576        let mut cli_exp = CLIexpression::new(expression);
577        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
578        assert_eq!(
579            result.unwrap(),
580            " AND genome_size > 1000 AND sequencing_status_dtol == published"
581        );
582    }
583
584    #[test]
585    fn test_field_subset_direct() {
586        let expression = "genome_size:direct > 1000";
587        let mut cli_exp = CLIexpression::new(expression);
588        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
589        assert_eq!(result.unwrap(), " AND genome_size:direct > 1000");
590    }
591
592    #[test]
593    fn test_field_subset_invalid_rejected() {
594        let expression = "genome_size:bogus > 1000";
595        let mut cli_exp = CLIexpression::new(expression);
596        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
597        assert!(result.is_err());
598        assert!(result.unwrap_err().to_string().contains("bogus"));
599    }
600
601    #[test]
602    fn test_null_value_accepted() {
603        let expression = "genome_size = null";
604        let mut cli_exp = CLIexpression::new(expression);
605        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
606        assert_eq!(result.unwrap(), " AND genome_size = null");
607    }
608
609    #[test]
610    fn test_not_null_value_accepted() {
611        let expression = "genome_size != !null";
612        let mut cli_exp = CLIexpression::new(expression);
613        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
614        assert_eq!(result.unwrap(), " AND genome_size != !null");
615    }
616
617    #[test]
618    fn test_extra_fields_dynamic_field_accepted() {
619        let mut extra = HashSet::new();
620        extra.insert("some_new_goat_field".to_string());
621        let expression = "some_new_goat_field > 5";
622        let mut cli_exp = CLIexpression::new(expression);
623        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, Some(&extra));
624        assert_eq!(result.unwrap(), " AND some_new_goat_field > 5");
625    }
626
627    #[test]
628    fn test_extra_fields_unknown_still_rejected() {
629        let expression = "truly_unknown_field > 5";
630        let mut cli_exp = CLIexpression::new(expression);
631        let result = cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None);
632        assert!(result.is_err());
633    }
634
635    #[test]
636    fn test_all_operators_without_spaces() {
637        for op in ["!=", "<", "<=", "=", "==", ">", ">="] {
638            let expression = format!("genome_size{op}1000");
639            let mut cli_exp = CLIexpression::new(&expression);
640            assert_eq!(
641                cli_exp.parse(&GOAT_TAXON_VARIABLE_DATA, None).unwrap(),
642                format!(" AND genome_size {op} 1000")
643            );
644        }
645    }
646
647    #[test]
648    fn test_date_check_accepts_year() {
649        let t = TypeOf::Date;
650        assert!(t.check("2024", "assembly_date").is_ok());
651    }
652
653    #[test]
654    fn test_date_check_accepts_full_date() {
655        let t = TypeOf::Date;
656        assert!(t.check("2024-03-10", "assembly_date").is_ok());
657    }
658
659    #[test]
660    fn test_date_check_accepts_year_month() {
661        let t = TypeOf::Date;
662        assert!(t.check("2024-03", "assembly_date").is_ok());
663    }
664
665    #[test]
666    fn test_date_check_rejects_bad_token_count() {
667        let t = TypeOf::Date;
668        assert!(t.check("2024-03-10-01", "assembly_date").is_err());
669    }
670
671    #[test]
672    fn test_date_check_rejects_bad_month() {
673        let t = TypeOf::Date;
674        assert!(t.check("2024-13", "assembly_date").is_err());
675    }
676
677    #[test]
678    fn test_date_check_rejects_non_numeric_year() {
679        let t = TypeOf::Date;
680        assert!(t.check("abcd", "assembly_date").is_err());
681    }
682
683    #[test]
684    fn test_date_check_rejects_bad_full_date_shape() {
685        let t = TypeOf::Date;
686        assert!(t.check("2024-3-10", "assembly_date").is_err());
687    }
688
689    fn parse(expression: &str) -> Result<String> {
690        CLIexpression::new(expression).parse(&GOAT_TAXON_VARIABLE_DATA, None)
691    }
692
693    #[test]
694    fn test_lowercase_and() {
695        assert_eq!(
696            parse("genome_size > 1000 and c_value < 5").unwrap(),
697            " AND genome_size > 1000 AND c_value < 5"
698        );
699    }
700
701    #[test]
702    fn test_and_inside_a_value_does_not_split() {
703        // "island" contains "and", but not surrounded by whitespace
704        assert!(parse("country_list = island").is_ok());
705    }
706
707    #[test]
708    fn test_or_groups() {
709        assert_eq!(
710            parse("genome_size > 1e9 AND c_value > 1 OR assembly_level = chromosome").unwrap(),
711            " AND genome_size > 1e9 AND c_value > 1 OR assembly_level = chromosome"
712        );
713    }
714
715    #[test]
716    fn test_or_lowercase_with_parentheses() {
717        assert_eq!(
718            parse("(genome_size > 1e9 AND c_value > 1) or (assembly_level = chromosome)").unwrap(),
719            " AND genome_size > 1e9 AND c_value > 1 OR assembly_level = chromosome"
720        );
721    }
722
723    #[test]
724    fn test_nested_parentheses_rejected() {
725        let err = parse("genome_size > 1 AND (c_value > 1 OR c_value < 0)").unwrap_err();
726        assert!(err.to_string().contains("nesting"), "{}", err);
727    }
728
729    #[test]
730    fn test_symbolic_boolean_operators_rejected() {
731        assert!(parse("genome_size > 1 && c_value > 1").is_err());
732        assert!(parse("genome_size > 1 || c_value > 1").is_err());
733    }
734
735    #[test]
736    fn test_scientific_notation_and_negative_numbers() {
737        assert!(parse("genome_size > 1e9").is_ok());
738        assert!(parse("genome_size > 1.5E9").is_ok());
739        assert!(parse("c_value > -1").is_ok());
740    }
741
742    #[test]
743    fn test_size_suffix_rejected_with_hint() {
744        let err = parse("genome_size > 1G").unwrap_err();
745        assert!(err.to_string().contains("scientific notation"), "{}", err);
746    }
747
748    #[test]
749    fn test_enforced_keyword_is_case_insensitive() {
750        assert_eq!(
751            parse("assembly_level = Chromosome").unwrap(),
752            " AND assembly_level = Chromosome"
753        );
754    }
755
756    #[test]
757    fn test_enforced_keyword_rejects_unknown_value() {
758        let err = parse("assembly_level = chromosom").unwrap_err();
759        assert!(err.to_string().contains("did you mean \"chromosome\""), "{}", err);
760    }
761
762    #[test]
763    fn test_unenforced_keyword_accepts_unknown_value() {
764        // long_list has known values, but the API accepts any value
765        assert!(parse("long_list = DTOL").is_ok());
766        assert!(parse("long_list = some_new_project").is_ok());
767    }
768
769    #[test]
770    fn test_free_text_keyword_accepts_any_value() {
771        assert!(parse("bioproject = PRJNA533106").is_ok());
772    }
773
774    #[test]
775    fn test_multi_word_values_and_quotes() {
776        assert_eq!(
777            parse("assembly_level = chromosome, \"complete genome\"").unwrap(),
778            " AND assembly_level = chromosome,complete genome"
779        );
780    }
781
782    #[test]
783    fn test_negated_list_values() {
784        assert_eq!(
785            parse("assembly_level = chromosome,!scaffold").unwrap(),
786            " AND assembly_level = chromosome,!scaffold"
787        );
788    }
789
790    #[test]
791    fn test_keyword_range_operator() {
792        assert_eq!(
793            parse("assembly_level >= scaffold").unwrap(),
794            " AND assembly_level >= scaffold"
795        );
796    }
797
798    #[test]
799    fn test_field_synonym_and_hyphen_and_case() {
800        assert_eq!(parse("ebp_metric_date >= 2023").unwrap(), " AND ebp_standard_date >= 2023");
801        assert_eq!(parse("genome-size > 1").unwrap(), " AND genome_size > 1");
802        assert_eq!(parse("Genome_Size > 1").unwrap(), " AND genome_size > 1");
803    }
804
805    #[test]
806    fn test_hyphenated_field_name() {
807        assert!(parse("marhabreg-2017 = yes").is_ok());
808    }
809
810    #[test]
811    fn test_identifier_terms() {
812        assert_eq!(parse("taxon_id = 9606,9598").unwrap(), " AND taxon_id = 9606,9598");
813    }
814
815    #[test]
816    fn test_bare_field_is_validated() {
817        let err = parse("genome_sizee").unwrap_err();
818        assert!(err.to_string().contains("did you mean \"genome_size\""), "{}", err);
819    }
820
821    #[test]
822    fn test_contains_rejected_with_hint() {
823        let err = parse("long_list contains dtol").unwrap_err();
824        assert!(err.to_string().contains("long_list = dtol"), "{}", err);
825    }
826
827    #[test]
828    fn test_count_function_takes_integer() {
829        assert!(parse("count(assembly_level) > 1").is_ok());
830        assert!(parse("length(long_list) > x").is_err());
831    }
832
833    #[test]
834    fn test_unknown_function_rejected() {
835        let err = parse("mean(genome_size) > 1").unwrap_err();
836        assert!(err.to_string().contains("unknown function"), "{}", err);
837    }
838
839    #[test]
840    fn test_long_expressions_allowed() {
841        let expression = vec!["genome_size > 1"; 20].join(" AND ");
842        assert!(parse(&expression).is_ok());
843    }
844
845    #[test]
846    fn test_trailing_and_rejected() {
847        assert!(parse("genome_size > 1 AND ").is_err());
848    }
849}