refactor(git): more derive_more replacing boilerplate

This commit is contained in:
Paul Campbell 2024-05-15 08:29:41 +01:00
parent d909d427c7
commit eb7d14bc33
6 changed files with 29 additions and 30 deletions

View file

@ -1,16 +1,10 @@
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] #[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)]
#[display("{}", sha)] #[display("{}", sha)]
pub struct Commit { pub struct Commit {
sha: Sha, sha: Sha,
message: Message, message: Message,
} }
impl Commit { impl Commit {
pub fn new(sha: &str, message: &str) -> Self {
Self {
sha: Sha::new(sha.to_string()),
message: Message::new(message.to_string()),
}
}
pub const fn sha(&self) -> &Sha { pub const fn sha(&self) -> &Sha {
&self.sha &self.sha
} }
@ -19,18 +13,8 @@ impl Commit {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] #[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)]
pub struct Sha(String); pub struct Sha(String);
impl Sha {
pub const fn new(value: String) -> Self {
Self(value)
}
}
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] #[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)]
pub struct Message(String); pub struct Message(String);
impl Message {
pub const fn new(value: String) -> Self {
Self(value)
}
}

View file

@ -1,7 +1,7 @@
use crate::Commit; use crate::Commit;
#[derive(Clone, Debug, Hash, PartialEq, Eq, derive_more::Display)] #[derive(Clone, Debug, Hash, PartialEq, Eq, derive_more::Display)]
pub struct GitRef(pub String); pub struct GitRef(String);
impl From<Commit> for GitRef { impl From<Commit> for GitRef {
fn from(value: Commit) -> Self { fn from(value: Commit) -> Self {
Self(value.sha().to_string()) Self(value.sha().to_string())

View file

@ -1,15 +1,12 @@
use git_next_config::{Hostname, RepoPath}; use git_next_config::{Hostname, RepoPath};
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] #[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)]
#[display("{}:{}", host, repo_path)] #[display("{}:{}", host, repo_path)]
pub struct GitRemote { pub struct GitRemote {
host: Hostname, host: Hostname,
repo_path: RepoPath, repo_path: RepoPath,
} }
impl GitRemote { impl GitRemote {
pub const fn new(host: Hostname, repo_path: RepoPath) -> Self {
Self { host, repo_path }
}
pub const fn host(&self) -> &Hostname { pub const fn host(&self) -> &Hostname {
&self.host &self.host
} }

View file

@ -6,13 +6,25 @@ mod branch {
#[actix_rt::test] #[actix_rt::test]
async fn test_find_next_commit_on_dev() { async fn test_find_next_commit_on_dev() {
let next = git::Commit::new("current-next", "foo"); let next = git::Commit::new(
let expected = git::Commit::new("dev-next", "next-should-go-here"); git::commit::Sha::new("current-next".to_string()),
git::commit::Message::new("foo".to_string()),
);
let expected = git::Commit::new(
git::commit::Sha::new("dev-next".to_string()),
git::commit::Message::new("next-should-go-here".to_string()),
);
let dev_commit_history = vec![ let dev_commit_history = vec![
git::Commit::new("dev", "future"), git::Commit::new(
git::commit::Sha::new("dev".to_string()),
git::commit::Message::new("future".to_string()),
),
expected.clone(), expected.clone(),
next.clone(), next.clone(),
git::Commit::new("current-main", "history"), git::Commit::new(
git::commit::Sha::new("current-main".to_string()),
git::commit::Message::new("history".to_string()),
),
]; ];
let next_commit = find_next_commit_on_dev(next, dev_commit_history); let next_commit = find_next_commit_on_dev(next, dev_commit_history);

View file

@ -301,7 +301,10 @@ impl Push {
} }
pub fn commit(&self) -> git::Commit { pub fn commit(&self) -> git::Commit {
git::Commit::new(&self.after, &self.head_commit.message) git::Commit::new(
git::commit::Sha::new(self.after.clone()),
git::commit::Message::new(self.head_commit.message.clone()),
)
} }
} }

View file

@ -254,6 +254,9 @@ struct RepoCommit {
} }
impl From<Commit> for git::Commit { impl From<Commit> for git::Commit {
fn from(value: Commit) -> Self { fn from(value: Commit) -> Self {
Self::new(&value.sha, &value.commit.message) Self::new(
git::commit::Sha::new(value.sha),
git::commit::Message::new(value.commit.message),
)
} }
} }