You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.3 KiB
65 lines
1.3 KiB
use std::{
|
|
error,
|
|
fmt,
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
pub enum Error
|
|
{
|
|
Unknown,
|
|
Message(String),
|
|
MessageStatic(&'static str),
|
|
FileNotFound(std::path::PathBuf),
|
|
BadPath(std::path::PathBuf),
|
|
ExpectedFile(std::path::PathBuf),
|
|
}
|
|
|
|
impl error::Error for Error{}
|
|
impl fmt::Display for Error
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
{
|
|
write!(f, "failed to parse args: ")?;
|
|
match self {
|
|
Error::Message(message) => write!(f, "{}", message),
|
|
Error::MessageStatic(message) => write!(f, "{}", message),
|
|
Error::FileNotFound(file) => write!(f, "file not found: `{:?}`", file),
|
|
Error::BadPath(file) => write!(f, "bad path: `{:?}`", file),
|
|
Error::ExpectedFile(file) => write!(f, "expected a file: `{:?}`", file),
|
|
_ => write!(f, "unknown error"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<String> for Error
|
|
{
|
|
fn from(string: String) -> Self
|
|
{
|
|
Self::Message(string)
|
|
}
|
|
}
|
|
|
|
impl From<&'static str> for Error
|
|
{
|
|
fn from(string: &'static str) -> Self
|
|
{
|
|
Self::MessageStatic(string)
|
|
}
|
|
}
|
|
|
|
use std::path::PathBuf;
|
|
impl From<PathBuf> for Error
|
|
{
|
|
fn from(path: PathBuf) -> Self
|
|
{
|
|
|
|
if !path.exists() {
|
|
Self::FileNotFound(path)
|
|
} else if path.is_dir() {
|
|
Self::ExpectedFile(path)
|
|
} else {
|
|
Self::BadPath(path)
|
|
}
|
|
}
|
|
}
|