WIP: newtype - after repo-actor tests
This commit is contained in:
parent
924fa2a319
commit
6f70803d25
11 changed files with 88 additions and 63 deletions
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
mod api_token;
|
mod api_token;
|
||||||
mod branch_name;
|
mod branch_name;
|
||||||
pub mod common;
|
pub mod common;
|
||||||
|
@ -8,6 +9,7 @@ mod forge_name;
|
||||||
mod forge_type;
|
mod forge_type;
|
||||||
pub mod git_dir;
|
pub mod git_dir;
|
||||||
mod host_name;
|
mod host_name;
|
||||||
|
mod newtype;
|
||||||
mod registered_webhook;
|
mod registered_webhook;
|
||||||
mod repo_alias;
|
mod repo_alias;
|
||||||
mod repo_branches;
|
mod repo_branches;
|
||||||
|
|
57
crates/config/src/newtype.rs
Normal file
57
crates/config/src/newtype.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
|
@ -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(),
|
||||||
|
|
|
@ -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)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(),
|
||||||
|
|
|
@ -631,7 +631,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 {
|
||||||
|
@ -645,9 +645,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(),
|
||||||
|
|
Loading…
Reference in a new issue