1use crate::client::GoatClient;
6use crate::error::Result;
7use crate::cli::LookupArgs;
8use crate::output::Table;
9use crate::IndexType;
10use futures::StreamExt;
11use serde_json::Value;
12
13pub mod lookup;
15use lookup::{AssemblyCollector, Collector, Lookups, TaxonCollector};
16
17pub enum LookupAction {
18 Continue,
19 PrintedAndExit,
20}
21
22pub async fn lookup(
24 args: &LookupArgs,
25 cli: bool,
26 index_type: IndexType,
27) -> Result<LookupAction> {
28 let lookups = Lookups::new(args, index_type)?;
29 let url_vector_api = lookups.make_urls();
30 let size = args.size;
31
32 if args.url {
33 for (index, (url, _)) in url_vector_api.iter().enumerate() {
34 crate::outln!("{}.\tGoaT lookup API URL: {}", index, url)?;
35 }
36 return Ok(LookupAction::PrintedAndExit);
37 }
38
39 let concurrent_requests = url_vector_api.len();
40
41 let client = GoatClient::new();
42 let fetches = futures::stream::iter(
43 url_vector_api
44 .into_iter()
45 .enumerate()
46 .map(|(idx, (path, search_query))| {
47 let client = client.clone();
48 async move {
49 let v: Value = client.get_json(&path).await?;
50 let request_size_op = &v["status"]["hits"].as_u64();
52 match request_size_op {
53 Some(s) => {
54 if size < *s {
55 eprintln!(
56 "For search query {}, size specified ({}) was less than the number of results returned, ({}).",
57 search_query, size, s
58 )
59 }
60 }
61 None => (),
62 }
63
64 let suggestions_text_op = &v["suggestions"].as_array();
66 let mut suggestions_vec = Vec::new();
68 let suggestions_text = match suggestions_text_op {
69 Some(suggestions) => {
70 for el in *suggestions {
71 let sug_str = el["suggestion"]["text"].as_str();
72 let sug_string_op = sug_str.map(String::from);
73 suggestions_vec.push(sug_string_op);
74 }
75 Some(suggestions_vec.clone())
76 }
77 None => None,
78 };
79
80 let collector = match index_type {
82 IndexType::Taxon => Collector::Taxon(process_taxon_results(
83 v,
84 search_query,
85 suggestions_text,
86 )),
87 IndexType::Assembly => Collector::Assembly(
88 process_assembly_results(v, search_query, suggestions_text),
89 ),
90 };
91
92 Ok((idx, collector))
93 }
94 }),
95 )
96 .buffer_unordered(crate::client::concurrency(concurrent_requests))
97 .collect::<Vec<_>>();
98
99 let awaited_fetches = fetches.await;
100
101 let mut ordered_results = Vec::new();
102 for el in awaited_fetches {
103 match el {
104 Ok(v) => ordered_results.push(v),
105 Err(e) => return Err(e),
106 }
107 }
108
109 ordered_results.sort_by_key(|(idx, _)| *idx);
110
111 if cli {
112 let mut table = Table::new(match index_type {
113 IndexType::Taxon => TaxonCollector::HEADER,
114 IndexType::Assembly => AssemblyCollector::HEADER,
115 });
116 for (_original_idx, collector) in ordered_results {
117 match collector {
118 Collector::Taxon(e) => e.add_rows(&mut table),
119 Collector::Assembly(e) => e.add_rows(&mut table),
120 }
121 }
122 table.print(args.format)?;
123 }
124
125 Ok(LookupAction::Continue)
126}
127
128fn process_taxon_results(
133 v: Value,
134 search_query: String,
135 suggestions_text: Option<Vec<Option<String>>>,
136) -> TaxonCollector {
137 let mut taxon_id_vec = Vec::new();
143 let mut taxon_rank_vec = Vec::new();
144 let mut taxon_names_array_vec = Vec::new();
145
146 let results_array = v["results"].as_array();
147 if let Some(arr) = results_array {
149 for el in arr {
150 let taxon_id = el["result"]["taxon_id"].as_str();
151 let taxon_rank = el["result"]["taxon_rank"].as_str();
152 let taxon_names_array_op = el["result"]["taxon_names"].as_array();
153
154 let taxon_names_array = match taxon_names_array_op {
155 Some(vec) => {
156 let mut collect_names = Vec::new();
157 for el in vec.iter() {
158 let key = el["name"].as_str().unwrap_or("-");
159 let value = el["class"].as_str().unwrap_or("-");
160 collect_names.push((key.to_string(), value.to_string()));
162 }
163 Some(collect_names)
164 }
165 None => None,
166 };
167
168 taxon_id_vec.push(taxon_id);
170 taxon_rank_vec.push(taxon_rank);
171 taxon_names_array_vec.push(taxon_names_array);
172 }
173 }
174
175 let taxon_id = taxon_id_vec.iter().map(|e| e.map(String::from)).collect();
177 let taxon_rank = taxon_rank_vec.iter().map(|e| e.map(String::from)).collect();
178
179 TaxonCollector {
180 search: Some(search_query),
181 suggestions: suggestions_text,
182 taxon_id,
183 taxon_names: taxon_names_array_vec,
184 taxon_rank,
185 }
186}
187
188fn process_assembly_results(
190 v: Value,
191 search_query: String,
192 suggestions_text: Option<Vec<Option<String>>>,
193) -> AssemblyCollector {
194 let mut taxon_id_vec = Vec::new();
196 let mut identifiers_array_vec = Vec::new();
198
199 let results_array = v["results"].as_array();
200 if let Some(arr) = results_array {
202 for el in arr {
203 let taxon_id = el["result"]["taxon_id"].as_str();
204 let identifiers_array_op = el["result"]["identifiers"].as_array();
205
206 let identifiers_array = match identifiers_array_op {
207 Some(vec) => {
208 let mut collect_names = Vec::new();
209 for el in vec.iter() {
210 let key = el["identifier"].as_str().unwrap_or("-");
211 let value = el["class"].as_str().unwrap_or("-");
212 collect_names.push((key.to_string(), value.to_string()));
214 }
215 Some(collect_names)
216 }
217 None => None,
218 };
219
220 taxon_id_vec.push(taxon_id);
222 identifiers_array_vec.push(identifiers_array);
223 }
224 }
225
226 let taxon_id = taxon_id_vec.iter().map(|e| e.map(String::from)).collect();
228
229 AssemblyCollector {
230 search: Some(search_query),
231 suggestions: suggestions_text,
232 taxon_id,
233 identifiers: identifiers_array_vec,
234 }
235}