2022-07-31 15:44:45 +02:00
|
|
|
use std::{env::VarError, io, num::ParseIntError, path::PathBuf, str::ParseBoolError};
|
|
|
|
|
|
|
|
use toml::de::Error as TomlError;
|
|
|
|
|
2022-07-31 21:12:19 +02:00
|
|
|
/// Errors that can be returned from methods in this crate.
|
|
|
|
#[derive(Debug, Clone)]
|
2022-08-10 10:13:34 +02:00
|
|
|
pub enum Error {
|
2022-07-31 21:12:19 +02:00
|
|
|
/// Environment variable does not exists or is invalid.
|
2022-07-31 15:44:45 +02:00
|
|
|
EnvVarError(VarError),
|
2022-07-31 21:12:19 +02:00
|
|
|
|
|
|
|
/// File already exists on disk.
|
2022-07-31 15:44:45 +02:00
|
|
|
FileExists(PathBuf),
|
2022-07-31 21:12:19 +02:00
|
|
|
|
|
|
|
/// Invalid value.
|
|
|
|
#[allow(missing_docs)]
|
2022-07-31 15:44:45 +02:00
|
|
|
InvalidValue {
|
|
|
|
expected: &'static str,
|
|
|
|
got: String,
|
|
|
|
file: &'static str,
|
|
|
|
line: u32,
|
|
|
|
column: u32,
|
|
|
|
},
|
2022-07-31 21:12:19 +02:00
|
|
|
|
|
|
|
/// I/O error.
|
2022-07-31 15:44:45 +02:00
|
|
|
IoError(ioe::IoError),
|
2022-07-31 21:12:19 +02:00
|
|
|
|
|
|
|
/// Value is not a boolean.
|
2022-07-31 15:44:45 +02:00
|
|
|
ParseBoolError(ParseBoolError),
|
2022-07-31 21:12:19 +02:00
|
|
|
|
|
|
|
/// Value is not an integer.
|
2022-07-31 15:44:45 +02:00
|
|
|
ParseIntError(ParseIntError),
|
2022-07-31 21:12:19 +02:00
|
|
|
|
|
|
|
/// Value is not an address.
|
2022-07-31 15:44:45 +02:00
|
|
|
ParseAddressError(String),
|
2022-07-31 21:12:19 +02:00
|
|
|
|
|
|
|
/// Error deserializing as TOML.
|
2022-07-31 15:44:45 +02:00
|
|
|
TomlError(TomlError),
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! InvalidValue {
|
|
|
|
(expected: $expected:expr, got: $got:expr,) => {
|
2022-08-10 10:13:34 +02:00
|
|
|
crate::Error::InvalidValue {
|
2022-07-31 15:44:45 +02:00
|
|
|
expected: $expected,
|
|
|
|
got: $got.to_string(),
|
|
|
|
file: file!(),
|
|
|
|
line: line!(),
|
|
|
|
column: column!(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
impl From<io::Error> for Error {
|
2022-07-31 15:44:45 +02:00
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
Self::IoError(ioe::IoError::from(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
impl From<ioe::IoError> for Error {
|
2022-07-31 15:44:45 +02:00
|
|
|
fn from(err: ioe::IoError) -> Self {
|
|
|
|
Self::IoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
impl From<ParseBoolError> for Error {
|
2022-07-31 15:44:45 +02:00
|
|
|
fn from(err: ParseBoolError) -> Self {
|
|
|
|
Self::ParseBoolError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
impl From<ParseIntError> for Error {
|
2022-07-31 15:44:45 +02:00
|
|
|
fn from(err: ParseIntError) -> Self {
|
|
|
|
Self::ParseIntError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
impl From<TomlError> for Error {
|
2022-07-31 15:44:45 +02:00
|
|
|
fn from(err: TomlError) -> Self {
|
|
|
|
Self::TomlError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
impl From<VarError> for Error {
|
2022-07-31 15:44:45 +02:00
|
|
|
fn from(err: VarError) -> Self {
|
|
|
|
Self::EnvVarError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
impl From<Error> for io::Error {
|
|
|
|
fn from(err: Error) -> Self {
|
2022-07-31 15:44:45 +02:00
|
|
|
match err {
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::EnvVarError(var_error) => {
|
2023-01-07 01:04:16 +00:00
|
|
|
let msg = format!("Env var error: {var_error}");
|
2022-07-31 15:44:45 +02:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::FileExists(path_buf) => {
|
2022-07-31 15:44:45 +02:00
|
|
|
let msg = format!("File exists: {}", path_buf.display());
|
|
|
|
io::Error::new(io::ErrorKind::AlreadyExists, msg)
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::InvalidValue {
|
2022-07-31 15:44:45 +02:00
|
|
|
expected,
|
|
|
|
ref got,
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
} => {
|
2023-01-07 01:04:16 +00:00
|
|
|
let msg = format!("Expected {expected}, got {got} (@ {file}:{line}:{column})");
|
2022-07-31 15:44:45 +02:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::IoError(io_error) => io_error.into(),
|
2022-07-31 15:44:45 +02:00
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::ParseBoolError(parse_bool_error) => {
|
2023-01-07 01:04:16 +00:00
|
|
|
let msg = format!("Failed to parse boolean: {parse_bool_error}");
|
2022-07-31 15:44:45 +02:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::ParseIntError(parse_int_error) => {
|
2023-01-07 01:04:16 +00:00
|
|
|
let msg = format!("Failed to parse integer: {parse_int_error}");
|
2022-07-31 15:44:45 +02:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::ParseAddressError(string) => {
|
2023-01-07 01:04:16 +00:00
|
|
|
let msg = format!("Failed to parse address: {string}");
|
2022-07-31 15:44:45 +02:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:34 +02:00
|
|
|
Error::TomlError(toml_error) => {
|
2023-01-07 01:04:16 +00:00
|
|
|
let msg = format!("TOML error: {toml_error}");
|
2022-07-31 15:44:45 +02:00
|
|
|
io::Error::new(io::ErrorKind::InvalidInput, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|