From dda599259685e5f5026ec24d69b303090746653d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 15 May 2024 21:01:19 +0100 Subject: [PATCH] refactor(server): more derive_more replacing boilerplate --- crates/server/src/actors/file_watcher.rs | 22 +++--------- crates/server/src/actors/repo/mod.rs | 40 ++++++++------------- crates/server/src/actors/repo/webhook.rs | 31 +++------------- crates/server/src/actors/webhook/message.rs | 27 +------------- crates/server/src/actors/webhook/mod.rs | 6 ++-- crates/server/src/config/mod.rs | 7 +--- crates/server/src/gitforge/forgejo/mod.rs | 7 +--- crates/server/src/types.rs | 7 +--- 8 files changed, 29 insertions(+), 118 deletions(-) diff --git a/crates/server/src/actors/file_watcher.rs b/crates/server/src/actors/file_watcher.rs index 8d616f7..4ad2e92 100644 --- a/crates/server/src/actors/file_watcher.rs +++ b/crates/server/src/actors/file_watcher.rs @@ -7,17 +7,13 @@ use tracing::{debug, info}; const CHECK_INTERVAL: Duration = Duration::from_secs(1); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Message)] +#[rtype(result = "()")] pub struct WatchFile; -impl Message for WatchFile { - type Result = (); -} -#[derive(Debug)] +#[derive(Debug, Message)] +#[rtype(result = "()")] pub struct FileUpdated; -impl Message for FileUpdated { - type Result = (); -} #[derive(Debug, derive_more::From, derive_more::Display)] pub enum Error { @@ -72,13 +68,3 @@ impl Handler for FileWatcher { } } } -// impl Handler for FileWatcher { -// type Result = anyhow::Result<()>; -// -// fn handle(&mut self, _msg: Stop, ctx: &mut Self::Context) -> Self::Result { -// warn!("Stopping file watcher actor"); -// self.run_interval.take(); -// ctx.stop(); -// Ok(()) -// } -// } diff --git a/crates/server/src/actors/repo/mod.rs b/crates/server/src/actors/repo/mod.rs index a6ca2e6..e6689f7 100644 --- a/crates/server/src/actors/repo/mod.rs +++ b/crates/server/src/actors/repo/mod.rs @@ -16,6 +16,8 @@ use crate::{actors::repo::webhook::WebhookAuth, config::Webhook, gitforge, types use self::webhook::WebhookId; +#[derive(Debug, derive_more::Display)] +#[display("{}:{}:{}", generation, details.forge.forge_name(), details.repo_alias)] pub struct RepoActor { generation: Generation, message_token: MessageToken, @@ -79,17 +81,6 @@ impl Actor for RepoActor { } } } -impl std::fmt::Display for RepoActor { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}:{}:{}", - self.generation, - self.details.forge.forge_name(), - self.details.repo_alias - ) - } -} #[derive(Message)] #[rtype(result = "()")] @@ -136,7 +127,7 @@ impl Handler for RepoActor { #[derive(Message)] #[rtype(result = "()")] -struct LoadedConfig(pub RepoConfig); +struct LoadedConfig(RepoConfig); impl Handler for RepoActor { type Result = (); #[tracing::instrument(name = "RepoActor::LoadedConfig", skip_all, fields(repo = %self.details, branches = %msg.0))] @@ -151,16 +142,11 @@ impl Handler for RepoActor { } } -#[derive(Message)] +#[derive(derive_more::Constructor, Message)] #[rtype(result = "()")] pub struct ValidateRepo { message_token: MessageToken, } -impl ValidateRepo { - pub const fn new(message_token: MessageToken) -> Self { - Self { message_token } - } -} impl Handler for RepoActor { type Result = (); #[tracing::instrument(name = "RepoActor::ValidateRepo", skip_all, fields(repo = %self.details, token = %msg.message_token))] @@ -208,17 +194,19 @@ impl Handler for RepoActor { } } -#[derive(Debug, Message)] +#[derive(Debug, derive_more::Constructor, Message)] #[rtype(result = "()")] pub struct StartMonitoring { - pub main: git::Commit, - pub next: git::Commit, - pub dev: git::Commit, - pub dev_commit_history: Vec, + main: git::Commit, + next: git::Commit, + dev: git::Commit, + dev_commit_history: Vec, } impl Handler for RepoActor { type Result = (); - #[tracing::instrument(name = "RepoActor::StartMonitoring", skip_all, fields(token = %self.message_token, repo = %self.details, main = %msg.main, next= %msg.next, dev = %msg.dev))] + #[tracing::instrument(name = "RepoActor::StartMonitoring", skip_all, + fields(token = %self.message_token, repo = %self.details, main = %msg.main, next= %msg.next, dev = %msg.dev)) + ] fn handle(&mut self, msg: StartMonitoring, ctx: &mut Self::Context) -> Self::Result { info!("Message Received"); let Some(repo_config) = self.details.repo_config.clone() else { @@ -259,7 +247,7 @@ impl Handler for RepoActor { #[derive(Message)] #[rtype(result = "()")] -pub struct WebhookRegistered(pub WebhookId, pub WebhookAuth); +pub struct WebhookRegistered(WebhookId, WebhookAuth); impl Handler for RepoActor { type Result = (); #[tracing::instrument(name = "RepoActor::WebhookRegistered", skip_all, fields(repo = %self.details, webhook_id = %msg.0))] @@ -272,7 +260,7 @@ impl Handler for RepoActor { #[derive(Message)] #[rtype(result = "()")] -pub struct AdvanceMainTo(pub git::Commit); +pub struct AdvanceMainTo(git::Commit); impl Handler for RepoActor { type Result = (); #[tracing::instrument(name = "RepoActor::AdvanceMainTo", skip_all, fields(repo = %self.details, commit = %msg.0))] diff --git a/crates/server/src/actors/repo/webhook.rs b/crates/server/src/actors/repo/webhook.rs index 33d569d..28affc6 100644 --- a/crates/server/src/actors/repo/webhook.rs +++ b/crates/server/src/actors/repo/webhook.rs @@ -5,7 +5,7 @@ use kxio::network::{self, json}; use tracing::{debug, info, warn}; use ulid::DecodeError; -use std::{collections::HashMap, fmt::Display, ops::Deref, str::FromStr}; +use std::{collections::HashMap, str::FromStr}; use crate::{ actors::{ @@ -15,27 +15,12 @@ use crate::{ config::{Webhook, WebhookUrl}, }; -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive( + Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Deref, derive_more::Display, +)] pub struct WebhookId(String); -impl WebhookId { - #[allow(dead_code)] - pub const fn new(id: String) -> Self { - Self(id) - } -} -impl Deref for WebhookId { - type Target = String; - fn deref(&self) -> &Self::Target { - &self.0 - } -} -impl Display for WebhookId { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Deref)] pub struct WebhookAuth(ulid::Ulid); impl WebhookAuth { pub fn from_str(authorisation: &str) -> Result { @@ -51,12 +36,6 @@ impl WebhookAuth { format!("Basic {}", self.0.to_string()) } } -impl Deref for WebhookAuth { - type Target = ulid::Ulid; - fn deref(&self) -> &Self::Target { - &self.0 - } -} #[tracing::instrument(skip_all, fields(%webhook_id))] pub async fn unregister(webhook_id: WebhookId, repo_details: RepoDetails, net: network::Network) { diff --git a/crates/server/src/actors/webhook/message.rs b/crates/server/src/actors/webhook/message.rs index e719fe2..92551a9 100644 --- a/crates/server/src/actors/webhook/message.rs +++ b/crates/server/src/actors/webhook/message.rs @@ -3,46 +3,21 @@ use actix::prelude::*; use crate::actors::repo::webhook::WebhookAuth; -#[derive(Message, Debug, Clone)] +#[derive(Message, Debug, Clone, derive_more::Constructor)] #[rtype(result = "()")] pub struct WebhookMessage { id: String, path: String, authorisation: String, - // query: String, - // headers: warp::http::HeaderMap, body: String, } impl WebhookMessage { - pub const fn new( - id: String, - path: String, - // query: String, - // headers: warp::http::HeaderMap, - body: String, - authorisation: String, - ) -> Self { - Self { - id, - path, - // query, - // headers, - body, - authorisation, - } - } pub const fn id(&self) -> &String { &self.id } pub const fn path(&self) -> &String { &self.path } - // pub const fn query(&self) -> &String { - // &self.query - // } - // pub const fn headers(&self) -> &warp::http::HeaderMap { - // &self.headers - // } pub const fn body(&self) -> &String { &self.body } diff --git a/crates/server/src/actors/webhook/mod.rs b/crates/server/src/actors/webhook/mod.rs index 4a76cbb..c9d42d8 100644 --- a/crates/server/src/actors/webhook/mod.rs +++ b/crates/server/src/actors/webhook/mod.rs @@ -42,11 +42,9 @@ impl Actor for WebhookActor { } } -#[derive(Debug)] +#[derive(Debug, Message)] +#[rtype(result = "()")] pub struct ShutdownWebhook; -impl Message for ShutdownWebhook { - type Result = (); -} impl Handler for WebhookActor { type Result = (); diff --git a/crates/server/src/config/mod.rs b/crates/server/src/config/mod.rs index 552dc4f..83ef965 100644 --- a/crates/server/src/config/mod.rs +++ b/crates/server/src/config/mod.rs @@ -89,13 +89,8 @@ impl Webhook { } /// The URL for the webhook where forges should send their updates -#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::AsRef)] pub struct WebhookUrl(String); -impl AsRef for WebhookUrl { - fn as_ref(&self) -> &str { - &self.0 - } -} /// The directory to store server data, such as cloned repos #[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize)] diff --git a/crates/server/src/gitforge/forgejo/mod.rs b/crates/server/src/gitforge/forgejo/mod.rs index bfb821f..764e1a5 100644 --- a/crates/server/src/gitforge/forgejo/mod.rs +++ b/crates/server/src/gitforge/forgejo/mod.rs @@ -59,12 +59,7 @@ impl super::ForgeLike for ForgeJoEnv { dev, dev_commit_history, }) => { - addr.do_send(StartMonitoring { - main, - next, - dev, - dev_commit_history, - }); + addr.do_send(StartMonitoring::new(main, next, dev, dev_commit_history)); } Err(err) => { warn!("{}", err); diff --git a/crates/server/src/types.rs b/crates/server/src/types.rs index 49b6725..370ef11 100644 --- a/crates/server/src/types.rs +++ b/crates/server/src/types.rs @@ -1,4 +1,4 @@ -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] pub struct MessageToken(u32); impl MessageToken { pub fn new() -> Self { @@ -8,8 +8,3 @@ impl MessageToken { Self(self.0 + 1) } } -impl std::fmt::Display for MessageToken { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -}