add rpm: get_installed_package()

Signed-off-by: Jia Chao <jiachao2130@126.com>
This commit is contained in:
Jia Chao 2024-06-26 14:29:40 +08:00
parent 11a6ca013a
commit bea65e445a
2 changed files with 50 additions and 11 deletions

View File

@ -1,14 +1,16 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
use std::sync::Once;
#[cfg(test)]
mod tests {
use super::*;
use librpm::config;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
// use librpm
pub mod rpm;
// 使用 Once 对配置进行一次性地初始化
static CONFIGURE: Once = Once::new();
// 使用系统的默认配置
pub fn configure() {
CONFIGURE.call_once(|| {
config::read_file(None).unwrap();
});
}

37
src/rpm.rs Normal file
View File

@ -0,0 +1,37 @@
use std::collections::HashMap;
use librpm::db;
use librpm::Package;
// 获取当前系统已安装的所有 RPM 包
// 最后返回一个 map其键为包名因有多个版本可并存的软件包如 kernel故其值为 `Vec<Package>`
pub fn get_installed_packages() -> HashMap<String, Vec<Package>> {
let mut installed: HashMap<String, Vec<Package>> = HashMap::new();
db::installed_packages().for_each(|pkg| {
if let Some(rpms) = installed.get_mut(pkg.name()) {
rpms.push(pkg.clone());
} else {
installed.insert(pkg.name().into(), vec![pkg.clone()]);
}
});
installed
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn get_installed_pkgs() {
crate::configure();
let pkgs = get_installed_packages();
if let Some(_) = pkgs.get("kernel") {
assert!(true);
} else {
assert_eq!("", "Package kernel not installed?");
}
}
}