goat_cli/search/
mod.rs

1//!
2//! Invoked by calling:
3//! `goat-cli search <args>`
4
5use futures::{FutureExt, StreamExt};
6use serde_json::{json, Value};
7
8use crate::client::GoatClient;
9use crate::error::Result;
10use crate::utils::cli_matches::CliAction;
11use crate::utils::{cli_matches, utils};
12use crate::cli::SearchRequest;
13use crate::count;
14use crate::output::Format;
15
16/// Execute the `search` subcommand from `goat-cli`. Print a TSV, CSV or
17/// JSON (`--format`).
18pub async fn search(request: &SearchRequest, unique_ids: Vec<String>) -> Result<()> {
19    let url_vector_api =
20        match cli_matches::process_cli_args(request, "search", unique_ids.clone())? {
21            CliAction::Continue { urls, .. } => urls,
22            CliAction::PrintedAndExit => return Ok(()),
23        };
24
25    let concurrent_requests = url_vector_api.len();
26
27    let format = request.output.format;
28    let client = GoatClient::new();
29    let fetches = futures::stream::iter(url_vector_api.into_iter().map(|path| {
30        let client = client.clone();
31        async move { client.get_text(&path, format.accept()).await }
32    }))
33    .buffered(crate::client::concurrency(concurrent_requests))
34    .collect::<Vec<_>>();
35
36    // the count is only used to print warnings, so fetch it alongside the
37    // search rather than before it.
38    let (_, awaited_fetches) = futures::try_join!(
39        count::count(request, false, true, unique_ids),
40        fetches.map(Ok)
41    )?;
42
43    match format {
44        Format::Json => merge_json(awaited_fetches.into_iter().collect::<Result<Vec<_>>>()?)?,
45        // GoaT's TSV and CSV have one header line, then a row per line
46        Format::Tsv | Format::Csv => utils::format_tsv_output(awaited_fetches)?,
47    }
48
49    Ok(())
50}
51
52/// Print the JSON responses for each taxon as one response.
53fn merge_json(responses: Vec<String>) -> Result<()> {
54    crate::outln!("{}", merged_json(responses)?)?;
55    Ok(())
56}
57
58/// GoaT's JSON response for one taxon, as is; for several, the first
59/// response with the results of all of them and the total hits.
60fn merged_json(mut responses: Vec<String>) -> Result<String> {
61    if responses.len() <= 1 {
62        return Ok(responses.pop().unwrap_or_default());
63    }
64    let mut merged: Value = serde_json::from_str(&responses[0])?;
65    let mut hits = 0;
66    let mut results = Vec::new();
67    for response in &responses {
68        let mut response: Value = serde_json::from_str(response)?;
69        hits += response["status"]["hits"].as_u64().unwrap_or(0);
70        if let Value::Array(r) = response["results"].take() {
71            results.extend(r);
72        }
73    }
74    merged["status"]["hits"] = json!(hits);
75    merged["results"] = Value::Array(results);
76    Ok(merged.to_string())
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_single_response_is_unchanged() {
85        let response = r#"{"status":{"hits":1,"took":5},"results":[{"id":1}],"fields":["x"],"query":{}}"#;
86        assert_eq!(merged_json(vec![response.to_string()]).unwrap(), response);
87    }
88
89    #[test]
90    fn test_merge_keeps_first_response_and_combines_results() {
91        let a = r#"{"status":{"hits":1,"success":true,"took":5},"results":[{"id":1}],"fields":["x"]}"#;
92        let b = r#"{"status":{"hits":2,"success":true,"took":9},"results":[{"id":2},{"id":3}],"fields":["x"]}"#;
93        let merged: Value = serde_json::from_str(&merged_json(vec![a.into(), b.into()]).unwrap()).unwrap();
94        assert_eq!(merged["status"], json!({"hits": 3, "success": true, "took": 5}));
95        assert_eq!(merged["results"], json!([{"id": 1}, {"id": 2}, {"id": 3}]));
96        assert_eq!(merged["fields"], json!(["x"]));
97    }
98}