Add new Package examples to main

Print debug line with module arguments when run starts
Make apt package manager non-interactive
Move PackageList to dedicated module and make more impls
utils::Cmd now can take environment variables
This commit is contained in:
selfhoster selfhoster 2023-04-20 18:31:45 +02:00
commit 1245db813b
7 changed files with 115 additions and 147 deletions

View file

@ -6,6 +6,9 @@ use crate::utils::cmd::CmdOutput;
use std::boxed::Box;
pub mod list;
pub use list::{PackageList, IntoPackageList};
pub mod pacman;
use pacman::ArchlinuxPackageManager;
pub mod apt;
@ -99,59 +102,4 @@ pub trait SpecificPackageManager: std::fmt::Debug {
fn update(&self) -> Result<(), PackageError>;
fn install(&self, list: PackageList) -> Result<(), PackageError>;
fn remove(&self, list: PackageList) -> Result<(), PackageError>;
}
#[derive(Clone, Debug, Serialize)]
pub struct PackageList {
pub list: Vec<String>,
}
impl PackageList {
pub fn list(&self) -> & [ String ] {
&self.list
}
pub fn add<T: IntoPackageList>(self, add: T) -> PackageList {
let Self { mut list } = self;
let extend_with = add.into_package_list();
list.extend(extend_with.list);
PackageList {
list
}
}
}
/// Turn a stringy value or list of values into an actual package list.
/// Only implemented for &str and &str/String slices because apparently
/// there may be a future implementation that turns a slice of stringy values
/// into a stringy value, resulting in conflicting implementations.
/// https://stackoverflow.com/questions/63136970/how-do-i-work-around-the-upstream-crates-may-add-a-new-impl-of-trait-error
pub trait IntoPackageList {
fn into_package_list(self) -> PackageList;
}
impl IntoPackageList for &str {
fn into_package_list(self) -> PackageList {
PackageList {
list: vec!(self.to_string()),
}
}
}
impl IntoPackageList for & [ &str ] {
fn into_package_list(self) -> PackageList {
PackageList {
list: self.iter().map(|x| x.to_string()).collect(),
}
}
}
impl IntoPackageList for & [ String ] {
fn into_package_list(self) -> PackageList {
PackageList {
list: self.to_vec(),
}
}
}
}