From 7fcf84f62eace93a4fa081dc113fccd2401435b2 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Sun, 29 Oct 2023 22:05:17 +0100 Subject: [PATCH] switch from custom_error to thiserror --- Cargo.lock | 7 ------- Cargo.toml | 1 - src/api/mod.rs | 15 +++++++++++---- src/config_manager.rs | 30 ++++++++++++++++++++++-------- src/web/auth.rs | 12 ++++++++---- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 16b72f9..365c23e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,12 +226,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "custom_error" -version = "1.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f8a51dd197fa6ba5b4dc98a990a43cc13693c23eb0089ebb0fcc1f04152bca6" - [[package]] name = "deranged" version = "0.3.9" @@ -696,7 +690,6 @@ version = "0.1.0" dependencies = [ "async-trait", "axum", - "custom_error", "ipnet", "jsonrpsee", "macaddr", diff --git a/Cargo.toml b/Cargo.toml index 43ca43e..bc0920b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ edition = "2021" [dependencies] async-trait = "0.1.74" axum = "0.6.20" -custom_error = "1.9.2" ipnet = { version = "2.8.0", features = ["serde"] } jsonrpsee = { version = "0.20.3", features = ["server"] } macaddr = { version = "1.0.1", features = ["serde"] } diff --git a/src/api/mod.rs b/src/api/mod.rs index 94b725e..6157638 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -12,12 +12,19 @@ use jsonrpsee::{ RpcModule, }; -use custom_error::custom_error; +use thiserror::Error; use tracing::info; -custom_error! { pub ApiError - InvalidParams = "Invalid Parameters", - Leet = "1337", +#[derive(Error, Debug)] +pub enum ApiError { + #[error("Unsupported config version")] + InvalidParams, + + #[error("1337")] + Leet, + + #[error(transparent)] + ConfigError(#[from] crate::config_manager::ConfigError), } impl Into> for ApiError { diff --git a/src/config_manager.rs b/src/config_manager.rs index 30fc760..04a55e9 100644 --- a/src/config_manager.rs +++ b/src/config_manager.rs @@ -7,18 +7,30 @@ use std::fs; use std::sync::{Arc, Mutex, MutexGuard}; use std::{io, result::Result}; -use custom_error::custom_error; - use pwhash::sha512_crypt; -custom_error! { pub ConfigError - IoError{source: io::Error} = "io error", - SerdeError{source: serde_json::Error} = "serde json error", - ValidatonError{source: validator::ValidationErrors} = "validation failed", - HashError{source: pwhash::error::Error} = "password hash generation", - UnsupportedVersionError = "unsupported config version", +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum ConfigError { + #[error("Json Error")] + SerdeError(#[from] serde_json::Error), + + #[error("Validation Error")] + ValidatonError(#[from] validator::ValidationErrors), + + #[error("Hash Error")] + HashError(#[from] pwhash::error::Error), + + #[error("Unsupported config version")] + UnsupportedVersionError, + + /// Represents all other cases of `std::io::Error`. + #[error(transparent)] + IOError(#[from] std::io::Error), } +/* // TODO do Error conversion impl Into for ConfigError { fn into(self) -> ApiError { @@ -26,6 +38,8 @@ impl Into for ConfigError { } } +*/ + pub const CURRENT_CONFIG_PATH: &str = "config.json"; pub const PENDING_CONFIG_PATH: &str = "pending.json"; diff --git a/src/web/auth.rs b/src/web/auth.rs index 526c1a2..c34d2e5 100644 --- a/src/web/auth.rs +++ b/src/web/auth.rs @@ -21,11 +21,15 @@ use axum::{ use pwhash::sha512_crypt; use tracing::info; -use custom_error::custom_error; +use thiserror::Error; -custom_error! { AuthError - NoSessionCookie = "No Session Cookie Found", - InvalidSession = "Invalid Session" +#[derive(Error, Debug)] +pub enum AuthError { + #[error("No Session Cookie Found")] + NoSessionCookie, + + #[error("Invalid Session")] + InvalidSession, } const SESSION_COOKIE: &str = "session";