From 0d27e3a65a0f05ebb2024c9f726f095ba8240b94 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 16 Sep 2023 01:19:16 +0100 Subject: [PATCH] feat(settings): impl Error for Error --- actix-settings/CHANGES.md | 4 ++ actix-settings/Cargo.toml | 3 +- actix-settings/src/error.rs | 74 ++++++++++++++----------------------- 3 files changed, 33 insertions(+), 48 deletions(-) diff --git a/actix-settings/CHANGES.md b/actix-settings/CHANGES.md index adf757f21..8efdeae48 100644 --- a/actix-settings/CHANGES.md +++ b/actix-settings/CHANGES.md @@ -5,6 +5,10 @@ - Rename `AtError => Error`. - Remove `AtResult` type alias. - Update `toml` dependency to `0.8`. +- Remove `ioe` dependency; `std::io::Error` is now used directly. +- Remove `Clone` implementation for `Error`. +- Implement `Display` for `Error`. +- Implement std's `Error` for `Error`. - Minimum supported Rust version (MSRV) is now 1.68. ## 0.6.0 diff --git a/actix-settings/Cargo.toml b/actix-settings/Cargo.toml index 5950dcb67..1c39ec981 100644 --- a/actix-settings/Cargo.toml +++ b/actix-settings/Cargo.toml @@ -18,8 +18,7 @@ all-features = true actix-http = "3" actix-service = "2" actix-web = "4" - -ioe = "0.5" +derive_more = "0.99.7" once_cell = "1.13" regex = "1.5.5" serde = { version = "1", features = ["derive"] } diff --git a/actix-settings/src/error.rs b/actix-settings/src/error.rs index f59e355f0..7803dca43 100644 --- a/actix-settings/src/error.rs +++ b/actix-settings/src/error.rs @@ -1,18 +1,22 @@ use std::{env::VarError, io, num::ParseIntError, path::PathBuf, str::ParseBoolError}; +use derive_more::{Display, Error}; use toml::de::Error as TomlError; /// Errors that can be returned from methods in this crate. -#[derive(Debug, Clone)] +#[derive(Debug, Display, Error)] pub enum Error { /// Environment variable does not exists or is invalid. + #[display(fmt = "Env var error: {_0}")] EnvVarError(VarError), /// File already exists on disk. - FileExists(PathBuf), + #[display(fmt = "File exists: {}", "_0.display()")] + FileExists(#[error(not(source))] PathBuf), /// Invalid value. #[allow(missing_docs)] + #[display(fmt = "Expected {expected}, got {got} (@ {file}:{line}:{column})")] InvalidValue { expected: &'static str, got: String, @@ -22,18 +26,23 @@ pub enum Error { }, /// I/O error. - IoError(ioe::IoError), + #[display(fmt = "")] + IoError(io::Error), /// Value is not a boolean. + #[display(fmt = "Failed to parse boolean: {_0}")] ParseBoolError(ParseBoolError), /// Value is not an integer. + #[display(fmt = "Failed to parse integer: {_0}")] ParseIntError(ParseIntError), /// Value is not an address. - ParseAddressError(String), + #[display(fmt = "Failed to parse address: {_0}")] + ParseAddressError(#[error(not(source))] String), /// Error deserializing as TOML. + #[display(fmt = "TOML error: {_0}")] TomlError(TomlError), } @@ -51,12 +60,6 @@ macro_rules! InvalidValue { impl From for Error { fn from(err: io::Error) -> Self { - Self::IoError(ioe::IoError::from(err)) - } -} - -impl From for Error { - fn from(err: ioe::IoError) -> Self { Self::IoError(err) } } @@ -88,48 +91,27 @@ impl From for Error { impl From for io::Error { fn from(err: Error) -> Self { match err { - Error::EnvVarError(var_error) => { - let msg = format!("Env var error: {var_error}"); - io::Error::new(io::ErrorKind::InvalidInput, msg) + Error::EnvVarError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()), + + Error::FileExists(_) => io::Error::new(io::ErrorKind::AlreadyExists, err.to_string()), + + Error::InvalidValue { .. } => { + io::Error::new(io::ErrorKind::InvalidInput, err.to_string()) } - Error::FileExists(path_buf) => { - let msg = format!("File exists: {}", path_buf.display()); - io::Error::new(io::ErrorKind::AlreadyExists, msg) + Error::IoError(io_error) => io_error, + + Error::ParseBoolError(_) => { + io::Error::new(io::ErrorKind::InvalidInput, err.to_string()) } - Error::InvalidValue { - expected, - ref got, - file, - line, - column, - } => { - let msg = format!("Expected {expected}, got {got} (@ {file}:{line}:{column})"); - io::Error::new(io::ErrorKind::InvalidInput, msg) + Error::ParseIntError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()), + + Error::ParseAddressError(_) => { + io::Error::new(io::ErrorKind::InvalidInput, err.to_string()) } - Error::IoError(io_error) => io_error.into(), - - Error::ParseBoolError(parse_bool_error) => { - let msg = format!("Failed to parse boolean: {parse_bool_error}"); - io::Error::new(io::ErrorKind::InvalidInput, msg) - } - - Error::ParseIntError(parse_int_error) => { - let msg = format!("Failed to parse integer: {parse_int_error}"); - io::Error::new(io::ErrorKind::InvalidInput, msg) - } - - Error::ParseAddressError(string) => { - let msg = format!("Failed to parse address: {string}"); - io::Error::new(io::ErrorKind::InvalidInput, msg) - } - - Error::TomlError(toml_error) => { - let msg = format!("TOML error: {toml_error}"); - io::Error::new(io::ErrorKind::InvalidInput, msg) - } + Error::TomlError(_) => io::Error::new(io::ErrorKind::InvalidInput, err.to_string()), } } }