Skip to content

Commit 88d10ee

Browse files
committed
Cleanup User
1 parent d19563f commit 88d10ee

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

src/git/user.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::Display;
2+
13
/// Represents a user within a commit with a name and email address
24
#[derive(Debug, Eq, PartialEq)]
35
pub(crate) struct User {
@@ -32,33 +34,23 @@ impl User {
3234
pub(crate) const fn is_some(&self) -> bool {
3335
self.name.is_some() || self.email.is_some()
3436
}
35-
36-
/// Returns `true` if both name and email is a `None` value.
37-
#[must_use]
38-
pub(crate) const fn is_none(&self) -> bool {
39-
self.name.is_none() && self.email.is_none()
40-
}
4137
}
4238

43-
impl ToString for User {
44-
/// Creates a formatted string of the user
45-
///
46-
/// The user if formatted with "Name <Email>", which matches the Git CLI. If name or email are
47-
/// `None` then they are omitted from the result. If neither are set, and empty is returned.
48-
fn to_string(&self) -> String {
49-
if let Some(name) = self.name.as_ref() {
50-
if let Some(email) = self.email.as_ref() {
51-
format!("{name} <{email}>")
39+
impl Display for User {
40+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41+
if let Some(name) = self.name() {
42+
if let Some(email) = self.email() {
43+
write!(f, "{name} <{email}>")
5244
}
5345
else {
54-
String::from(name)
46+
write!(f, "{name}")
5547
}
5648
}
57-
else if let Some(email) = self.email.as_ref() {
58-
format!("<{email}>")
49+
else if let Some(email) = self.email() {
50+
write!(f, "<{email}>")
5951
}
6052
else {
61-
String::new()
53+
write!(f, "")
6254
}
6355
}
6456
}
@@ -89,14 +81,12 @@ mod tests {
8981
fn is_some_none_when_some(#[case] name: Option<&str>, #[case] email: Option<&str>) {
9082
let user = User::new(name, email);
9183
assert!(user.is_some());
92-
assert!(!user.is_none());
9384
}
9485

9586
#[test]
9687
fn is_some_none_when_none() {
9788
let user = User::new(None, None);
9889
assert!(!user.is_some());
99-
assert!(user.is_none());
10090
}
10191

10292
#[test]

0 commit comments

Comments
 (0)