refact(rustup): Use RepoType instead of strings

Use RepoType enums instead of strings to match in various places.

Signed-off-by: innocentzero <isfarulhaque@proton.me>
This commit is contained in:
innocentzero
2024-04-02 04:51:15 +05:30
parent fc83c43997
commit 6f92e66777
+20 -16
View File
@@ -12,6 +12,11 @@ pub struct Rustup {
pub(crate) packages: HashSet<Package>, pub(crate) packages: HashSet<Package>,
} }
enum Repotype {
Toolchain,
Component,
}
const BINARY: Text = "rustup"; const BINARY: Text = "rustup";
const SECTION: Text = "rustup"; const SECTION: Text = "rustup";
@@ -28,7 +33,7 @@ impl Backend for Rustup {
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> { fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
let mut toolchains_vec = self let mut toolchains_vec = self
.run_toolchain_command(get_info_switches("toolchain")) .run_toolchain_command(get_info_switches(Repotype::Toolchain))
.context("Getting installed toolchains")?; .context("Getting installed toolchains")?;
let mut toolchains: HashSet<Package> = toolchains_vec let mut toolchains: HashSet<Package> = toolchains_vec
@@ -37,7 +42,7 @@ impl Backend for Rustup {
.collect(); .collect();
let packages: HashSet<Package> = self let packages: HashSet<Package> = self
.run_component_command(get_info_switches("component"), &mut toolchains_vec) .run_component_command(get_info_switches(Repotype::Component), &mut toolchains_vec)
.context("Getting installed components")? .context("Getting installed components")?
.iter() .iter()
.map(|name| ["component", name].join("/").into()) .map(|name| ["component", name].join("/").into())
@@ -64,12 +69,13 @@ impl Backend for Rustup {
.expect("Not specified whether it is a toolchain or a component!"); .expect("Not specified whether it is a toolchain or a component!");
let mut cmd = Command::new(self.get_binary()); let mut cmd = Command::new(self.get_binary());
cmd.args(get_install_switches(repo));
match repo.as_str() { match repo.as_str() {
"toolchain" => { "toolchain" => {
cmd.args(get_install_switches(Repotype::Toolchain));
cmd.arg(&p.name); cmd.arg(&p.name);
} }
"component" => { "component" => {
cmd.args(get_install_switches(Repotype::Component));
let mut iter = p.name.split('/'); let mut iter = p.name.split('/');
let toolchain = iter.next().expect("Toolchain not specified!"); let toolchain = iter.next().expect("Toolchain not specified!");
let component = iter.next().expect("Component not specified!"); let component = iter.next().expect("Component not specified!");
@@ -99,7 +105,8 @@ impl Backend for Rustup {
.expect("Not specified whether it is a toolchain or a component"); .expect("Not specified whether it is a toolchain or a component");
if repo == "toolchain" { if repo == "toolchain" {
let mut cmd = Command::new(self.get_binary()); let mut cmd = Command::new(self.get_binary());
cmd.args(get_remove_switches(repo)).arg(&p.name); cmd.args(get_remove_switches(Repotype::Toolchain))
.arg(&p.name);
toolchains_rem.push(p.name.as_str()); toolchains_rem.push(p.name.as_str());
let result = cmd.status().context("Removing toolchain {p}"); let result = cmd.status().context("Removing toolchain {p}");
if !result.as_ref().is_ok_and(|exit| exit.success()) { if !result.as_ref().is_ok_and(|exit| exit.success()) {
@@ -168,27 +175,24 @@ impl Rustup {
} }
} }
fn get_install_switches(repotype: &str) -> Switches { fn get_install_switches(repotype: Repotype) -> Switches {
match repotype { match repotype {
"toolchain" => &["toolchain", "install"], Repotype::Toolchain => &["toolchain", "install"],
"component" => &["component", "add", "--toolchain"], Repotype::Component => &["component", "add", "--toolchain"],
_ => panic!("No such type managed by rust"),
} }
} }
fn get_remove_switches(repotype: &str) -> Switches { fn get_remove_switches(repotype: Repotype) -> Switches {
match repotype { match repotype {
"toolchain" => &["toolchain", "uninstall"], Repotype::Toolchain => &["toolchain", "uninstall"],
"component" => &["component", "remove", "--toolchain"], Repotype::Component => &["component", "remove", "--toolchain"],
_ => panic!("No such type managed by rust"),
} }
} }
fn get_info_switches(repotype: &str) -> Switches { fn get_info_switches(repotype: Repotype) -> Switches {
match repotype { match repotype {
"toolchain" => &["toolchain", "list"], Repotype::Toolchain => &["toolchain", "list"],
"component" => &["component", "list", "--installed", "--toolchain"], Repotype::Component => &["component", "list", "--installed", "--toolchain"],
_ => panic!("No such type managed by rust"),
} }
} }