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
pub mod parser;
use std::process::Command;
use std::str;
#[derive(Debug)]
pub enum AptError {
NotFound(String),
}
pub fn apt_cache(
cmd: &str,
pkg: &str,
parser: &dyn Fn(&str) -> Option<&str>,
) -> Option<Vec<String>> {
let cmd = Command::new("apt-cache")
.arg(cmd)
.arg(pkg)
.output()
.expect("Failed to run apt");
let output = str::from_utf8(&cmd.stdout);
let packages: Vec<String> = output
.unwrap()
.lines()
.filter_map(|pkg| parser(pkg))
.map(|s| s.to_string())
.collect();
match packages.len() {
0 => None,
_ => Some(packages),
}
}
#[cfg(test)]
mod tests {
use super::parser::*;
use super::*;
#[test]
fn test_apt_cache() {
assert!(apt_cache("search", "bash", &search)
.unwrap()
.contains(&"bash".to_string()));
assert!(apt_cache("search", "does-not-exist", &search).is_none());
}
}