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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2023 Andreas Hartmann <hartan@7x.de>
// GNU General Public License v3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
// SPDX-License-Identifier: GPL-3.0-or-later

//! Search packages with cargo (Rust)
use crate::provider::prelude::*;

use regex::Regex;

#[derive(Default, Debug, PartialEq)]
pub struct Cargo;

impl fmt::Display for Cargo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "cargo")
    }
}

impl Cargo {
    pub fn new() -> Self {
        Default::default()
    }

    pub(crate) fn get_candidates_from_search_output(
        &self,
        output: &str,
    ) -> ProviderResult<Vec<Candidate>> {
        let err_context = "failed to parse output from cargo";
        log::trace!("parsing search output: \n'{}'", output);

        let lines = output
            .lines()
            .map(|s| s.to_string())
            .collect::<Vec<String>>();

        let mut results = vec![];

        let cargo_regex = Regex::new(
            "^(?P<package>[a-zA-Z0-9-_]+) = \"(?P<version>.*)\"\\s+# (?P<description>.+)$",
        )
        .context(err_context)?;

        for line in lines {
            if line.is_empty() {
                continue;
            }
            match cargo_regex.captures(&line) {
                Some(caps) => {
                    let mut candidate = Candidate {
                        package: match_to_string(&caps, 1).context(err_context)?,
                        version: match_to_string(&caps, 2).context(err_context)?,
                        description: match_to_string(&caps, 3).context(err_context)?,
                        origin: "".to_string(),
                        ..Candidate::default()
                    };
                    candidate.actions.install = Some(cmd!(
                        "cargo".into(),
                        "install".into(),
                        "--version".into(),
                        candidate.version.clone(),
                        candidate.package.clone()
                    ));

                    results.push(candidate);
                }
                None => {
                    log::trace!("regex didn't match on line '{}'", line);
                    continue;
                }
            }
        }

        Ok(results)
    }
}

fn match_to_string(capture: &regex::Captures, index: usize) -> ProviderResult<String> {
    Ok(capture
        .get(index)
        .with_context(|| format!("failed to retrieve regex capture group {}", index))?
        .as_str()
        .to_string())
}

#[async_trait]
impl IsProvider for Cargo {
    async fn search_internal(
        &self,
        command: &str,
        target_env: Arc<Environment>,
    ) -> ProviderResult<Vec<Candidate>> {
        let stdout = target_env
            .output_of(cmd!(
                "cargo", "search", "--limit", "5", "--color", "never", command
            ))
            .await?;

        let mut candidates = self.get_candidates_from_search_output(&stdout)?;
        // Fill in the execution details
        for c in candidates.iter_mut() {
            c.actions.execute = cmd!(command.to_string());
        }

        Ok(candidates)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::prelude::*;

    #[test]
    fn initialize() {
        let _cargo = Cargo::new();
    }

    test::default_tests!(Cargo::new());

    /// Searching a nonexistent package creates empty output, which means "Command not found".
    ///
    /// - Searched with: cargo 1.69.0
    /// - Search command: "cargo search --limit 5 --color never 'asdlwhajksdmwdankjs'"
    #[test]
    fn matches_empty() {
        let query = quick_test!(Cargo::new(), Ok("".to_string()));

        assert::is_err!(query);
        assert::err::not_found!(query);
    }

    /// Searching an existent package
    ///
    /// - Searched with: cargo 1.69.0
    /// - Search command: "cargo search --limit 5 --color never zellij"
    #[test]
    fn matches_zellij() {
        let query = quick_test!(Cargo::new(), Ok("
zellij = \"0.36.0\"                          # A terminal workspace with batteries included
zellij-runner = \"0.2.0\"                    # Session runner/switcher for Zellij
zellij-client = \"0.36.0\"                   # The client-side library for Zellij
zellij-server = \"0.36.0\"                   # The server-side library for Zellij
zellij-tile = \"0.36.0\"                     # A small client-side library for writing Zellij plugins
... and 11 crates more (use --limit N to see more)
".to_string()));

        let result = query.results.unwrap();

        assert!(result.len() == 5);
        assert!(result[0].package == "zellij");
        assert!(result[0].version == "0.36.0");
        assert!(result[0].origin.is_empty());
        assert!(result[0].description == "A terminal workspace with batteries included");
        assert!(result[1].description == "Session runner/switcher for Zellij");
    }

    /// Searching without network connection.
    ///
    /// - Searched with: cargo 1.69.0
    /// - Search command: "cargo search --limit 5 --color never zellij"
    #[test]
    fn no_network() {
        let query = quick_test!(Cargo::new(), Err(ExecutionError::NonZero {
            command: "cargo".to_string(),
            output: std::process::Output {
                stdout: r"".into(),
                stderr: r"error: failed to retrieve search results from the registry at https://crates.io

Caused by:
  [6] Couldn't resolve host name (Could not resolve host: crates.io)
".into(),
                status: ExitStatus::from_raw(101),
            },
        }));

        assert::is_err!(query);
        assert::err::execution!(query);
    }
}