From 84af6da22f3de9914911ea89376f8b9ed84cbb95 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 15 May 2024 08:29:41 +0100 Subject: [PATCH] WIP: refactor(git): more derive_more replacing boilerplate --- crates/git/src/commit.rs | 22 +++---------------- crates/git/src/git_ref.rs | 2 +- crates/git/src/git_remote.rs | 5 +---- crates/server/src/actors/repo/tests.rs | 20 +++++++++++++---- crates/server/src/actors/repo/webhook.rs | 5 ++++- .../forgejo/branch/validate_positions.rs | 5 ++++- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/crates/git/src/commit.rs b/crates/git/src/commit.rs index 9605352..1347b4e 100644 --- a/crates/git/src/commit.rs +++ b/crates/git/src/commit.rs @@ -1,16 +1,10 @@ -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] #[display("{}", sha)] pub struct Commit { sha: Sha, message: Message, } impl Commit { - pub fn new(sha: &str, message: &str) -> Self { - Self { - sha: Sha::new(sha.to_string()), - message: Message::new(message.to_string()), - } - } pub const fn sha(&self) -> &Sha { &self.sha } @@ -19,18 +13,8 @@ impl Commit { } } -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] pub struct Sha(String); -impl Sha { - pub const fn new(value: String) -> Self { - Self(value) - } -} -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] pub struct Message(String); -impl Message { - pub const fn new(value: String) -> Self { - Self(value) - } -} diff --git a/crates/git/src/git_ref.rs b/crates/git/src/git_ref.rs index 047ec6b..8746208 100644 --- a/crates/git/src/git_ref.rs +++ b/crates/git/src/git_ref.rs @@ -1,7 +1,7 @@ use crate::Commit; #[derive(Clone, Debug, Hash, PartialEq, Eq, derive_more::Display)] -pub struct GitRef(pub String); +pub struct GitRef(String); impl From for GitRef { fn from(value: Commit) -> Self { Self(value.sha().to_string()) diff --git a/crates/git/src/git_remote.rs b/crates/git/src/git_remote.rs index 926380f..8f383be 100644 --- a/crates/git/src/git_remote.rs +++ b/crates/git/src/git_remote.rs @@ -1,15 +1,12 @@ use git_next_config::{Hostname, RepoPath}; -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] #[display("{}:{}", host, repo_path)] pub struct GitRemote { host: Hostname, repo_path: RepoPath, } impl GitRemote { - pub const fn new(host: Hostname, repo_path: RepoPath) -> Self { - Self { host, repo_path } - } pub const fn host(&self) -> &Hostname { &self.host } diff --git a/crates/server/src/actors/repo/tests.rs b/crates/server/src/actors/repo/tests.rs index dc75c13..463b368 100644 --- a/crates/server/src/actors/repo/tests.rs +++ b/crates/server/src/actors/repo/tests.rs @@ -6,13 +6,25 @@ mod branch { #[actix_rt::test] async fn test_find_next_commit_on_dev() { - let next = git::Commit::new("current-next", "foo"); - let expected = git::Commit::new("dev-next", "next-should-go-here"); + let next = git::Commit::new( + git::commit::Sha::new("current-next".to_string()), + git::commit::Message::new("foo".to_string()), + ); + let expected = git::Commit::new( + git::commit::Sha::new("dev-next".to_string()), + git::commit::Message::new("next-should-go-here".to_string()), + ); let dev_commit_history = vec![ - git::Commit::new("dev", "future"), + git::Commit::new( + git::commit::Sha::new("dev".to_string()), + git::commit::Message::new("future".to_string()), + ), expected.clone(), next.clone(), - git::Commit::new("current-main", "history"), + git::Commit::new( + git::commit::Sha::new("current-main".to_string()), + git::commit::Message::new("history".to_string()), + ), ]; let next_commit = find_next_commit_on_dev(next, dev_commit_history); diff --git a/crates/server/src/actors/repo/webhook.rs b/crates/server/src/actors/repo/webhook.rs index 4c6808e..33d569d 100644 --- a/crates/server/src/actors/repo/webhook.rs +++ b/crates/server/src/actors/repo/webhook.rs @@ -301,7 +301,10 @@ impl Push { } pub fn commit(&self) -> git::Commit { - git::Commit::new(&self.after, &self.head_commit.message) + git::Commit::new( + git::commit::Sha::new(self.after.clone()), + git::commit::Message::new(self.head_commit.message.clone()), + ) } } diff --git a/crates/server/src/gitforge/forgejo/branch/validate_positions.rs b/crates/server/src/gitforge/forgejo/branch/validate_positions.rs index 2a6d61d..e3abfb9 100644 --- a/crates/server/src/gitforge/forgejo/branch/validate_positions.rs +++ b/crates/server/src/gitforge/forgejo/branch/validate_positions.rs @@ -254,6 +254,9 @@ struct RepoCommit { } impl From for git::Commit { fn from(value: Commit) -> Self { - Self::new(&value.sha, &value.commit.message) + Self::new( + git::commit::Sha::new(value.sha), + git::commit::Message::new(value.commit.message), + ) } }