From 2008afa4dd256d6796bf60203185f0fb66694c16 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 29 Jun 2024 10:49:12 +0100 Subject: [PATCH] refactor: extract actor-macros crate --- Cargo.toml | 2 ++ crates/actor-macros/Cargo.toml | 31 +++++++++++++++++++ crates/actor-macros/src/lib.rs | 1 + crates/actor-macros/src/message.rs | 27 ++++++++++++++++ crates/repo-actor/Cargo.toml | 1 + crates/repo-actor/src/messages.rs | 29 +---------------- .../src/tests/handlers/loaded_config.rs | 20 +++--------- crates/repo-actor/src/tests/mod.rs | 3 +- crates/server/Cargo.toml | 1 + crates/server/src/actors/messages.rs | 3 -- crates/server/src/actors/server.rs | 17 +++++----- 11 files changed, 80 insertions(+), 55 deletions(-) create mode 100644 crates/actor-macros/Cargo.toml create mode 100644 crates/actor-macros/src/lib.rs create mode 100644 crates/actor-macros/src/message.rs diff --git a/Cargo.toml b/Cargo.toml index c752d80..65e6c68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ members = [ "crates/forge-github", "crates/repo-actor", "crates/webhook-actor", + "crates/actor-macros", ] [workspace.package] @@ -31,6 +32,7 @@ git-next-forge-forgejo = { path = "crates/forge-forgejo" } git-next-forge-github = { path = "crates/forge-github" } git-next-repo-actor = { path = "crates/repo-actor" } git-next-webhook-actor = { path = "crates/webhook-actor" } +git-next-actor-macros = { path = "crates/actor-macros" } # CLI parsing clap = { version = "4.5", features = ["cargo", "derive"] } diff --git a/crates/actor-macros/Cargo.toml b/crates/actor-macros/Cargo.toml new file mode 100644 index 0000000..616995d --- /dev/null +++ b/crates/actor-macros/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "git-next-actor-macros" +version = { workspace = true } +edition = { workspace = true } + +[dependencies] +# git-next-config = { workspace = true } +# git-next-repo-actor = { workspace = true } +# +# # logging +# tracing = { workspace = true } +# +# # Webhooks +# bytes = { workspace = true } +# warp = { workspace = true } +# +# # boilerplate +# derive_more = { workspace = true } + +# Actors +actix = { workspace = true } + +[dev-dependencies] +# Testing +# assert2 = { workspace = true } + +[lints.clippy] +nursery = { level = "warn", priority = -1 } +# pedantic = "warn" +unwrap_used = "warn" +expect_used = "warn" diff --git a/crates/actor-macros/src/lib.rs b/crates/actor-macros/src/lib.rs new file mode 100644 index 0000000..e935b02 --- /dev/null +++ b/crates/actor-macros/src/lib.rs @@ -0,0 +1 @@ +mod message; diff --git a/crates/actor-macros/src/message.rs b/crates/actor-macros/src/message.rs new file mode 100644 index 0000000..4a8e784 --- /dev/null +++ b/crates/actor-macros/src/message.rs @@ -0,0 +1,27 @@ +#[macro_export] +macro_rules! message { + ($name:ident wraps $value:ty) => { + git_next_config::newtype!($name is a $value); + impl actix::prelude::Message for $name { + type Result = (); + } + }; + ($name:ident) => { + git_next_config::newtype!($name); + impl actix::prelude::Message for $name { + type Result = (); + } + }; + ($name:ident wraps $value:ty => $result:ty) => { + git_next_config::newtype!($name is a $value); + impl actix::prelude::Message for $name { + type Result = $result; + } + }; + ($name:ident => $result:ty) => { + git_next_config::newtype!($name); + impl actix::prelude::Message for $name { + type Result = $result; + } + }; +} diff --git a/crates/repo-actor/Cargo.toml b/crates/repo-actor/Cargo.toml index 8473cb4..254603c 100644 --- a/crates/repo-actor/Cargo.toml +++ b/crates/repo-actor/Cargo.toml @@ -12,6 +12,7 @@ github = [] git-next-config = { workspace = true } git-next-git = { workspace = true } git-next-forge = { workspace = true } +git-next-actor-macros = { workspace = true } # logging tracing = { workspace = true } diff --git a/crates/repo-actor/src/messages.rs b/crates/repo-actor/src/messages.rs index 69bbc18..7f47589 100644 --- a/crates/repo-actor/src/messages.rs +++ b/crates/repo-actor/src/messages.rs @@ -2,37 +2,10 @@ use config::newtype; use derive_more::Display; +use git_next_actor_macros::message; use git_next_config as config; use git_next_git as git; -#[macro_export] -macro_rules! message { - ($name:ident wraps $value:ty) => { - git_next_config::newtype!($name is a $value); - impl actix::prelude::Message for $name { - type Result = (); - } - }; - ($name:ident) => { - git_next_config::newtype!($name); - impl actix::prelude::Message for $name { - type Result = (); - } - }; - ($name:ident wraps $value:ty => $result:ty) => { - git_next_config::newtype!($name is a $value); - impl actix::prelude::Message for $name { - type Result = $result; - } - }; - ($name:ident => $result:ty) => { - git_next_config::newtype!($name); - impl actix::prelude::Message for $name { - type Result = $result; - } - }; -} - message!(LoadConfigFromRepo); message!(CloneRepo); message!(ReceiveRepoConfig wraps config::RepoConfig); diff --git a/crates/repo-actor/src/tests/handlers/loaded_config.rs b/crates/repo-actor/src/tests/handlers/loaded_config.rs index 3f1f8bc..6447ec4 100644 --- a/crates/repo-actor/src/tests/handlers/loaded_config.rs +++ b/crates/repo-actor/src/tests/handlers/loaded_config.rs @@ -24,8 +24,11 @@ async fn should_store_repo_config_in_actor() -> TestResult { //then tracing::debug!(?log, ""); - let repo_config = addr.send(TakeRepoConfig).await?; - assert_eq!(repo_config, Some(new_repo_config)); + let reo_actor_view = addr.send(ExamineActor).await?; + assert_eq!( + reo_actor_view.repo_details.repo_config, + Some(new_repo_config) + ); Ok(()) } @@ -88,16 +91,3 @@ async fn should_register_webhook() -> TestResult { })?; Ok(()) } - -actor::message!(TakeRepoConfig => Option); - -impl Handler for actor::RepoActor { - type Result = Option; - - fn handle(&mut self, _msg: TakeRepoConfig, _ctx: &mut Self::Context) -> Self::Result { - tracing::debug!("TakeRepoConfig >>"); - let repo_config = self.repo_details.repo_config.take(); - tracing::debug!(?repo_config, "TakeRepoConfig <<"); - repo_config - } -} diff --git a/crates/repo-actor/src/tests/mod.rs b/crates/repo-actor/src/tests/mod.rs index 8d41620..bb0938c 100644 --- a/crates/repo-actor/src/tests/mod.rs +++ b/crates/repo-actor/src/tests/mod.rs @@ -1,5 +1,5 @@ // -use crate::{self as actor, message}; +use crate::{self as actor}; use actix::prelude::*; use actor::{ messages::{CloneRepo, MessageToken}, @@ -16,6 +16,7 @@ use git::{ repository::{open::MockOpenRepositoryLike, Direction, MockRepositoryFactory}, Generation, GitRemote, MockForgeLike, RepoDetails, }; +use git_next_actor_macros::message; use git_next_config as config; use git_next_git as git; use mockall::predicate::eq; diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index 488c11e..1338b73 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -8,6 +8,7 @@ git-next-config = { workspace = true } git-next-git = { workspace = true } git-next-forge = { workspace = true } git-next-repo-actor = { workspace = true } +git-next-actor-macros = { workspace = true } git-next-webhook-actor = { workspace = true } # logging diff --git a/crates/server/src/actors/messages.rs b/crates/server/src/actors/messages.rs index d1db605..8b13789 100644 --- a/crates/server/src/actors/messages.rs +++ b/crates/server/src/actors/messages.rs @@ -1,4 +1 @@ -use git_next_config::server::ServerConfig; -use git_next_repo_actor::message; -message!(ReceiveServerConfig wraps ServerConfig); diff --git a/crates/server/src/actors/server.rs b/crates/server/src/actors/server.rs index b6567a9..75bcb00 100644 --- a/crates/server/src/actors/server.rs +++ b/crates/server/src/actors/server.rs @@ -1,21 +1,22 @@ // use std::path::PathBuf; +use crate::actors::file_watcher::FileUpdated; use actix::prelude::*; - -use crate::actors::{file_watcher::FileUpdated, messages::ReceiveServerConfig}; use config::server::{ServerConfig, ServerStorage, Webhook}; -use git_next_config::{ - self as config, ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig, -}; -use git_next_git::{repository::RepositoryFactory, Generation, RepoDetails}; -use git_next_repo_actor::messages::CloneRepo; -use git_next_repo_actor::RepoActor; +use config::{ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig}; +use git::{repository::RepositoryFactory, Generation, RepoDetails}; +use git_next_actor_macros::message; +use git_next_config as config; +use git_next_git as git; +use git_next_repo_actor::{messages::CloneRepo, RepoActor}; use git_next_webhook_actor as webhook; use kxio::{fs::FileSystem, network::Network}; use tracing::{error, info, warn}; use webhook::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter}; +message!(ReceiveServerConfig wraps ServerConfig); + #[derive(Debug, derive_more::Display, derive_more::From)] pub enum Error { #[display("Failed to create data directories")]