refactor: extract actor-macros crate

This commit is contained in:
Paul Campbell 2024-06-29 10:49:12 +01:00
parent eba00a112f
commit 2008afa4dd
11 changed files with 80 additions and 55 deletions

View file

@ -10,6 +10,7 @@ members = [
"crates/forge-github", "crates/forge-github",
"crates/repo-actor", "crates/repo-actor",
"crates/webhook-actor", "crates/webhook-actor",
"crates/actor-macros",
] ]
[workspace.package] [workspace.package]
@ -31,6 +32,7 @@ git-next-forge-forgejo = { path = "crates/forge-forgejo" }
git-next-forge-github = { path = "crates/forge-github" } git-next-forge-github = { path = "crates/forge-github" }
git-next-repo-actor = { path = "crates/repo-actor" } git-next-repo-actor = { path = "crates/repo-actor" }
git-next-webhook-actor = { path = "crates/webhook-actor" } git-next-webhook-actor = { path = "crates/webhook-actor" }
git-next-actor-macros = { path = "crates/actor-macros" }
# CLI parsing # CLI parsing
clap = { version = "4.5", features = ["cargo", "derive"] } clap = { version = "4.5", features = ["cargo", "derive"] }

View file

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

View file

@ -0,0 +1 @@
mod message;

View file

@ -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;
}
};
}

View file

@ -12,6 +12,7 @@ github = []
git-next-config = { workspace = true } git-next-config = { workspace = true }
git-next-git = { workspace = true } git-next-git = { workspace = true }
git-next-forge = { workspace = true } git-next-forge = { workspace = true }
git-next-actor-macros = { workspace = true }
# logging # logging
tracing = { workspace = true } tracing = { workspace = true }

View file

@ -2,37 +2,10 @@
use config::newtype; use config::newtype;
use derive_more::Display; use derive_more::Display;
use git_next_actor_macros::message;
use git_next_config as config; use git_next_config as config;
use git_next_git as git; 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!(LoadConfigFromRepo);
message!(CloneRepo); message!(CloneRepo);
message!(ReceiveRepoConfig wraps config::RepoConfig); message!(ReceiveRepoConfig wraps config::RepoConfig);

View file

@ -24,8 +24,11 @@ async fn should_store_repo_config_in_actor() -> TestResult {
//then //then
tracing::debug!(?log, ""); tracing::debug!(?log, "");
let repo_config = addr.send(TakeRepoConfig).await?; let reo_actor_view = addr.send(ExamineActor).await?;
assert_eq!(repo_config, Some(new_repo_config)); assert_eq!(
reo_actor_view.repo_details.repo_config,
Some(new_repo_config)
);
Ok(()) Ok(())
} }
@ -88,16 +91,3 @@ async fn should_register_webhook() -> TestResult {
})?; })?;
Ok(()) Ok(())
} }
actor::message!(TakeRepoConfig => Option<RepoConfig>);
impl Handler<TakeRepoConfig> for actor::RepoActor {
type Result = Option<RepoConfig>;
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
}
}

View file

@ -1,5 +1,5 @@
// //
use crate::{self as actor, message}; use crate::{self as actor};
use actix::prelude::*; use actix::prelude::*;
use actor::{ use actor::{
messages::{CloneRepo, MessageToken}, messages::{CloneRepo, MessageToken},
@ -16,6 +16,7 @@ use git::{
repository::{open::MockOpenRepositoryLike, Direction, MockRepositoryFactory}, repository::{open::MockOpenRepositoryLike, Direction, MockRepositoryFactory},
Generation, GitRemote, MockForgeLike, RepoDetails, Generation, GitRemote, MockForgeLike, RepoDetails,
}; };
use git_next_actor_macros::message;
use git_next_config as config; use git_next_config as config;
use git_next_git as git; use git_next_git as git;
use mockall::predicate::eq; use mockall::predicate::eq;

View file

@ -8,6 +8,7 @@ git-next-config = { workspace = true }
git-next-git = { workspace = true } git-next-git = { workspace = true }
git-next-forge = { workspace = true } git-next-forge = { workspace = true }
git-next-repo-actor = { workspace = true } git-next-repo-actor = { workspace = true }
git-next-actor-macros = { workspace = true }
git-next-webhook-actor = { workspace = true } git-next-webhook-actor = { workspace = true }
# logging # logging

View file

@ -1,4 +1 @@
use git_next_config::server::ServerConfig;
use git_next_repo_actor::message;
message!(ReceiveServerConfig wraps ServerConfig);

View file

@ -1,21 +1,22 @@
// //
use std::path::PathBuf; use std::path::PathBuf;
use crate::actors::file_watcher::FileUpdated;
use actix::prelude::*; use actix::prelude::*;
use crate::actors::{file_watcher::FileUpdated, messages::ReceiveServerConfig};
use config::server::{ServerConfig, ServerStorage, Webhook}; use config::server::{ServerConfig, ServerStorage, Webhook};
use git_next_config::{ use config::{ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig};
self as config, ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig, use git::{repository::RepositoryFactory, Generation, RepoDetails};
}; use git_next_actor_macros::message;
use git_next_git::{repository::RepositoryFactory, Generation, RepoDetails}; use git_next_config as config;
use git_next_repo_actor::messages::CloneRepo; use git_next_git as git;
use git_next_repo_actor::RepoActor; use git_next_repo_actor::{messages::CloneRepo, RepoActor};
use git_next_webhook_actor as webhook; use git_next_webhook_actor as webhook;
use kxio::{fs::FileSystem, network::Network}; use kxio::{fs::FileSystem, network::Network};
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use webhook::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter}; use webhook::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter};
message!(ReceiveServerConfig wraps ServerConfig);
#[derive(Debug, derive_more::Display, derive_more::From)] #[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error { pub enum Error {
#[display("Failed to create data directories")] #[display("Failed to create data directories")]