From 88d10ee0e2d51e8a04e9c2a08157eb4ed0fac2bd Mon Sep 17 00:00:00 2001 From: Tim Oram Date: Thu, 15 Feb 2024 20:11:56 -0330 Subject: Cleanup User --- src/git/user.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/git/user.rs b/src/git/user.rs index a8fdc0d..d0d32c1 100644 --- a/src/git/user.rs +++ b/src/git/user.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + /// Represents a user within a commit with a name and email address #[derive(Debug, Eq, PartialEq)] pub(crate) struct User { @@ -32,33 +34,23 @@ impl User { pub(crate) const fn is_some(&self) -> bool { self.name.is_some() || self.email.is_some() } - - /// Returns `true` if both name and email is a `None` value. - #[must_use] - pub(crate) const fn is_none(&self) -> bool { - self.name.is_none() && self.email.is_none() - } } -impl ToString for User { - /// Creates a formatted string of the user - /// - /// The user if formatted with "Name <Email>", which matches the Git CLI. If name or email are - /// `None` then they are omitted from the result. If neither are set, and empty is returned. - fn to_string(&self) -> String { - if let Some(name) = self.name.as_ref() { - if let Some(email) = self.email.as_ref() { - format!("{name} <{email}>") +impl Display for User { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if let Some(name) = self.name() { + if let Some(email) = self.email() { + write!(f, "{name} <{email}>") } else { - String::from(name) + write!(f, "{name}") } } - else if let Some(email) = self.email.as_ref() { - format!("<{email}>") + else if let Some(email) = self.email() { + write!(f, "<{email}>") } else { - String::new() + write!(f, "") } } } @@ -89,14 +81,12 @@ mod tests { fn is_some_none_when_some(#[case] name: Option<&str>, #[case] email: Option<&str>) { let user = User::new(name, email); assert!(user.is_some()); - assert!(!user.is_none()); } #[test] fn is_some_none_when_none() { let user = User::new(None, None); assert!(!user.is_some()); - assert!(user.is_none()); } #[test] -- cgit v1.2.3