From 0b8e41a8ecca1ee8f565478f7b275b8228df123c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 3 Jun 2024 07:38:59 +0100 Subject: [PATCH] refactor: config: use thiserror and move tests about --- Cargo.toml | 1 + crates/config/Cargo.toml | 1 + crates/config/src/server.rs | 17 ++++++----- crates/config/src/{tests/mod.rs => tests.rs} | 0 crates/config/src/webhook/auth.rs | 30 +------------------- crates/config/src/webhook/mod.rs | 3 ++ crates/config/src/webhook/tests.rs | 25 ++++++++++++++++ 7 files changed, 41 insertions(+), 36 deletions(-) rename crates/config/src/{tests/mod.rs => tests.rs} (100%) create mode 100644 crates/config/src/webhook/tests.rs diff --git a/Cargo.toml b/Cargo.toml index d4c2a118..d6c0192c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,7 @@ derive_more = { version = "1.0.0-beta.6", features = [ "from", ] } derive-with = "0.5" +thiserror = "1.0" # file watcher inotify = "0.10" diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml index 6e4e5282..11d47e7d 100644 --- a/crates/config/Cargo.toml +++ b/crates/config/Cargo.toml @@ -44,6 +44,7 @@ ulid = { workspace = true } # boilerplate derive_more = { workspace = true } derive-with = { workspace = true } +thiserror = { workspace = true } # # # file watcher # inotify = { workspace = true } diff --git a/crates/config/src/server.rs b/crates/config/src/server.rs index 7dc43f40..f08f83f5 100644 --- a/crates/config/src/server.rs +++ b/crates/config/src/server.rs @@ -13,14 +13,17 @@ use tracing::info; use crate::{ForgeAlias, ForgeConfig, RepoAlias}; -#[derive(Debug, derive_more::From, derive_more::Display)] +#[derive(Debug, thiserror::Error)] pub enum Error { - Io(std::io::Error), - KxIoFs(kxio::fs::Error), - TomlDe(toml::de::Error), - AddressParse(std::net::AddrParseError), + #[error("fs: {0}")] + KxioFs(#[from] kxio::fs::Error), + + #[error("deserialise toml: {0}")] + TomlDe(#[from] toml::de::Error), + + #[error("parse IP addres/port: {0}")] + AddressParse(#[from] std::net::AddrParseError), } -impl std::error::Error for Error {} type Result = core::result::Result; @@ -39,7 +42,7 @@ impl ServerConfig { let file = fs.base().join("git-next-server.toml"); info!(?file, ""); let str = fs.file_read_to_string(&file)?; - toml::from_str(&str).map_err(Into::into) + Ok(toml::from_str(&str)?) } pub fn forges(&self) -> impl Iterator { diff --git a/crates/config/src/tests/mod.rs b/crates/config/src/tests.rs similarity index 100% rename from crates/config/src/tests/mod.rs rename to crates/config/src/tests.rs diff --git a/crates/config/src/webhook/auth.rs b/crates/config/src/webhook/auth.rs index ab619b8d..2e4e1ffc 100644 --- a/crates/config/src/webhook/auth.rs +++ b/crates/config/src/webhook/auth.rs @@ -1,9 +1,8 @@ -use std::str::FromStr as _; - #[derive(Clone, Debug, PartialEq, Eq, derive_more::Deref, derive_more::Display)] pub struct WebhookAuth(ulid::Ulid); impl WebhookAuth { pub fn new(authorisation: &str) -> Result { + use std::str::FromStr as _; let id = ulid::Ulid::from_str(authorisation)?; tracing::info!("Parse auth token: {}", id); Ok(Self(id)) @@ -21,30 +20,3 @@ impl WebhookAuth { self.0.to_bytes() } } - -#[cfg(test)] -mod tests { - use crate::WebhookAuth; - - #[test] - fn bytes() -> Result<(), Box> { - let ulid = ulid::Ulid::new(); - - let wa = WebhookAuth::new(ulid.to_string().as_str())?; - - assert_eq!(ulid.to_bytes(), wa.to_bytes()); - - Ok(()) - } - - #[test] - fn string() -> Result<(), Box> { - let ulid = ulid::Ulid::new(); - - let wa = WebhookAuth::new(ulid.to_string().as_str())?; - - assert_eq!(ulid.to_string(), wa.to_string()); - - Ok(()) - } -} diff --git a/crates/config/src/webhook/mod.rs b/crates/config/src/webhook/mod.rs index 34295c07..91de1c79 100644 --- a/crates/config/src/webhook/mod.rs +++ b/crates/config/src/webhook/mod.rs @@ -3,4 +3,7 @@ pub mod id; pub mod message; pub mod push; +#[cfg(test)] +mod tests; + pub use push::Push; diff --git a/crates/config/src/webhook/tests.rs b/crates/config/src/webhook/tests.rs new file mode 100644 index 00000000..c7e1df44 --- /dev/null +++ b/crates/config/src/webhook/tests.rs @@ -0,0 +1,25 @@ +mod auth { + use crate::WebhookAuth; + + #[test] + fn bytes() -> Result<(), Box> { + let ulid = ulid::Ulid::new(); + + let wa = WebhookAuth::new(ulid.to_string().as_str())?; + + assert_eq!(ulid.to_bytes(), wa.to_bytes()); + + Ok(()) + } + + #[test] + fn string() -> Result<(), Box> { + let ulid = ulid::Ulid::new(); + + let wa = WebhookAuth::new(ulid.to_string().as_str())?; + + assert_eq!(ulid.to_string(), wa.to_string()); + + Ok(()) + } +}