From bea65e445ac7d94f31c29a935d0ff2822726dd9d Mon Sep 17 00:00:00 2001 From: Jia Chao Date: Wed, 26 Jun 2024 14:29:40 +0800 Subject: [PATCH] add rpm: get_installed_package() Signed-off-by: Jia Chao --- src/lib.rs | 24 +++++++++++++----------- src/rpm.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 src/rpm.rs diff --git a/src/lib.rs b/src/lib.rs index 7d12d9a..513969e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(); + }); } diff --git a/src/rpm.rs b/src/rpm.rs new file mode 100644 index 0000000..7092a91 --- /dev/null +++ b/src/rpm.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; + +use librpm::db; +use librpm::Package; + +// 获取当前系统已安装的所有 RPM 包 +// 最后返回一个 map,其键为包名,因有多个版本可并存的软件包如 kernel,故其值为 `Vec` +pub fn get_installed_packages() -> HashMap> { + let mut installed: HashMap> = 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?"); + } + } +}