goat_cli/utils/
cli_matches.rs

1use crate::cli::SearchRequest;
2use crate::error::{Error, ErrorKind, Result};
3use crate::utils::{tax_ranks, url, utils};
4use crate::{GOAT_URL, TAXONOMY, UPPER_CLI_SIZE_LIMIT};
5
6pub enum CliAction {
7    Continue {
8        size: u64,
9        taxa: Vec<String>,
10        urls: Vec<String>,
11    },
12    PrintedAndExit,
13}
14
15/// Build the GoaT API URLs for a `search` or `count` (`api`), one per taxon.
16///
17/// If `-u`/`-U` were given, prints the URLs and returns
18/// [`CliAction::PrintedAndExit`]; otherwise returns the size, the taxa and
19/// their URLs.
20pub fn process_cli_args(
21    request: &SearchRequest,
22    api: &str,
23    unique_ids: Vec<String>,
24) -> Result<CliAction> {
25    let query = &request.query;
26    let index_type = request.index_type;
27
28    let expression = match &query.expression {
29        Some(s) => url::format_expression(s, index_type)?,
30        None => "".to_string(),
31    };
32    let mut taxon_filters = match &query.tax_rank {
33        Some(t) => tax_ranks::TaxRanks::init().parse(t, false)?,
34        None => "".to_string(),
35    };
36    // clap ensures --tax-depth comes with -d (tax_tree); the depth is
37    // relative to each taxon
38    if let Some(depth) = query.tax_depth {
39        taxon_filters.push_str(&format!(" AND tax_depth({})", depth));
40    }
41    let sort = match &request.sort.sort_by {
42        Some(field) => url::sort_params(
43            field,
44            &request.sort.sort_order.unwrap_or_default().to_string(),
45            index_type,
46        )?,
47        None => String::new(),
48    };
49
50    if query.size as usize > *UPPER_CLI_SIZE_LIMIT {
51        let limit_string = utils::pretty_print_usize(*UPPER_CLI_SIZE_LIMIT);
52        return Err(Error::new(ErrorKind::GenericCli(format!(
53            "searches with more than {} results are not currently supported.",
54            limit_string
55        ))));
56    }
57
58    // tree includes all descendents of a node; clap ensures -d and -l
59    // aren't both given.
60    let tax_tree = if query.descendents {
61        "tree"
62    } else if query.lineage {
63        "lineage"
64    } else {
65        "name"
66    };
67
68    let url_vector = utils::taxa_from_input(
69        query.taxon.as_deref(),
70        query.file.as_deref(),
71        query.expression.is_some(),
72    )?;
73
74    let mut url_vector_api = url::make_goat_urls(
75        api,
76        &url_vector,
77        &GOAT_URL,
78        tax_tree,
79        request.include_estimates,
80        request.include_raw_values,
81        query.exclude,
82        "count",
83        &index_type.to_string(),
84        &TAXONOMY,
85        query.size,
86        &query.ranks,
87        request.fields,
88        query.variables.as_deref(),
89        &expression,
90        &taxon_filters,
91        unique_ids,
92        index_type,
93    )?;
94    for url in &mut url_vector_api {
95        url.push_str(&sort);
96    }
97
98    if request.output.url {
99        for (index, url) in url_vector_api.iter().enumerate() {
100            crate::outln!("{}.\tGoaT API URL: {}", index, url)?;
101        }
102        return Ok(CliAction::PrintedAndExit);
103    } else if request.output.goat_ui_url {
104        for (index, url) in url_vector_api.iter().enumerate() {
105            let new_url = url.replace("api/v2/", "");
106            crate::outln!("{}.\tGoaT UI URL: {}", index, new_url)?;
107        }
108        return Ok(CliAction::PrintedAndExit);
109    }
110
111    Ok(CliAction::Continue {
112        size: query.size,
113        taxa: url_vector,
114        urls: url_vector_api,
115    })
116}