refactor: start to use newtype macro
All checks were successful
Rust / build (push) Successful in 1m21s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful

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
crate::newtype!(BranchName is a String);
/// 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
}
}

View file

@ -1,11 +1,6 @@
use std::path::PathBuf; // The name of a Forge to connect to
crate::newtype!(ForgeAlias is a String);
use derive_more::{AsRef, Constructor, Display}; impl From<&ForgeAlias> for std::path::PathBuf {
/// 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 {
fn from(value: &ForgeAlias) -> Self { fn from(value: &ForgeAlias) -> Self {
Self::from(&value.0) Self::from(&value.0)
} }

View file

@ -1,22 +1,8 @@
//
use std::path::PathBuf; use std::path::PathBuf;
#[derive( crate::newtype!(GitDir is a PathBuf, without Display);
Clone,
Default,
Debug,
Hash,
PartialEq,
Eq,
serde::Deserialize,
derive_more::Deref,
derive_more::From,
)]
pub struct GitDir(PathBuf);
impl GitDir { impl GitDir {
pub fn new(path: &std::path::Path) -> Self {
Self(path.to_path_buf())
}
pub const fn pathbuf(&self) -> &PathBuf { pub const fn pathbuf(&self) -> &PathBuf {
&self.0 &self.0
} }

View file

@ -1,8 +1,2 @@
/// The hostname of a forge // The hostname of a forge
#[derive(Clone, Default, Debug, PartialEq, Eq, derive_more::Display)] crate::newtype!(Hostname is a String);
pub struct Hostname(String);
impl Hostname {
pub fn new(str: impl Into<String>) -> Self {
Self(str.into())
}
}

View file

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

View file

@ -86,7 +86,7 @@ mod server_repo_config {
assert_eq!( assert_eq!(
src.gitdir(), 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 pathbuf = PathBuf::default().join(given::a_name());
let gitdir = GitDir::new(&pathbuf); let gitdir = GitDir::new(&pathbuf);
let result = gitdir.pathbuf(); let result = gitdir.unwrap();
assert_eq!(result, &pathbuf); assert_eq!(result, pathbuf);
} }
#[test] #[test]
fn should_display() { fn should_display() {
let pathbuf = PathBuf::default().join("foo"); let pathbuf = PathBuf::default().join("foo");
let gitdir = GitDir::new(&pathbuf); let gitdir = GitDir::new(pathbuf);
let result = gitdir.to_string(); let result = gitdir.to_string();
@ -415,7 +415,7 @@ mod gitdir {
let pathbuf = PathBuf::default().join(path.clone()); let pathbuf = PathBuf::default().join(path.clone());
let gitdir: GitDir = path.as_str().into(); let gitdir: GitDir = path.as_str().into();
assert_eq!(gitdir, GitDir::new(&pathbuf)); assert_eq!(gitdir, GitDir::new(pathbuf));
} }
#[test] #[test]
fn should_convert_to_pathbuf_from_ref() { fn should_convert_to_pathbuf_from_ref() {
@ -520,6 +520,8 @@ mod server {
let branch = server_repo_config.branch(); let branch = server_repo_config.branch();
let gitdir = server_repo_config let gitdir = server_repo_config
.gitdir() .gitdir()
.map(|gitdir| gitdir.unwrap())
.map(|pathbuf| pathbuf.display().to_string())
.map(|gitdir| format!(r#", gitdir = "{gitdir}" "#)) .map(|gitdir| format!(r#", gitdir = "{gitdir}" "#))
.unwrap_or_default(); .unwrap_or_default();
let repo_server_config = server_repo_config 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 { pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> config::GitDir {
let dir_name = a_name(); let dir_name = a_name();
let dir = fs.base().join(dir_name); let dir = fs.base().join(dir_name);
config::GitDir::new(&dir) config::GitDir::new(dir)
} }
pub fn a_forge_config() -> config::ForgeConfig { pub fn a_forge_config() -> config::ForgeConfig {
@ -747,9 +747,9 @@ mod forgejo {
} }
pub fn a_server_repo_config() -> config::ServerRepoConfig { pub fn a_server_repo_config() -> config::ServerRepoConfig {
let main = a_branch_name().into_string(); let main = a_branch_name().unwrap();
let next = a_branch_name().into_string(); let next = a_branch_name().unwrap();
let dev = a_branch_name().into_string(); let dev = a_branch_name().unwrap();
config::ServerRepoConfig::new( config::ServerRepoConfig::new(
format!("{}/{}", a_name(), a_name()), format!("{}/{}", a_name(), a_name()),
main.clone(), main.clone(),

View file

@ -92,7 +92,7 @@ mod server_repo_config {
assert_eq!( assert_eq!(
src.gitdir(), 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 { pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir {
let dir_name = a_name(); let dir_name = a_name();
let dir = fs.base().join(dir_name); let dir = fs.base().join(dir_name);
GitDir::new(&dir) GitDir::new(dir)
} }
pub fn a_forge_config() -> ForgeConfig { pub fn a_forge_config() -> ForgeConfig {
@ -280,9 +280,9 @@ pub mod given {
} }
pub fn a_server_repo_config() -> ServerRepoConfig { pub fn a_server_repo_config() -> ServerRepoConfig {
let main = a_branch_name().into_string(); let main = a_branch_name().unwrap();
let next = a_branch_name().into_string(); let next = a_branch_name().unwrap();
let dev = a_branch_name().into_string(); let dev = a_branch_name().unwrap();
ServerRepoConfig::new( ServerRepoConfig::new(
format!("{}/{}", a_name(), a_name()), format!("{}/{}", a_name(), a_name()),
main.clone(), main.clone(),