From c3740763230f981a70e1ff43f7b36d8edbfdfa29 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 14 May 2024 16:28:17 +0100 Subject: [PATCH] refactor(git): more use of derive_more --- Cargo.toml | 6 +++++- crates/git/src/commit.rs | 22 ++++------------------ crates/git/src/generation.rs | 7 +------ crates/git/src/git_ref.rs | 7 ------- crates/git/src/git_remote.rs | 8 ++------ crates/git/src/repo_details.rs | 21 ++++----------------- crates/git/src/repository.rs | 25 ++----------------------- crates/git/src/reset.rs | 10 +++++----- crates/git/src/validate.rs | 25 +++---------------------- 9 files changed, 26 insertions(+), 105 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c0b8a8ba..ef8905bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,11 @@ ulid = "1.1" warp = "0.3" # error handling -derive_more = { version = "1.0.0-beta.6", features = ["from", "display"] } +derive_more = { version = "1.0.0-beta.6", features = [ + "from", + "display", + "deref", +] } terrors = "0.3" # file watcher diff --git a/crates/git/src/commit.rs b/crates/git/src/commit.rs index 390046a6..96053526 100644 --- a/crates/git/src/commit.rs +++ b/crates/git/src/commit.rs @@ -1,4 +1,5 @@ -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] +#[display("{}", sha)] pub struct Commit { sha: Sha, message: Message, @@ -17,34 +18,19 @@ impl Commit { &self.message } } -impl std::fmt::Display for Commit { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.sha) - } -} -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] pub struct Sha(String); impl Sha { pub const fn new(value: String) -> Self { Self(value) } } -impl std::fmt::Display for Sha { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] pub struct Message(String); impl Message { pub const fn new(value: String) -> Self { Self(value) } } -impl std::fmt::Display for Message { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} diff --git a/crates/git/src/generation.rs b/crates/git/src/generation.rs index 5e0e0e4e..e5a445c2 100644 --- a/crates/git/src/generation.rs +++ b/crates/git/src/generation.rs @@ -1,4 +1,4 @@ -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] pub struct Generation(u32); impl Generation { pub fn new() -> Self { @@ -8,8 +8,3 @@ impl Generation { self.0 += 1 } } -impl std::fmt::Display for Generation { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} diff --git a/crates/git/src/git_ref.rs b/crates/git/src/git_ref.rs index 261382c0..047ec6b6 100644 --- a/crates/git/src/git_ref.rs +++ b/crates/git/src/git_ref.rs @@ -1,5 +1,3 @@ -use git_next_config::BranchName; - use crate::Commit; #[derive(Clone, Debug, Hash, PartialEq, Eq, derive_more::Display)] @@ -9,8 +7,3 @@ impl From for GitRef { Self(value.sha().to_string()) } } -impl From for GitRef { - fn from(value: BranchName) -> Self { - Self(value.to_string()) - } -} diff --git a/crates/git/src/git_remote.rs b/crates/git/src/git_remote.rs index e2340049..926380f8 100644 --- a/crates/git/src/git_remote.rs +++ b/crates/git/src/git_remote.rs @@ -1,6 +1,7 @@ use git_next_config::{Hostname, RepoPath}; -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)] +#[display("{}:{}", host, repo_path)] pub struct GitRemote { host: Hostname, repo_path: RepoPath, @@ -16,8 +17,3 @@ impl GitRemote { &self.repo_path } } -impl std::fmt::Display for GitRemote { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}:{}", self.host, self.repo_path) - } -} diff --git a/crates/git/src/repo_details.rs b/crates/git/src/repo_details.rs index 7afd2f73..e61da584 100644 --- a/crates/git/src/repo_details.rs +++ b/crates/git/src/repo_details.rs @@ -6,7 +6,10 @@ use git_next_config::{ use super::{Generation, GitRemote}; /// The derived information about a repo, used to interact with it -#[derive(Clone, Debug)] +#[derive(Clone, Debug, derive_more::Display)] +#[display("gen-{}:{}:{}/{}:{}@{}/{}@{}", generation, forge.forge_type, + forge.forge_name, repo_alias, forge.user, forge.hostname, repo_path, + branch)] pub struct RepoDetails { pub generation: Generation, pub repo_alias: RepoAlias, @@ -57,19 +60,3 @@ impl RepoDetails { GitRemote::new(self.forge.hostname.clone(), self.repo_path.clone()) } } -impl std::fmt::Display for RepoDetails { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "gen-{}:{}:{}/{}:{}@{}/{}@{}", - self.generation, - self.forge.forge_type, - self.forge.forge_name, - self.repo_alias, - self.forge.user, - self.forge.hostname, - self.repo_path, - self.branch, - ) - } -} diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rs index 3dbcccaa..b3562c78 100644 --- a/crates/git/src/repository.rs +++ b/crates/git/src/repository.rs @@ -3,7 +3,7 @@ use std::{ops::Deref as _, path::PathBuf, sync::atomic::AtomicBool}; use super::RepoDetails; -#[derive(Debug, Clone, derive_more::From)] +#[derive(Debug, Clone, derive_more::From, derive_more::Deref)] pub struct Repository(gix::ThreadSafeRepository); impl Repository { pub fn open(gitdir: impl Into) -> Result { @@ -19,15 +19,8 @@ impl Repository { Ok(repository.into_sync().into()) } } -impl std::ops::Deref for Repository { - type Target = gix::ThreadSafeRepository; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -#[derive(Debug)] +#[derive(Debug, derive_more::Display)] pub enum Error { InvalidGitDir(git_next_config::GitDir), Io(std::io::Error), @@ -39,20 +32,6 @@ pub enum Error { GixFetch(Box), } impl std::error::Error for Error {} -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::InvalidGitDir(gitdir) => write!(f, "Invalid Git dir: {:?}", gitdir), - Self::Io(err) => write!(f, "IO error: {:?}", err), - Self::Wait(err) => write!(f, "Waiting for command: {:?}", err), - Self::Spawn(err) => write!(f, "Spawning comming: {:?}", err), - Self::Validation(err) => write!(f, "Validation: {}", err), - Self::GixClone(err) => write!(f, "Clone: {:?}", err), - Self::GixOpen(err) => write!(f, "Open: {:?}", err), - Self::GixFetch(err) => write!(f, "Fetch: {:?}", err), - } - } -} impl From for Error { fn from(value: gix::clone::Error) -> Self { Self::GixClone(Box::new(value)) diff --git a/crates/git/src/reset.rs b/crates/git/src/reset.rs index 4be02ed0..5d3f0bbb 100644 --- a/crates/git/src/reset.rs +++ b/crates/git/src/reset.rs @@ -59,22 +59,22 @@ pub fn reset( } Err(err) => { warn!(?err, ?git_dir, "Failed (wait)"); - Err(BranchResetError::Push) + Err(Error::Push) } }, Err(err) => { warn!(?err, ?git_dir, "Failed (spawn)"); - Err(BranchResetError::Push) + Err(Error::Push) } } } #[derive(Debug, derive_more::From, derive_more::Display)] -pub enum BranchResetError { +pub enum Error { Open(Box), Fetch(super::fetch::Error), Push, } -impl std::error::Error for BranchResetError {} +impl std::error::Error for Error {} -pub type Result = core::result::Result<(), BranchResetError>; +pub type Result = core::result::Result<(), Error>; diff --git a/crates/git/src/validate.rs b/crates/git/src/validate.rs index f6b93d4b..ae7eedf1 100644 --- a/crates/git/src/validate.rs +++ b/crates/git/src/validate.rs @@ -53,41 +53,22 @@ pub fn find_default_remote( } type Result = core::result::Result; -#[derive(Debug)] +#[derive(Debug, derive_more::Display)] pub enum Error { NoDefaultPushRemote, NoUrlForDefaultPushRemote, NoHostnameForDefaultPushRemote, UnableToOpenRepo(String), Io(std::io::Error), + #[display("MismatchDefaultPushRemote(found: {found}, expected: {expected})")] MismatchDefaultPushRemote { found: GitRemote, expected: GitRemote, }, + #[display("MismatchDefaultFetchRemote(found: {found}, expected: {expected})")] MismatchDefaultFetchRemote { found: GitRemote, expected: GitRemote, }, } impl std::error::Error for Error {} -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::NoDefaultPushRemote => write!(f, "There is no default push remote"), - Self::NoUrlForDefaultPushRemote => write!(f, "The default push remote has no url"), - Self::NoHostnameForDefaultPushRemote => { - write!(f, "The default push remote has no hostname") - } - Self::UnableToOpenRepo(err) => write!(f, "Unable to open the git dir: {err}"), - Self::Io(err) => write!(f, "IO Error: {err:?}"), - Self::MismatchDefaultPushRemote { found, expected } => write!( - f, - "The default push remote doesn't match: {found}, expected: {expected}" - ), - Self::MismatchDefaultFetchRemote { found, expected } => write!( - f, - "The default fetch remote doesn't match: {found}, expected: {expected}" - ), - } - } -}