refactor: start to use newtype macro

This commit is contained in:
Paul Campbell 2024-06-16 08:00:43 +01:00
parent 2e71e40378
commit 5e9f9eb80f
9 changed files with 26 additions and 59 deletions

View file

@ -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<String>) -> Self {
Self(str.into())
}
pub fn into_string(self) -> String {
self.0
}
}
// The name of a Branch
crate::newtype!(BranchName is a String);

View file

@ -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)
}

View file

@ -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
}

View file

@ -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<String>) -> Self {
Self(str.into())
}
}
// The hostname of a forge
crate::newtype!(Hostname is a String);

View file

@ -1,4 +1,5 @@
//
mod api_token;
mod branch_name;
pub mod common;

View file

@ -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

View file

@ -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(),

View file

@ -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)))
);
}
}

View file

@ -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(),