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 bdbcba5..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; @@ -8,6 +9,7 @@ mod forge_name; mod forge_type; pub mod git_dir; mod host_name; +mod newtype; mod registered_webhook; mod repo_alias; mod repo_branches; diff --git a/crates/config/src/newtype.rs b/crates/config/src/newtype.rs new file mode 100644 index 0000000..df2ef6c --- /dev/null +++ b/crates/config/src/newtype.rs @@ -0,0 +1,57 @@ +// +#[macro_export] +macro_rules! newtype { + ($name:ident is a $type:ty, without Display) => { + #[derive( + Clone, + Default, + Debug, + derive_more::From, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + derive_more::AsRef, + derive_more::Deref, + serde::Deserialize, + serde::Serialize, + )] + pub struct $name($type); + impl $name { + pub fn new(value: impl Into<$type>) -> Self { + Self(value.into()) + } + pub fn unwrap(self) -> $type { + self.0 + } + } + }; + ($name:ident is a $type:ty) => { + #[derive( + Clone, + Default, + Debug, + derive_more::Display, + derive_more::From, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + derive_more::AsRef, + derive_more::Deref, + serde::Deserialize, + serde::Serialize, + )] + pub struct $name($type); + impl $name { + pub fn new(value: impl Into<$type>) -> Self { + Self(value.into()) + } + pub fn unwrap(self) -> $type { + self.0 + } + } + }; +} diff --git a/crates/config/src/tests.rs b/crates/config/src/tests.rs index a532641..397d524 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 46d7476..1afedd6 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(), diff --git a/crates/repo-actor/src/tests.rs b/crates/repo-actor/src/tests.rs index 819b01b..739c425 100644 --- a/crates/repo-actor/src/tests.rs +++ b/crates/repo-actor/src/tests.rs @@ -631,7 +631,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 { @@ -645,9 +645,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(),