add infallible From<String> for Package

This commit is contained in:
steven-omaha
2023-01-10 15:37:50 +01:00
parent 2ad0d84794
commit c2072223c6
2 changed files with 11 additions and 1 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ fn get_explicitly_installed_packages_from_alpm() -> HashSet<String> {
}
fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
packages.into_iter().filter_map(Package::try_from).collect()
packages.into_iter().map(Package::from).collect()
}
impl Pacman {
+10
View File
@@ -14,6 +14,16 @@ fn remove_all_but_package_name(s: &str) -> &str {
.trim() // remove whitespace
}
impl From<String> for Package {
fn from(value: String) -> Self {
let trimmed = remove_all_but_package_name(&value);
debug_assert!(!trimmed.is_empty(), "empty package names are not allowed");
let (name, repo) = Self::split_into_name_and_repo(trimmed);
Self { name, repo }
}
}
impl Package {
fn split_into_name_and_repo(s: &str) -> (String, Option<String>) {
let mut iter = s.split('/').rev();