use std::{fmt, io, path::PathBuf};
pub enum Error {
ConfigExist(PathBuf),
NoFile(PathBuf, io::Error),
UnableToParse(toml::de::Error),
UnableToSerialize(toml::ser::Error),
NoCmdStringFound(PathBuf, String),
CmdStringExists(PathBuf, String),
NoConfigForPath(PathBuf),
Command(io::Error),
CurrentDir,
ConfigDir,
ConfigPath(io::Error),
ConfigFileCreation(PathBuf, io::Error),
ConfigFileWrite(PathBuf, io::Error),
ConfigDirCreation(PathBuf, io::Error),
NewConfig(PathBuf),
NoConfigFile,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let err = match self {
Error::ConfigExist(p) => {
format!("configuration file alr exist at {:?}", p)
}
Error::NoConfigFile => {
"No configuration file found. Create one in this directory using `dipse init`"
.to_string()
}
Error::NoFile(file_loc, e) => {
format!("Could not read file: {}\n{}", file_loc.display(), e)
}
Error::ConfigFileWrite(path, e) => {
format!("Could not write to config file: {}\n{}", path.display(), e)
}
Error::UnableToParse(e) => {
format!("{}", e)
}
Error::UnableToSerialize(e) => {
format!("{}", e)
}
Error::CurrentDir => "Unable to access the current working directory.".to_string(),
Error::NoConfigForPath(path) => {
format!("No entries found for path: {}", path.display())
}
Error::NoCmdStringFound(path, cmd) => {
format!("No command {} found for path: {}", cmd, path.display())
}
Error::CmdStringExists(path, cmd) => {
format!(
"Command {} already exists for path: {}",
cmd,
path.display()
)
}
Error::Command(e) => {
format!("{}", e)
}
Error::ConfigDirCreation(path, e) => {
format!(
"Could not create configuration directory: {}\n{}",
path.display(),
e
)
}
Error::ConfigPath(e) => {
format!("{}", e)
}
Error::ConfigFileCreation(path, e) => {
format!(
"Could not create configuration file: {}\n{}",
path.display(),
e
)
}
Error::NewConfig(path) => {
format!("Empty configuration file. Please edit {}", path.display())
}
Error::ConfigDir => "Could not access config directory".to_string(),
};
write!(f, "{}", err)
}
}