1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::{error::RPCResult, models::PackageMetadata};

/// Searches for packages by name
pub async fn search<S: AsRef<str>>(keywords: S) -> RPCResult<Vec<PackageMetadata>> {
    search_by(SearchField::Name, keywords).await
}

/// Searches for packages by a specific field
pub async fn search_by<S: AsRef<str>>(
    field: SearchField,
    keywords: S,
) -> RPCResult<Vec<PackageMetadata>> {
    let args: Vec<(&str, &str)> = vec![
        ("v", "5"),
        ("type", "search"),
        ("by", field.to_string()),
        ("arg", keywords.as_ref()),
    ];
    let response = super::call_aur::<_, PackageMetadata>(&args).await?;

    Ok(response.results)
}

/// Represents a field to search by
#[derive(Debug, Clone, Copy)]
pub enum SearchField {
    /// Searches by name
    Name,
    /// Searches name and description
    NameDesc,
    /// Searches by package maintainer
    Maintainer,
    /// Searches for packages that depend on the given keywods
    Depends,
    /// Searches for packages that require the given keywords to be build
    MakeDepends,
    /// Searches for packages that optionally depend on the given keywods
    OptDepends,
    /// Searches for packages that require the given keywods to be present
    CheckDepends,
}

impl SearchField {
    pub(crate) fn to_string(&self) -> &'static str {
        match self {
            SearchField::Name => "name",
            SearchField::NameDesc => "name-desc",
            SearchField::Maintainer => "maintainer",
            SearchField::Depends => "depends",
            SearchField::MakeDepends => "makedepends",
            SearchField::OptDepends => "optdepends",
            SearchField::CheckDepends => "checkdepends",
        }
    }
}