From 5e9f9eb80ff9e645576a73854a63b437d97731cf Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 16 Jun 2024 08:00:43 +0100 Subject: [PATCH] refactor: start to use newtype macro --- crates/config/src/branch_name.rs | 15 ++------------- crates/config/src/forge_name.rs | 11 +++-------- crates/config/src/git_dir.rs | 18 ++---------------- crates/config/src/host_name.rs | 10 ++-------- crates/config/src/lib.rs | 1 + crates/config/src/tests.rs | 12 +++++++----- crates/forge-forgejo/src/tests.rs | 8 ++++---- crates/git/src/repository/open/tests.rs | 2 +- crates/git/src/tests.rs | 8 ++++---- 9 files changed, 26 insertions(+), 59 deletions(-) diff --git a/crates/config/src/branch_name.rs b/crates/config/src/branch_name.rs index 92a5bd5..136f460 100644 --- a/crates/config/src/branch_name.rs +++ b/crates/config/src/branch_name.rs @@ -1,13 +1,2 @@ -use derive_more::Display; - -/// The name of a Branch -#[derive(Clone, Default, Debug, Hash, PartialEq, Eq, Display, PartialOrd, Ord)] -pub struct BranchName(String); -impl BranchName { - pub fn new(str: impl Into) -> Self { - Self(str.into()) - } - pub fn into_string(self) -> String { - self.0 - } -} +// The name of a Branch +crate::newtype!(BranchName is a String); diff --git a/crates/config/src/forge_name.rs b/crates/config/src/forge_name.rs index db29d9a..1cb9892 100644 --- a/crates/config/src/forge_name.rs +++ b/crates/config/src/forge_name.rs @@ -1,11 +1,6 @@ -use std::path::PathBuf; - -use derive_more::{AsRef, Constructor, Display}; - -/// The name of a Forge to connect to -#[derive(Clone, Default, Debug, Hash, PartialEq, Eq, Constructor, Display, AsRef)] -pub struct ForgeAlias(String); -impl From<&ForgeAlias> for PathBuf { +// The name of a Forge to connect to +crate::newtype!(ForgeAlias is a String); +impl From<&ForgeAlias> for std::path::PathBuf { fn from(value: &ForgeAlias) -> Self { Self::from(&value.0) } diff --git a/crates/config/src/git_dir.rs b/crates/config/src/git_dir.rs index 3c9aa86..9716c01 100644 --- a/crates/config/src/git_dir.rs +++ b/crates/config/src/git_dir.rs @@ -1,22 +1,8 @@ +// use std::path::PathBuf; -#[derive( - Clone, - Default, - Debug, - Hash, - PartialEq, - Eq, - serde::Deserialize, - derive_more::Deref, - derive_more::From, -)] -pub struct GitDir(PathBuf); +crate::newtype!(GitDir is a PathBuf, without Display); impl GitDir { - pub fn new(path: &std::path::Path) -> Self { - Self(path.to_path_buf()) - } - pub const fn pathbuf(&self) -> &PathBuf { &self.0 } diff --git a/crates/config/src/host_name.rs b/crates/config/src/host_name.rs index cb2967a..ca69be4 100644 --- a/crates/config/src/host_name.rs +++ b/crates/config/src/host_name.rs @@ -1,8 +1,2 @@ -/// The hostname of a forge -#[derive(Clone, Default, Debug, PartialEq, Eq, derive_more::Display)] -pub struct Hostname(String); -impl Hostname { - pub fn new(str: impl Into) -> Self { - Self(str.into()) - } -} +// The hostname of a forge +crate::newtype!(Hostname is a String); diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 89776dc..260dee2 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -1,4 +1,5 @@ // + mod api_token; mod branch_name; pub mod common; diff --git a/crates/config/src/tests.rs b/crates/config/src/tests.rs index 4a47c47..62e0ded 100644 --- a/crates/config/src/tests.rs +++ b/crates/config/src/tests.rs @@ -86,7 +86,7 @@ mod server_repo_config { assert_eq!( src.gitdir(), - Some(GitDir::new(&PathBuf::default().join(gitdir))) + Some(GitDir::new(PathBuf::default().join(gitdir))) ); } } @@ -396,14 +396,14 @@ mod gitdir { let pathbuf = PathBuf::default().join(given::a_name()); let gitdir = GitDir::new(&pathbuf); - let result = gitdir.pathbuf(); + let result = gitdir.unwrap(); - assert_eq!(result, &pathbuf); + assert_eq!(result, pathbuf); } #[test] fn should_display() { let pathbuf = PathBuf::default().join("foo"); - let gitdir = GitDir::new(&pathbuf); + let gitdir = GitDir::new(pathbuf); let result = gitdir.to_string(); @@ -415,7 +415,7 @@ mod gitdir { let pathbuf = PathBuf::default().join(path.clone()); let gitdir: GitDir = path.as_str().into(); - assert_eq!(gitdir, GitDir::new(&pathbuf)); + assert_eq!(gitdir, GitDir::new(pathbuf)); } #[test] fn should_convert_to_pathbuf_from_ref() { @@ -520,6 +520,8 @@ mod server { let branch = server_repo_config.branch(); let gitdir = server_repo_config .gitdir() + .map(|gitdir| gitdir.unwrap()) + .map(|pathbuf| pathbuf.display().to_string()) .map(|gitdir| format!(r#", gitdir = "{gitdir}" "#)) .unwrap_or_default(); let repo_server_config = server_repo_config diff --git a/crates/forge-forgejo/src/tests.rs b/crates/forge-forgejo/src/tests.rs index cf10a6d..80cd51a 100644 --- a/crates/forge-forgejo/src/tests.rs +++ b/crates/forge-forgejo/src/tests.rs @@ -733,7 +733,7 @@ mod forgejo { pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> config::GitDir { let dir_name = a_name(); let dir = fs.base().join(dir_name); - config::GitDir::new(&dir) + config::GitDir::new(dir) } pub fn a_forge_config() -> config::ForgeConfig { @@ -747,9 +747,9 @@ mod forgejo { } pub fn a_server_repo_config() -> config::ServerRepoConfig { - let main = a_branch_name().into_string(); - let next = a_branch_name().into_string(); - let dev = a_branch_name().into_string(); + let main = a_branch_name().unwrap(); + let next = a_branch_name().unwrap(); + let dev = a_branch_name().unwrap(); config::ServerRepoConfig::new( format!("{}/{}", a_name(), a_name()), main.clone(), diff --git a/crates/git/src/repository/open/tests.rs b/crates/git/src/repository/open/tests.rs index 4500173..65988b3 100644 --- a/crates/git/src/repository/open/tests.rs +++ b/crates/git/src/repository/open/tests.rs @@ -92,7 +92,7 @@ mod server_repo_config { assert_eq!( src.gitdir(), - Some(config::GitDir::new(&PathBuf::default().join(gitdir))) + Some(config::GitDir::new(PathBuf::default().join(gitdir))) ); } } diff --git a/crates/git/src/tests.rs b/crates/git/src/tests.rs index f54c0cb..40dbb13 100644 --- a/crates/git/src/tests.rs +++ b/crates/git/src/tests.rs @@ -266,7 +266,7 @@ pub mod given { pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir { let dir_name = a_name(); let dir = fs.base().join(dir_name); - GitDir::new(&dir) + GitDir::new(dir) } pub fn a_forge_config() -> ForgeConfig { @@ -280,9 +280,9 @@ pub mod given { } pub fn a_server_repo_config() -> ServerRepoConfig { - let main = a_branch_name().into_string(); - let next = a_branch_name().into_string(); - let dev = a_branch_name().into_string(); + let main = a_branch_name().unwrap(); + let next = a_branch_name().unwrap(); + let dev = a_branch_name().unwrap(); ServerRepoConfig::new( format!("{}/{}", a_name(), a_name()), main.clone(),