add error handling
This commit is contained in:
@@ -207,7 +207,7 @@ impl Pacdef {
|
||||
|
||||
fn show_group_content(&self, groups: &ArgMatches) -> Result<()> {
|
||||
let mut iter = groups
|
||||
.get_many::<String>("group")
|
||||
.get_many::<String>("groups")
|
||||
.context("getting groups from args")?
|
||||
.peekable();
|
||||
|
||||
@@ -218,7 +218,7 @@ impl Pacdef {
|
||||
.groups
|
||||
.iter()
|
||||
.find(|g| g.name == *arg_group)
|
||||
.ok_or_else(|| anyhow!("group {} not found", *arg_group))?;
|
||||
.ok_or_else(|| anyhow!(crate::Error::GroupFileNotFound(g.name)))?;
|
||||
|
||||
if show_more_than_one_group {
|
||||
let name = &group.name;
|
||||
@@ -297,7 +297,13 @@ impl Pacdef {
|
||||
.collect();
|
||||
|
||||
for file in &paths {
|
||||
ensure!(!file.exists(), "group already exists under {file:?}");
|
||||
file.file_name()
|
||||
.ok_or_else(|| crate::Error::InvalidGroupName("..".to_string()))?;
|
||||
|
||||
ensure!(
|
||||
!file.exists(),
|
||||
crate::Error::GroupAlreadyExists(file.clone())
|
||||
);
|
||||
}
|
||||
|
||||
for file in &paths {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::error::Error as ErrorTrait;
|
||||
use std::fmt::Display;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Error types for pacdef.
|
||||
#[derive(Debug)]
|
||||
@@ -11,6 +12,10 @@ pub enum Error {
|
||||
ConfigFileNotFound,
|
||||
/// Group file not found.
|
||||
GroupFileNotFound(String),
|
||||
/// Group already exists.
|
||||
GroupAlreadyExists(PathBuf),
|
||||
/// Invalid group name ('.' or '..')
|
||||
InvalidGroupName(String),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
@@ -19,6 +24,13 @@ impl Display for Error {
|
||||
Self::NoPackagesFound => f.write_str("no packages matching query"),
|
||||
Self::ConfigFileNotFound => f.write_str("config file not found"),
|
||||
Self::GroupFileNotFound(name) => f.write_str(&format!("group file '{name}' not found")),
|
||||
Self::GroupAlreadyExists(path) => f.write_str(&format!(
|
||||
"group file '{}' already exists",
|
||||
path.to_string_lossy()
|
||||
)),
|
||||
Self::InvalidGroupName(name) => {
|
||||
f.write_str(&format!("group name '{name}' is not valid"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user