From 8c1968005641677d50f80768f1a5dca9b861d69a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 6 Aug 2024 20:06:39 +0100 Subject: [PATCH] refactor: macros use a more common syntax Parameters were separated by ':', but are now separated by ','. --- crates/cli/src/alerts/messages.rs | 8 ++- crates/cli/src/repo/messages.rs | 94 +++++++++++++++++++------ crates/cli/src/repo/tests/mod.rs | 2 +- crates/cli/src/server/actor/messages.rs | 16 +++-- crates/cli/src/webhook/messages.rs | 2 +- crates/core/src/config/branch_name.rs | 10 ++- crates/core/src/config/forge_alias.rs | 14 +++- crates/core/src/config/host_name.rs | 12 +++- crates/core/src/config/remote_url.rs | 10 ++- crates/core/src/config/repo_alias.rs | 14 +++- crates/core/src/config/server.rs | 17 +++-- crates/core/src/config/webhook/auth.rs | 9 ++- crates/core/src/config/webhook/id.rs | 9 ++- crates/core/src/git/commit.rs | 22 +++++- crates/core/src/git/generation.rs | 11 ++- crates/core/src/git/git_ref.rs | 2 +- crates/core/src/macros/message.rs | 16 ++--- crates/core/src/macros/newtype.rs | 4 +- 18 files changed, 215 insertions(+), 57 deletions(-) diff --git a/crates/cli/src/alerts/messages.rs b/crates/cli/src/alerts/messages.rs index 79d0fd3..409d2ce 100644 --- a/crates/cli/src/alerts/messages.rs +++ b/crates/cli/src/alerts/messages.rs @@ -1,6 +1,10 @@ // use git_next_core::{git::UserNotification, message, server::Shout}; -message!(UpdateShout: Shout: "Updated Shout configuration"); +message!(UpdateShout, Shout, "Updated Shout configuration"); -message!(NotifyUser: UserNotification: "Request to send the message payload to the notification webhook"); +message!( + NotifyUser, + UserNotification, + "Request to send the message payload to the notification webhook" +); diff --git a/crates/cli/src/repo/messages.rs b/crates/cli/src/repo/messages.rs index 0a2ccf3..5a9fa86 100644 --- a/crates/cli/src/repo/messages.rs +++ b/crates/cli/src/repo/messages.rs @@ -6,22 +6,37 @@ use git_next_core::{ message, newtype, ForgeNotification, RegisteredWebhook, RepoConfig, WebhookAuth, WebhookId, }; -message!(LoadConfigFromRepo: "Request to load the `git-next.toml` from the git repo."); -message!(CloneRepo: "Request to clone (or open) the git repo."); -message!(ReceiveRepoConfig: RepoConfig: r#"Notification that the `git-next.toml` file has been loaded from the repo and parsed. +message!( + LoadConfigFromRepo, + "Request to load the `git-next.toml` from the git repo." +); +message!(CloneRepo, "Request to clone (or open) the git repo."); +message!( + ReceiveRepoConfig, + RepoConfig, + r#"Notification that the `git-next.toml` file has been loaded from the repo and parsed. -Contains the parsed contents of the `git-next.toml` file."#); -message!(ValidateRepo: MessageToken: r#"Request that the state of the branches in the git repo be assessed and generate any followup actions. +Contains the parsed contents of the `git-next.toml` file."# +); +message!( + ValidateRepo, + MessageToken, + r#"Request that the state of the branches in the git repo be assessed and generate any followup actions. This is the main function of `git-next` where decisions are made on what branches need to be updated and when. Contains a [MessageToken] to reduce duplicate messages being sent. Only messages with the latest [MessageToken] are handled, -all others are dropped."#); +all others are dropped."# +); -message!(WebhookRegistered: (WebhookId, WebhookAuth): r#"Notification that a webhook has been registered with a forge. +message!( + WebhookRegistered, + (WebhookId, WebhookAuth), + r#"Notification that a webhook has been registered with a forge. Contains a tuple of the ID for the webhook returned from the forge, and the unique authorisation token that -incoming messages from the forge must provide."#); +incoming messages from the forge must provide."# +); impl WebhookRegistered { pub const fn webhook_id(&self) -> &WebhookId { &self.0 .0 @@ -38,28 +53,51 @@ impl From for WebhookRegistered { } } -message!(UnRegisterWebhook: "Request that the webhook be removed from the forge, so they will stop notifying us."); +message!( + UnRegisterWebhook, + "Request that the webhook be removed from the forge, so they will stop notifying us." +); -newtype!(MessageToken: u32, Copy, Default, Display, PartialOrd, Ord: r#"An incremental token used to identify the current set of messages. +newtype!( + MessageToken, + u32, + Copy, + Default, + Display, + PartialOrd, + Ord, + r#"An incremental token used to identify the current set of messages. Primarily used by [ValidateRepo] to reduce duplicate messages. The token is incremented when a new Webhook message is received, marking that message the latest, and causing any previous messages still being processed to be dropped when -they next send a [ValidateRepo] message."#); +they next send a [ValidateRepo] message."# +); impl MessageToken { pub const fn next(self) -> Self { Self(self.0 + 1) } } -message!(RegisterWebhook: "Requests that a Webhook be registered with the forge."); -message!(CheckCIStatus: Commit: r#"Requests that the CI status for the commit be checked. +message!( + RegisterWebhook, + "Requests that a Webhook be registered with the forge." +); +message!( + CheckCIStatus, + Commit, + r#"Requests that the CI status for the commit be checked. Once the CI Status has been received it will be sent via a [ReceiveCIStatus] message. -Contains the commit from the tip of the `next` branch."#); // next commit -message!(ReceiveCIStatus: (Commit, Status): r#"Notification of the status of the CI checks for the commit. +Contains the commit from the tip of the `next` branch."# +); // next commit +message!( + ReceiveCIStatus, + (Commit, Status), + r#"Notification of the status of the CI checks for the commit. -Contains a tuple of the commit that was checked (the tip of the `next` branch) and the status."#); // commit and it's status +Contains a tuple of the commit that was checked (the tip of the `next` branch) and the status."# +); // commit and it's status #[derive(Clone, Debug, PartialEq, Eq)] pub struct AdvanceNextPayload { @@ -67,7 +105,23 @@ pub struct AdvanceNextPayload { pub main: Commit, pub dev_commit_history: Vec, } -message!(AdvanceNext: AdvanceNextPayload: "Request to advance the `next` branch on to the next commit on the `dev branch."); // next commit and the dev commit history -message!(AdvanceMain: Commit: "Request to advance the `main` branch on to same commit as the `next` branch."); // next commit -message!(WebhookNotification: ForgeNotification: "Notification of a webhook message from the forge."); -message!(NotifyUser: UserNotification: "Request to send the message payload to the notification webhook"); +message!( + AdvanceNext, + AdvanceNextPayload, + "Request to advance the `next` branch on to the next commit on the `dev branch." +); // next commit and the dev commit history +message!( + AdvanceMain, + Commit, + "Request to advance the `main` branch on to same commit as the `next` branch." +); // next commit +message!( + WebhookNotification, + ForgeNotification, + "Notification of a webhook message from the forge." +); +message!( + NotifyUser, + UserNotification, + "Request to send the message payload to the notification webhook" +); diff --git a/crates/cli/src/repo/tests/mod.rs b/crates/cli/src/repo/tests/mod.rs index 5781b77..9249721 100644 --- a/crates/cli/src/repo/tests/mod.rs +++ b/crates/cli/src/repo/tests/mod.rs @@ -78,7 +78,7 @@ impl ActorLog { } } -message!(ExamineActor => RepoActorView: "Request a view of the current state of the [RepoActor]."); +message!(ExamineActor => RepoActorView, "Request a view of the current state of the [RepoActor]."); impl Handler for RepoActor { type Result = RepoActorView; diff --git a/crates/cli/src/server/actor/messages.rs b/crates/cli/src/server/actor/messages.rs index a046e15..1a7e1dd 100644 --- a/crates/cli/src/server/actor/messages.rs +++ b/crates/cli/src/server/actor/messages.rs @@ -9,11 +9,15 @@ use git_next_core::{ use std::net::SocketAddr; // receive server config -message!(ReceiveAppConfig: AppConfig: "Notification of newly loaded server configuration. +message!( + ReceiveAppConfig, + AppConfig, + "Notification of newly loaded server configuration. This message will prompt the `git-next` server to stop and restart all repo-actors. -Contains the new server configuration."); +Contains the new server configuration." +); // receive valid server config #[derive(Clone, Debug, PartialEq, Eq, Constructor)] @@ -22,6 +26,10 @@ pub struct ValidAppConfig { pub socket_address: SocketAddr, pub storage: Storage, } -message!(ReceiveValidAppConfig: ValidAppConfig: "Notification of validated server configuration."); +message!( + ReceiveValidAppConfig, + ValidAppConfig, + "Notification of validated server configuration." +); -message!(Shutdown: "Notification to shutdown the server actor"); +message!(Shutdown, "Notification to shutdown the server actor"); diff --git a/crates/cli/src/webhook/messages.rs b/crates/cli/src/webhook/messages.rs index 886414a..74add75 100644 --- a/crates/cli/src/webhook/messages.rs +++ b/crates/cli/src/webhook/messages.rs @@ -1,4 +1,4 @@ // use git_next_core::message; -message!(ShutdownWebhook: "Request to shutdown the Webhook actor"); +message!(ShutdownWebhook, "Request to shutdown the Webhook actor"); diff --git a/crates/core/src/config/branch_name.rs b/crates/core/src/config/branch_name.rs index 694a33c..83b628f 100644 --- a/crates/core/src/config/branch_name.rs +++ b/crates/core/src/config/branch_name.rs @@ -3,4 +3,12 @@ use serde::Serialize; use crate::newtype; -newtype!(BranchName: String, Display, Default, Hash, Serialize: "The name of a Git branch"); +newtype!( + BranchName, + String, + Display, + Default, + Hash, + Serialize, + "The name of a Git branch" +); diff --git a/crates/core/src/config/forge_alias.rs b/crates/core/src/config/forge_alias.rs index f27a7a8..1a8444c 100644 --- a/crates/core/src/config/forge_alias.rs +++ b/crates/core/src/config/forge_alias.rs @@ -1,6 +1,18 @@ use serde::Serialize; -crate::newtype!(ForgeAlias: String, Hash, PartialOrd, Ord, derive_more::Display, Default, Serialize: "The name of a Forge to connect to"); +use crate::newtype; + +newtype!( + ForgeAlias, + String, + Hash, + PartialOrd, + Ord, + derive_more::Display, + Default, + Serialize, + "The name of a Forge to connect to" +); impl From<&ForgeAlias> for std::path::PathBuf { fn from(value: &ForgeAlias) -> Self { Self::from(&value.0) diff --git a/crates/core/src/config/host_name.rs b/crates/core/src/config/host_name.rs index 3f1bb89..b819b14 100644 --- a/crates/core/src/config/host_name.rs +++ b/crates/core/src/config/host_name.rs @@ -1,2 +1,10 @@ -// The hostname of a forge -crate::newtype!(Hostname: String, derive_more::Display, Default: "The hostname for the forge"); +// +use crate::newtype; + +newtype!( + Hostname, + String, + derive_more::Display, + Default, + "The hostname for the forge" +); diff --git a/crates/core/src/config/remote_url.rs b/crates/core/src/config/remote_url.rs index 5d41611..d43ece6 100644 --- a/crates/core/src/config/remote_url.rs +++ b/crates/core/src/config/remote_url.rs @@ -1,6 +1,14 @@ +// use std::borrow::ToOwned; -crate::newtype!(RemoteUrl: git_url_parse::GitUrl, derive_more::Display: "The URL of a remote repository"); +use crate::newtype; + +newtype!( + RemoteUrl, + git_url_parse::GitUrl, + derive_more::Display, + "The URL of a remote repository" +); impl RemoteUrl { pub fn parse(url: impl Into) -> Option { Some(Self::new(::git_url_parse::GitUrl::parse(&url.into()).ok()?)) diff --git a/crates/core/src/config/repo_alias.rs b/crates/core/src/config/repo_alias.rs index 67bfaac..a2d9820 100644 --- a/crates/core/src/config/repo_alias.rs +++ b/crates/core/src/config/repo_alias.rs @@ -3,6 +3,16 @@ use serde::Serialize; use crate::newtype; -newtype!(RepoAlias: String, Display, Default, Hash, PartialOrd, Ord, Serialize: r#"The alias of a repo. +newtype!( + RepoAlias, + String, + Display, + Default, + Hash, + PartialOrd, + Ord, + Serialize, + r#"The alias of a repo. -This is the alias for the repo within `git-next-server.toml`."#); +This is the alias for the repo within `git-next-server.toml`."# +); diff --git a/crates/core/src/config/server.rs b/crates/core/src/config/server.rs index acf4a8c..0561303 100644 --- a/crates/core/src/config/server.rs +++ b/crates/core/src/config/server.rs @@ -122,8 +122,13 @@ impl Listen { } newtype!( - ListenUrl: - String, Serialize, Deserialize, PartialOrd, Ord, Display: + ListenUrl, + String, + Serialize, + Deserialize, + PartialOrd, + Ord, + Display, "The base url for receiving all webhooks from all forges" ); impl ListenUrl { @@ -133,9 +138,13 @@ impl ListenUrl { } } -newtype!(ForgeWebhookUrl: String: "Raw URL from a forge Webhook"); +newtype!(ForgeWebhookUrl, String, "Raw URL from a forge Webhook"); -newtype!(RepoListenUrl: (ListenUrl, ForgeAlias, RepoAlias): "URL to listen for webhook from forges"); +newtype!( + RepoListenUrl, + (ListenUrl, ForgeAlias, RepoAlias), + "URL to listen for webhook from forges" +); impl Display for RepoListenUrl { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( diff --git a/crates/core/src/config/webhook/auth.rs b/crates/core/src/config/webhook/auth.rs index a001257..4d07fda 100644 --- a/crates/core/src/config/webhook/auth.rs +++ b/crates/core/src/config/webhook/auth.rs @@ -1,9 +1,14 @@ // use crate::newtype; -newtype!(WebhookAuth: ulid::Ulid, derive_more::Display: r#"The unique token authorisation for the webhook. +newtype!( + WebhookAuth, + ulid::Ulid, + derive_more::Display, + r#"The unique token authorisation for the webhook. -Each monitored repository has it's own unique token, and it is different each time `git-next` runs."#); +Each monitored repository has it's own unique token, and it is different each time `git-next` runs."# +); impl WebhookAuth { /// Parses the authorisation string as a `Ulid` to create a `WebhookAuth`. /// diff --git a/crates/core/src/config/webhook/id.rs b/crates/core/src/config/webhook/id.rs index d197944..04cfda4 100644 --- a/crates/core/src/config/webhook/id.rs +++ b/crates/core/src/config/webhook/id.rs @@ -1,2 +1,9 @@ // -crate::newtype!(WebhookId: String, derive_more::Display: "The ID of the webhook, as returned by the forge when it is registered."); +use crate::newtype; + +newtype!( + WebhookId, + String, + derive_more::Display, + "The ID of the webhook, as returned by the forge when it is registered." +); diff --git a/crates/core/src/git/commit.rs b/crates/core/src/git/commit.rs index f1d25ba..99b821d 100644 --- a/crates/core/src/git/commit.rs +++ b/crates/core/src/git/commit.rs @@ -41,8 +41,26 @@ impl From for Commit { } } -newtype!(Sha: String, Display, Hash,PartialOrd, Ord, Serialize: "The unique SHA for a git commit."); -newtype!(Message: String, Display, Hash, PartialOrd, Ord, Serialize: "The commit message for a git commit."); +newtype!( + Sha, + String, + Display, + Hash, + PartialOrd, + Ord, + Serialize, + "The unique SHA for a git commit." +); +newtype!( + Message, + String, + Display, + Hash, + PartialOrd, + Ord, + Serialize, + "The commit message for a git commit." +); #[derive(Clone, Debug)] pub struct Histories { diff --git a/crates/core/src/git/generation.rs b/crates/core/src/git/generation.rs index 05b9dcf..afecfca 100644 --- a/crates/core/src/git/generation.rs +++ b/crates/core/src/git/generation.rs @@ -2,9 +2,16 @@ use derive_more::Display; use crate::newtype; -newtype!(Generation: u32, Display, Default, Copy: r#"A counter for the server generation. +newtype!( + Generation, + u32, + Display, + Default, + Copy, + r#"A counter for the server generation. -This counter is increased by one each time the server restarts itself when the configuration file is updated."#); +This counter is increased by one each time the server restarts itself when the configuration file is updated."# +); impl Generation { pub fn inc(&mut self) { self.0 += 1; diff --git a/crates/core/src/git/git_ref.rs b/crates/core/src/git/git_ref.rs index 5a7c3a6..7548962 100644 --- a/crates/core/src/git/git_ref.rs +++ b/crates/core/src/git/git_ref.rs @@ -5,7 +5,7 @@ use derive_more::Display; use crate::git::{commit::Sha, Commit}; -newtype!(GitRef: String, Display: "A git reference to a git commit."); +newtype!(GitRef, String, Display, "A git reference to a git commit."); impl From for GitRef { fn from(value: Commit) -> Self { Self(value.sha().to_string()) diff --git a/crates/core/src/macros/message.rs b/crates/core/src/macros/message.rs index e1df53c..cc3ad7c 100644 --- a/crates/core/src/macros/message.rs +++ b/crates/core/src/macros/message.rs @@ -1,25 +1,25 @@ #[macro_export] macro_rules! message { - ($name:ident: $value:ty: $docs:literal) => { - git_next_core::newtype!($name: $value: $docs); + ($name:ident, $value:ty, $docs:literal) => { + git_next_core::newtype!($name, $value, $docs); impl actix::prelude::Message for $name { type Result = (); } }; - ($name:ident: $docs:literal) => { - git_next_core::newtype!($name: $docs); + ($name:ident, $docs:literal) => { + git_next_core::newtype!($name, $docs); impl actix::prelude::Message for $name { type Result = (); } }; - ($name:ident: $value:ty => $result:ty: $docs:literal) => { - git_next_core::newtype!($name is a $value: $docs); + ($name:ident, $value:ty => $result:ty, $docs:literal) => { + git_next_core::newtype!($name, $value, $docs); impl actix::prelude::Message for $name { type Result = $result; } }; - ($name:ident => $result:ty: $docs:literal) => { - git_next_core::newtype!($name: $docs); + ($name:ident => $result:ty, $docs:literal) => { + git_next_core::newtype!($name, $docs); impl actix::prelude::Message for $name { type Result = $result; } diff --git a/crates/core/src/macros/newtype.rs b/crates/core/src/macros/newtype.rs index 6c7d065..8704798 100644 --- a/crates/core/src/macros/newtype.rs +++ b/crates/core/src/macros/newtype.rs @@ -1,7 +1,7 @@ // #[macro_export] macro_rules! newtype { - ($name:ident: $docs:literal) => { + ($name:ident, $docs:literal) => { #[doc = $docs] #[derive( Clone, @@ -20,7 +20,7 @@ macro_rules! newtype { )] pub struct $name; }; - ($name:ident: $type:ty $(, $derive:ty)*: $docs:literal) => { + ($name:ident, $type:ty $(, $derive:ty)*, $docs:literal) => { #[doc = $docs] #[derive( Clone,