pacsea/sources/search.rs
1//! AUR search query execution and result parsing.
2
3use crate::integrations::arch_toolkit::ToolkitContext;
4use crate::state::PackageItem;
5
6/// What: Fetch search results from AUR and return items along with any error messages.
7///
8/// Input:
9/// - `query` raw query string to search
10///
11/// Output:
12/// - Tuple `(items, errors)` where `items` are `PackageItem`s found and `errors` are human-readable messages for partial failures
13///
14/// Details:
15/// - Constructs a standalone toolkit context for CLI/non-runtime callers; the TUI uses
16/// [`fetch_all_with_context`] so workers share one configured client.
17pub async fn fetch_all_with_errors(query: String) -> (Vec<PackageItem>, Vec<String>) {
18 let context = match ToolkitContext::new() {
19 Ok(context) => context,
20 Err(error) => return (Vec::new(), vec![error.to_string()]),
21 };
22 fetch_all_with_context(&context, query).await
23}
24
25/// What: Fetch AUR search results with a caller-owned shared toolkit context.
26///
27/// Inputs:
28/// - `context`: Runtime integration context.
29/// - `query`: Raw query string.
30///
31/// Output:
32/// - Pacsea package rows and nonfatal user-facing errors.
33///
34/// Details:
35/// - arch-toolkit owns bounded transport and parsing; Pacsea retains the 200-row cap and UI model.
36pub async fn fetch_all_with_context(
37 context: &ToolkitContext,
38 query: String,
39) -> (Vec<PackageItem>, Vec<String>) {
40 match crate::integrations::arch_toolkit::aur::search(context, &query).await {
41 Ok(items) => (items, Vec::new()),
42 Err(error) => (Vec::new(), vec![error]),
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 /// What: Verify an invalid search query is rejected without returning rows.
49 ///
50 /// Inputs:
51 /// - An empty query and a standalone toolkit context.
52 ///
53 /// Output:
54 /// - No rows and one actionable nonfatal error.
55 ///
56 /// Details:
57 /// - Validation occurs before any network request.
58 #[tokio::test]
59 async fn invalid_search_query_returns_nonfatal_error() {
60 let context = crate::integrations::arch_toolkit::ToolkitContext::new()
61 .expect("toolkit context should construct");
62 let (items, errors) = super::fetch_all_with_context(&context, " \t\n ".to_string()).await;
63 assert!(items.is_empty());
64 assert_eq!(errors.len(), 1);
65 assert_eq!(
66 errors[0],
67 "AUR search query is empty; enter a package name and retry"
68 );
69 }
70}