refact(rustup): Use match statements and bail

Use match statements for Option types and use bail! macro to utilize
recoverable errors.

Signed-off-by: innocentzero <isfarulhaque@proton.me>
This commit is contained in:
innocentzero
2024-04-02 04:58:19 +05:30
parent fd47659959
commit 75709a7ed9
+43 -33
View File
@@ -1,8 +1,7 @@
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants;
use crate::{Group, Package};
use anyhow::{Context, Result};
use core::panic;
use anyhow::{bail, Context, Result};
use std::collections::HashSet;
use std::os::unix::process::ExitStatusExt;
use std::process::{Command, ExitStatus};
@@ -58,15 +57,14 @@ impl Backend for Rustup {
}
fn make_dependency(&self, _: &[Package]) -> Result<ExitStatus> {
anyhow::bail!("Not supported by {}", self.get_binary())
bail!("Not supported by {}", self.get_binary())
}
fn install_packages(&self, packages: &[Package], _: bool) -> Result<ExitStatus> {
for p in packages {
let repo = if p.repo.is_some() {
p.repo.as_ref().expect("This should never be reached")
} else {
anyhow::bail!("Not specified whether it is a toolchain or a component")
let repo = match p.repo.as_ref() {
Some(name) => name,
None => bail!("Not specified whether it is a toolchain or a component"),
};
let mut cmd = Command::new(self.get_binary());
@@ -77,12 +75,22 @@ impl Backend for Rustup {
}
"component" => {
cmd.args(get_install_switches(Repotype::Component));
let mut iter = p.name.split('/');
let toolchain = iter.next().expect("Toolchain not specified!");
let component = iter.next().expect("Component not specified!");
cmd.arg(toolchain).arg(component);
let toolchain = match iter.next() {
Some(name) => name,
None => bail!("Toolchain not specified!"),
};
cmd.arg(toolchain);
let component = match iter.next() {
Some(name) => name,
None => bail!("Component not specified!"),
};
cmd.arg(component);
}
_ => anyhow::bail!("No such type is managed by rustup!"),
_ => bail!("No such type is managed by rustup!"),
}
let result = cmd.status().context("Installing toolchain {p}");
@@ -97,10 +105,9 @@ impl Backend for Rustup {
let mut toolchains_rem = Vec::new();
for p in packages {
let repo = if p.repo.is_some() {
p.repo.as_ref().expect("This will never be printed!")
} else {
anyhow::bail!("Not specified whether it is a toolchain or a component")
let repo = match p.repo.as_ref() {
Some(reponame) => reponame,
None => bail!("Not specified whether it is a toolchain or a component"),
};
if repo == "toolchain" {
@@ -117,29 +124,29 @@ impl Backend for Rustup {
}
for p in packages {
let repo = if p.repo.is_some() {
p.repo.as_ref().expect("This will never be printed!")
} else {
anyhow::bail!("Not specified whether it is a toolchain or a component")
let repo = match p.repo.as_ref() {
Some(reponame) => reponame,
None => bail!("Not specified whether it is a toolchain or a component"),
};
let mut iter = p.name.split('/').peekable();
let toolchain = if iter.peek().is_some() {
iter.next().expect("This should never be printed")
} else {
anyhow::bail!("No toolchain name provided for the given component!")
let toolchain = match iter.peek() {
Some(name) => name,
None => bail!("No toolchain name provided for the given component!"),
};
if repo == "component" && !toolchains_rem.contains(&toolchain) {
if repo == "component" && !toolchains_rem.contains(toolchain) {
let mut cmd = Command::new(self.get_binary());
cmd.args(get_remove_switches(Repotype::Component))
.arg(toolchain)
.arg(
iter.next().unwrap_or_else(|| {
panic!("Component name not provided for {}", p.name)
}),
);
.arg(toolchain);
let component = match iter.next() {
Some(name) => name,
None => bail!("No component name provided for {}", p.name),
};
cmd.arg(component);
let result = cmd.status().context("Removing toolchain {p}");
if !result.as_ref().is_ok_and(|exit| exit.success()) {
@@ -179,9 +186,12 @@ impl Rustup {
cmd.args(args);
let output = String::from_utf8(cmd.output()?.stdout)?;
let mut val = Vec::new();
for i in output.lines() {
let mut it = i.splitn(2, '-');
val.push(it.next().expect("Toolchain name is empty.").to_string());
for line in output.lines() {
let toolchain = line.split('-').next();
match toolchain {
Some(name) => val.push(name.to_string()),
None => bail!("Toolchain name not provided!"),
}
}
Ok(val)
}