|
| 1 | +use std::fmt::Display; |
| 2 | + |
1 | 3 | /// Represents a user within a commit with a name and email address
|
2 | 4 | #[derive(Debug, Eq, PartialEq)]
|
3 | 5 | pub(crate) struct User {
|
@@ -32,33 +34,23 @@ impl User {
|
32 | 34 | pub(crate) const fn is_some(&self) -> bool {
|
33 | 35 | self.name.is_some() || self.email.is_some()
|
34 | 36 | }
|
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 |
| - } |
41 | 37 | }
|
42 | 38 |
|
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}>") |
52 | 44 | }
|
53 | 45 | else {
|
54 |
| - String::from(name) |
| 46 | + write!(f, "{name}") |
55 | 47 | }
|
56 | 48 | }
|
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}>") |
59 | 51 | }
|
60 | 52 | else {
|
61 |
| - String::new() |
| 53 | + write!(f, "") |
62 | 54 | }
|
63 | 55 | }
|
64 | 56 | }
|
@@ -89,14 +81,12 @@ mod tests {
|
89 | 81 | fn is_some_none_when_some(#[case] name: Option<&str>, #[case] email: Option<&str>) {
|
90 | 82 | let user = User::new(name, email);
|
91 | 83 | assert!(user.is_some());
|
92 |
| - assert!(!user.is_none()); |
93 | 84 | }
|
94 | 85 |
|
95 | 86 | #[test]
|
96 | 87 | fn is_some_none_when_none() {
|
97 | 88 | let user = User::new(None, None);
|
98 | 89 | assert!(!user.is_some());
|
99 |
| - assert!(user.is_none()); |
100 | 90 | }
|
101 | 91 |
|
102 | 92 | #[test]
|
|
0 commit comments