refactor(git): more use of derive_more

This commit is contained in:
Paul Campbell 2024-05-14 16:28:17 +01:00
parent ac3e1be261
commit c374076323
9 changed files with 26 additions and 105 deletions

View file

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

View file

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

View file

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

View file

@ -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<Commit> for GitRef {
Self(value.sha().to_string())
}
}
impl From<BranchName> for GitRef {
fn from(value: BranchName) -> Self {
Self(value.to_string())
}
}

View file

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

View file

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

View file

@ -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<PathBuf>) -> Result<Self, Error> {
@ -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<gix::clone::fetch::Error>),
}
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<gix::clone::Error> for Error {
fn from(value: gix::clone::Error) -> Self {
Self::GixClone(Box::new(value))

View file

@ -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<gix::open::Error>),
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>;

View file

@ -53,41 +53,22 @@ pub fn find_default_remote(
}
type Result<T> = core::result::Result<T, Error>;
#[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}"
),
}
}
}