refactor: config: use thiserror and move tests about
All checks were successful
Rust / build (push) Successful in 1m14s
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

This commit is contained in:
Paul Campbell 2024-06-03 07:38:59 +01:00
parent 235aee8b11
commit 0b8e41a8ec
7 changed files with 41 additions and 36 deletions

View file

@ -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"

View file

@ -44,6 +44,7 @@ ulid = { workspace = true }
# boilerplate
derive_more = { workspace = true }
derive-with = { workspace = true }
thiserror = { workspace = true }
#
# # file watcher
# inotify = { workspace = true }

View file

@ -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<T> = core::result::Result<T, Error>;
@ -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<Item = (ForgeAlias, &ForgeConfig)> {

View file

@ -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<Self, ulid::DecodeError> {
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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let ulid = ulid::Ulid::new();
let wa = WebhookAuth::new(ulid.to_string().as_str())?;
assert_eq!(ulid.to_string(), wa.to_string());
Ok(())
}
}

View file

@ -3,4 +3,7 @@ pub mod id;
pub mod message;
pub mod push;
#[cfg(test)]
mod tests;
pub use push::Push;

View file

@ -0,0 +1,25 @@
mod auth {
use crate::WebhookAuth;
#[test]
fn bytes() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let ulid = ulid::Ulid::new();
let wa = WebhookAuth::new(ulid.to_string().as_str())?;
assert_eq!(ulid.to_string(), wa.to_string());
Ok(())
}
}