From 2cdaf39c0f0bd2ba1997faa141bbe24489591d0e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 20 Jun 2024 19:03:11 +0100 Subject: [PATCH] refactor: git: use newtype --- crates/forge-forgejo/src/tests.rs | 2 +- crates/forge-github/src/tests/mod.rs | 2 +- crates/forge/src/tests.rs | 2 +- crates/git/src/commit.rs | 21 +++++++++++++++------ crates/git/src/generation.rs | 9 ++++----- crates/git/src/git_ref.rs | 6 +++--- crates/git/src/tests.rs | 8 ++++---- crates/server/src/actors/server.rs | 2 +- crates/server/src/config/tests.rs | 6 +++--- 9 files changed, 33 insertions(+), 25 deletions(-) diff --git a/crates/forge-forgejo/src/tests.rs b/crates/forge-forgejo/src/tests.rs index 80cd51a..f803f06 100644 --- a/crates/forge-forgejo/src/tests.rs +++ b/crates/forge-forgejo/src/tests.rs @@ -777,7 +777,7 @@ mod forgejo { } pub fn repo_details(fs: &kxio::fs::FileSystem) -> git::RepoDetails { - let generation = git::Generation::new(); + let generation = git::Generation::default(); let repo_alias = a_repo_alias(); let server_repo_config = a_server_repo_config(); let forge_alias = a_forge_alias(); diff --git a/crates/forge-github/src/tests/mod.rs b/crates/forge-github/src/tests/mod.rs index 403b9b6..0ca6995 100644 --- a/crates/forge-github/src/tests/mod.rs +++ b/crates/forge-github/src/tests/mod.rs @@ -636,7 +636,7 @@ mod github { } pub fn repo_details() -> git::RepoDetails { git::RepoDetails::new( - git::Generation::new(), + git::Generation::default(), &a_repo_alias(), &ServerRepoConfig::new( format!("{}/{}", a_name(), a_name()), // repo path: owner/repo diff --git a/crates/forge/src/tests.rs b/crates/forge/src/tests.rs index 62f60e1..bf6a9cb 100644 --- a/crates/forge/src/tests.rs +++ b/crates/forge/src/tests.rs @@ -39,7 +39,7 @@ fn given_repo_details(forge_type: config::ForgeType) -> RepoDetails { let fs = given_fs(); git::common::repo_details( 1, - git::Generation::new(), + git::Generation::default(), config::common::forge_details(1, forge_type), Some(config::common::repo_config( 1, diff --git a/crates/git/src/commit.rs b/crates/git/src/commit.rs index 39f4c6c..c7172db 100644 --- a/crates/git/src/commit.rs +++ b/crates/git/src/commit.rs @@ -1,7 +1,19 @@ +use config::newtype; +use derive_more::Display; // use git_next_config as config; -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] +#[derive( + Clone, + Debug, + Hash, + PartialEq, + Eq, + PartialOrd, + Ord, + derive_more::Constructor, + derive_more::Display, +)] #[display("{}", sha)] pub struct Commit { sha: Sha, @@ -25,11 +37,8 @@ impl From for Commit { } } -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] -pub struct Sha(String); - -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] -pub struct Message(String); +newtype!(Sha is a String, Display); +newtype!(Message is a String); #[derive(Clone, Debug)] pub struct Histories { diff --git a/crates/git/src/generation.rs b/crates/git/src/generation.rs index e5a445c..05cdbec 100644 --- a/crates/git/src/generation.rs +++ b/crates/git/src/generation.rs @@ -1,9 +1,8 @@ -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] -pub struct Generation(u32); +use derive_more::Display; +use git_next_config::newtype; + +newtype!(Generation is a u32, Display, Default, Copy); impl Generation { - pub fn new() -> Self { - Self::default() - } pub fn inc(&mut self) { self.0 += 1 } diff --git a/crates/git/src/git_ref.rs b/crates/git/src/git_ref.rs index 450e8a1..6cc3764 100644 --- a/crates/git/src/git_ref.rs +++ b/crates/git/src/git_ref.rs @@ -1,11 +1,11 @@ // use crate as git; -use derive_more::Constructor; +use derive_more::Display; +use git_next_config::newtype; use crate::Commit; -#[derive(Clone, Constructor, Debug, Hash, PartialEq, Eq, derive_more::Display)] -pub struct GitRef(String); +newtype!(GitRef is a String, Display); impl From for GitRef { fn from(value: Commit) -> Self { Self(value.sha().to_string()) diff --git a/crates/git/src/tests.rs b/crates/git/src/tests.rs index 35dee26..a668cea 100644 --- a/crates/git/src/tests.rs +++ b/crates/git/src/tests.rs @@ -40,7 +40,7 @@ mod generation { #[test] fn should_increment() { - let mut g = Generation::new(); + let mut g = Generation::default(); assert_eq!(g.to_string(), "0"); g.inc(); @@ -153,7 +153,7 @@ mod repo_details { #[test] fn should_return_origin() { let rd = RepoDetails::new( - Generation::new(), + Generation::default(), &RepoAlias::new("foo"), &ServerRepoConfig::new( "repo".to_string(), @@ -182,7 +182,7 @@ mod repo_details { #[test] fn should_return_git_remote() { let rd = RepoDetails::new( - Generation::new(), + Generation::default(), &RepoAlias::new("foo"), &ServerRepoConfig::new( "user/repo".to_string(), @@ -327,7 +327,7 @@ pub mod given { } pub fn repo_details(fs: &kxio::fs::FileSystem) -> git::RepoDetails { - let generation = git::Generation::new(); + let generation = git::Generation::default(); let repo_alias = a_repo_alias(); let server_repo_config = a_server_repo_config(); let forge_alias = a_forge_alias(); diff --git a/crates/server/src/actors/server.rs b/crates/server/src/actors/server.rs index 8a1f549..6929c66 100644 --- a/crates/server/src/actors/server.rs +++ b/crates/server/src/actors/server.rs @@ -115,7 +115,7 @@ impl Handler for Server { } impl Server { pub fn new(fs: FileSystem, net: Network, repo: Box) -> Self { - let generation = Generation::new(); + let generation = Generation::default(); Self { generation, webhook: None, diff --git a/crates/server/src/config/tests.rs b/crates/server/src/config/tests.rs index 8d2a241..98f60d9 100644 --- a/crates/server/src/config/tests.rs +++ b/crates/server/src/config/tests.rs @@ -50,7 +50,7 @@ fn repo_details_find_default_push_remote_finds_correct_remote() -> Result<()> { let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent())); let mut repo_details = git::common::repo_details( 1, - git::Generation::new(), + git::Generation::default(), config::common::forge_details(1, ForgeType::MockForge), None, GitDir::new(root), // Server GitDir - should be ignored @@ -81,7 +81,7 @@ fn gitdir_validate_should_pass_a_valid_git_repo() -> Result<()> { let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent())); let mut repo_details = git::common::repo_details( 1, - git::Generation::new(), + git::Generation::default(), config::common::forge_details(1, ForgeType::MockForge), None, GitDir::new(root), // Server GitDir - should be ignored @@ -105,7 +105,7 @@ fn gitdir_validate_should_fail_a_git_repo_with_wrong_remote() -> Result<()> { let_assert!(Some(Some(root)) = cli_crate_dir.parent().map(|p| p.parent())); let repo_details = git::common::repo_details( 1, - git::Generation::new(), + git::Generation::default(), config::common::forge_details(1, ForgeType::MockForge), None, GitDir::new(root), // Server GitDir - should be ignored