|
1 | 1 | use anyhow::Result; |
2 | 2 | use serde::{Deserialize, Serialize}; |
3 | 3 |
|
4 | | -#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, PartialOrd, Eq, Ord)] |
| 4 | +#[derive( |
| 5 | + Debug, Serialize, Deserialize, Clone, PartialEq, PartialOrd, Eq, Ord, |
| 6 | +)] |
5 | 7 | pub struct Buddy { |
6 | | - pub alias: String, |
7 | | - pub name: String, |
8 | | - pub email: String, |
| 8 | + pub alias: String, |
| 9 | + pub name: String, |
| 10 | + pub email: String, |
9 | 11 | } |
10 | 12 |
|
11 | 13 | impl Buddy { |
12 | | - pub fn format_buddy(&self) -> String { |
13 | | - format!("{} <{}>", self.name, self.email) |
14 | | - } |
| 14 | + pub fn format_buddy(&self) -> String { |
| 15 | + format!("{} <{}>", self.name, self.email) |
| 16 | + } |
15 | 17 |
|
16 | | - pub fn format_co_author(&self) -> String { |
17 | | - format!("Co-authored-by: {}", self.format_buddy()) |
18 | | - } |
| 18 | + pub fn format_co_author(&self) -> String { |
| 19 | + format!("Co-authored-by: {}", self.format_buddy()) |
| 20 | + } |
19 | 21 | } |
20 | 22 |
|
21 | 23 | #[derive(Debug, Serialize, Deserialize, Default)] |
22 | 24 | pub struct Buddies { |
23 | | - pub buddies: Vec<Buddy>, |
| 25 | + pub buddies: Vec<Buddy>, |
24 | 26 | } |
25 | 27 |
|
26 | 28 | impl Buddies { |
27 | | - pub fn new(buddies: Vec<Buddy>) -> Self { |
28 | | - Buddies { buddies } |
29 | | - } |
| 29 | + pub fn new(buddies: Vec<Buddy>) -> Self { |
| 30 | + Buddies { buddies } |
| 31 | + } |
30 | 32 |
|
31 | | - pub fn get(&self, alias: &str) -> Option<&Buddy> { |
32 | | - self.buddies |
33 | | - .iter() |
34 | | - .find(|buddy| buddy.alias == alias) |
35 | | - } |
| 33 | + pub fn get(&self, alias: &str) -> Option<&Buddy> { |
| 34 | + self.buddies.iter().find(|buddy| buddy.alias == alias) |
| 35 | + } |
36 | 36 |
|
37 | | - pub fn has(&self, alias: &str) -> bool { |
38 | | - self.get(alias).is_some() |
39 | | - } |
| 37 | + pub fn has(&self, alias: &str) -> bool { |
| 38 | + self.get(alias).is_some() |
| 39 | + } |
40 | 40 |
|
41 | | - pub fn get_buddy_by_email(&self, email: &str) -> Option<&Buddy> { |
42 | | - self.buddies |
43 | | - .iter() |
44 | | - .find(|buddy| buddy.email == email) |
45 | | - } |
| 41 | + pub fn get_buddy_by_email(&self, email: &str) -> Option<&Buddy> { |
| 42 | + self.buddies.iter().find(|buddy| buddy.email == email) |
| 43 | + } |
46 | 44 |
|
47 | | - pub fn add(&mut self, buddy: Buddy) -> Result<()> { |
48 | | - if self.has(&buddy.alias) { |
49 | | - anyhow::bail!("Buddy with alias '{}' already exists", buddy.alias); |
50 | | - } |
51 | | - |
52 | | - self.buddies.push(buddy); |
53 | | - Ok(()) |
| 45 | + pub fn add(&mut self, buddy: Buddy) -> Result<()> { |
| 46 | + if self.has(&buddy.alias) { |
| 47 | + anyhow::bail!("Buddy with alias '{}' already exists", buddy.alias); |
54 | 48 | } |
55 | 49 |
|
56 | | - pub fn forget(&mut self, alias: &str) -> Result<()> { |
57 | | - if let Some(index) = self.buddies.iter().position(|buddy| buddy.alias == alias) { |
58 | | - self.buddies.swap_remove(index); |
59 | | - return Ok(()); |
60 | | - } |
| 50 | + self.buddies.push(buddy); |
| 51 | + Ok(()) |
| 52 | + } |
61 | 53 |
|
62 | | - anyhow::bail!("Buddy with alias '{}' doesn't exist", alias); |
| 54 | + pub fn forget(&mut self, alias: &str) -> Result<()> { |
| 55 | + if let Some(index) = |
| 56 | + self.buddies.iter().position(|buddy| buddy.alias == alias) |
| 57 | + { |
| 58 | + self.buddies.swap_remove(index); |
| 59 | + return Ok(()); |
63 | 60 | } |
| 61 | + |
| 62 | + anyhow::bail!("Buddy with alias '{}' doesn't exist", alias); |
| 63 | + } |
64 | 64 | } |
65 | 65 |
|
66 | 66 | #[cfg(test)] |
67 | 67 | mod tests { |
68 | | - use super::*; |
69 | | - |
70 | | - #[test] |
71 | | - fn test_get_buddy_by_email() { |
72 | | - let mut buddies = Buddies::default(); |
73 | | - let _ = buddies.add(Buddy { |
74 | | - alias: "peter".to_string(), |
75 | | - name: "Peter Pan".to_string(), |
76 | | - email: "peter.pan@example.com".to_string(), |
77 | | - }); |
78 | | - |
79 | | - let result = buddies.get_buddy_by_email("peter.pan@example.com"); |
80 | | - assert!(result.is_some()); |
81 | | - let buddy = result.unwrap(); |
82 | | - assert_eq!(buddy.alias, "peter"); |
83 | | - assert_eq!(buddy.name, "Peter Pan"); |
84 | | - |
85 | | - // Non-existent email |
86 | | - // |
87 | | - let result = buddies.get_buddy_by_email("captain.hook@example.com"); |
88 | | - assert!(result.is_none()); |
89 | | - } |
90 | | - |
91 | | - #[test] |
92 | | - fn test_format_co_author() { |
93 | | - let buddy = Buddy { |
94 | | - alias: "peter".to_string(), |
95 | | - name: "Peter Pan".to_string(), |
96 | | - email: "peter.pan@example.com".to_string(), |
97 | | - }; |
98 | | - |
99 | | - let co_author = buddy.format_co_author(); |
100 | | - assert_eq!( |
101 | | - co_author, |
102 | | - "Co-authored-by: Peter Pan <peter.pan@example.com>".to_string() |
103 | | - ); |
104 | | - } |
| 68 | + use super::*; |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_get_buddy_by_email() { |
| 72 | + let mut buddies = Buddies::default(); |
| 73 | + let _ = buddies.add(Buddy { |
| 74 | + alias: "peter".to_string(), |
| 75 | + name: "Peter Pan".to_string(), |
| 76 | + email: "peter.pan@example.com".to_string(), |
| 77 | + }); |
| 78 | + |
| 79 | + let result = buddies.get_buddy_by_email("peter.pan@example.com"); |
| 80 | + assert!(result.is_some()); |
| 81 | + let buddy = result.unwrap(); |
| 82 | + assert_eq!(buddy.alias, "peter"); |
| 83 | + assert_eq!(buddy.name, "Peter Pan"); |
| 84 | + |
| 85 | + // Non-existent email |
| 86 | + // |
| 87 | + let result = buddies.get_buddy_by_email("captain.hook@example.com"); |
| 88 | + assert!(result.is_none()); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_format_co_author() { |
| 93 | + let buddy = Buddy { |
| 94 | + alias: "peter".to_string(), |
| 95 | + name: "Peter Pan".to_string(), |
| 96 | + email: "peter.pan@example.com".to_string(), |
| 97 | + }; |
| 98 | + |
| 99 | + let co_author = buddy.format_co_author(); |
| 100 | + assert_eq!( |
| 101 | + co_author, |
| 102 | + "Co-authored-by: Peter Pan <peter.pan@example.com>".to_string() |
| 103 | + ); |
| 104 | + } |
105 | 105 | } |
0 commit comments