refact(backend): cleanup

This commit is contained in:
steven-omaha
2024-04-09 10:17:20 +02:00
parent f02eb76396
commit 319be7303a
3 changed files with 48 additions and 45 deletions
@@ -1,4 +1,4 @@
use super::types::{Repotype, RustupPackage};
use super::types::RustupPackage;
pub fn toolchain_of_component_was_already_removed(
removed_toolchains: &[String],
@@ -7,22 +7,6 @@ pub fn toolchain_of_component_was_already_removed(
removed_toolchains.contains(&component.toolchain)
}
pub fn sort_packages_into_toolchains_and_components(
packages: Vec<RustupPackage>,
) -> (Vec<RustupPackage>, Vec<RustupPackage>) {
let mut toolchains = vec![];
let mut components = vec![];
for package in packages {
match package.repotype {
Repotype::Toolchain => toolchains.push(package),
Repotype::Component => components.push(package),
}
}
(toolchains, components)
}
pub fn install_components(line: &str, toolchain: &str, val: &mut Vec<String>) {
let mut chunks = line.splitn(3, '-');
let component = chunks.next().expect("Component name is empty!");
@@ -61,6 +45,7 @@ pub fn group_components_by_toolchains(components: Vec<RustupPackage>) -> Vec<Vec
result.push(vec![]);
toolchains.len() - 1
});
result
.get_mut(index)
.expect(
@@ -10,6 +10,9 @@ use std::collections::HashSet;
use std::os::unix::process::ExitStatusExt;
use std::process::{Command, ExitStatus};
use self::helpers::{
group_components_by_toolchains, install_components, toolchain_of_component_was_already_removed,
};
pub use self::types::Rustup;
use self::types::{Repotype, RustupPackage};
@@ -65,7 +68,7 @@ impl Backend for Rustup {
let packages = RustupPackage::from_pacdef_packages(packages)?;
let (toolchains, components) =
helpers::sort_packages_into_toolchains_and_components(packages);
RustupPackage::sort_packages_into_toolchains_and_components(packages);
self.install_toolchains(toolchains)?;
self.install_components(components)?;
@@ -77,7 +80,7 @@ impl Backend for Rustup {
let rustup_packages = RustupPackage::from_pacdef_packages(packages)?;
let (toolchains, components) =
helpers::sort_packages_into_toolchains_and_components(rustup_packages);
RustupPackage::sort_packages_into_toolchains_and_components(rustup_packages);
let removed_toolchains = self.remove_toolchains(toolchains)?;
@@ -87,12 +90,6 @@ impl Backend for Rustup {
}
impl Rustup {
pub(crate) fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
fn run_component_command(&self, args: &[&str], toolchains: &[String]) -> Result<Vec<String>> {
let mut val = Vec::new();
@@ -103,7 +100,7 @@ impl Rustup {
let output = String::from_utf8(cmd.output()?.stdout)?;
for component in output.lines() {
helpers::install_components(component, toolchain, &mut val);
install_components(component, toolchain, &mut val);
}
}
@@ -150,7 +147,7 @@ impl Rustup {
return Ok(());
}
let components_by_toolchain = helpers::group_components_by_toolchains(components);
let components_by_toolchain = group_components_by_toolchains(components);
for components_for_one_toolchain in components_by_toolchain {
let mut cmd = Command::new(self.get_binary());
@@ -206,10 +203,7 @@ impl Rustup {
let mut cmd = Command::new(self.get_binary());
cmd.args(Repotype::Component.get_remove_switches());
if helpers::toolchain_of_component_was_already_removed(
&removed_toolchains,
&component_package,
) {
if toolchain_of_component_was_already_removed(&removed_toolchains, &component_package) {
continue;
}
@@ -14,6 +14,28 @@ pub enum Repotype {
Component,
}
/// A package as used exclusively in the rustup backend. Contrary to other packages, this does not
/// have an (optional) repository and a name, but is either a component or a toolchain, has a
/// toolchain version, and if it is a toolchain also a name.
#[derive(Debug)]
pub struct RustupPackage {
/// Whether it is a toolchain or a component.
pub repotype: Repotype,
/// The name of the toolchain this belongs to (stable, nightly, a pinned version)
pub toolchain: String,
/// If it is a toolchain, it will not have a component name.
/// If it is a component, this will be its name.
pub component: Option<String>,
}
impl Rustup {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
}
}
impl Repotype {
fn try_from<T>(value: T) -> Result<Self>
where
@@ -50,20 +72,6 @@ impl Repotype {
}
}
/// A package as used exclusively in the rustup backend. Contrary to other packages, this does not
/// have an (optional) repository and a name, but is either a component or a toolchain, has a
/// toolchain version, and if it is a toolchain also a name.
#[derive(Debug)]
pub struct RustupPackage {
/// Whether it is a toolchain or a component.
pub repotype: Repotype,
/// The name of the toolchain this belongs to (stable, nightly, a pinned version)
pub toolchain: String,
/// If it is a toolchain, it will not have a component name.
/// If it is a component, this will be its name.
pub component: Option<String>,
}
impl RustupPackage {
/// Creates a new [`RustupPackage`].
///
@@ -85,6 +93,22 @@ impl RustupPackage {
}
}
pub fn sort_packages_into_toolchains_and_components(
packages: Vec<Self>,
) -> (Vec<Self>, Vec<Self>) {
let mut toolchains = vec![];
let mut components = vec![];
for package in packages {
match package.repotype {
Repotype::Toolchain => toolchains.push(package),
Repotype::Component => components.push(package),
}
}
(toolchains, components)
}
pub fn from_pacdef_packages(packages: &[Package]) -> Result<Vec<Self>> {
let mut result = vec![];