1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// Represents a user within a commit with a name and email address
#[derive(Debug, Eq, PartialEq)]
pub struct User {
	name: Option<String>,
	email: Option<String>,
}

impl User {
	/// Creates a new user
	#[inline]
	#[must_use]
	pub fn new(name: Option<&str>, email: Option<&str>) -> Self {
		Self {
			email: email.map(String::from),
			name: name.map(String::from),
		}
	}

	/// Get the optional name of the user
	#[inline]
	#[must_use]
	pub const fn name(&self) -> &Option<String> {
		&self.name
	}

	/// Get the optional email of the user
	#[inline]
	#[must_use]
	pub const fn email(&self) -> &Option<String> {
		&self.email
	}

	/// Returns `true` if one of name or email is a `Some` value.
	#[inline]
	#[must_use]
	pub 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.
	#[inline]
	#[must_use]
	pub 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 &lt;Email&gt;", 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.
	#[inline]
	fn to_string(&self) -> String {
		if let Some(name) = self.name.as_ref() {
			if let Some(email) = self.email.as_ref() {
				format!("{} <{}>", name, email)
			}
			else {
				String::from(name)
			}
		}
		else if let Some(email) = self.email.as_ref() {
			format!("<{}>", email)
		}
		else {
			String::from("")
		}
	}
}

#[cfg(test)]
mod tests {
	use rstest::rstest;

	use super::*;

	#[test]
	fn name() {
		let user = User::new(Some("name"), None);
		assert_eq!(user.name(), &Some(String::from("name")));
	}

	#[test]
	fn email() {
		let user = User::new(None, Some("email"));
		assert_eq!(user.email(), &Some(String::from("email")));
	}

	#[rstest]
	#[case(None, None, false)]
	#[case(Some("name"), None, true)]
	#[case(None, Some("email"), true)]
	#[case(Some("email"), Some("email"), true)]
	fn is_some(#[case] name: Option<&str>, #[case] email: Option<&str>, #[case] expected: bool) {
		assert_eq!(User::new(name, email).is_some(), expected);
	}

	#[rstest]
	#[case(None, None, true)]
	#[case(Some("name"), None, false)]
	#[case(None, Some("email"), false)]
	#[case(Some("email"), Some("email"), false)]
	fn is_none(#[case] name: Option<&str>, #[case] email: Option<&str>, #[case] expected: bool) {
		assert_eq!(User::new(name, email).is_none(), expected);
	}

	#[test]
	fn to_string_with_none_name_and_none_email() {
		let user = User::new(None, None);
		assert_eq!(user.to_string(), "");
	}

	#[test]
	fn to_string_with_none_name_and_some_email() {
		let user = User::new(None, Some("me@example.com"));
		assert_eq!(user.to_string(), "<me@example.com>");
	}

	#[test]
	fn to_string_with_some_name_and_none_email() {
		let user = User::new(Some("Tim Oram"), None);
		assert_eq!(user.to_string(), "Tim Oram");
	}

	#[test]
	fn to_string_with_some_name_and_some_email() {
		let user = User::new(Some("Tim Oram"), Some("me@example.com"));
		assert_eq!(user.to_string(), "Tim Oram <me@example.com>");
	}
}