|
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 | | - } |
30 | | - |
31 | | - pub fn get(&self, alias: &str) -> Option<&Buddy> { |
32 | | - self.buddies |
33 | | - .iter() |
34 | | - .find(|buddy| buddy.alias == alias) |
35 | | - .map(|buddy| (buddy)) |
36 | | - } |
37 | | - |
38 | | - pub fn has(&self, alias: &str) -> bool { |
39 | | - match self.get(alias) { |
40 | | - Some(_) => true, |
41 | | - None => false, |
42 | | - } |
| 29 | + pub fn new(buddies: Vec<Buddy>) -> Self { |
| 30 | + Buddies { buddies } |
| 31 | + } |
| 32 | + |
| 33 | + pub fn get(&self, alias: &str) -> Option<&Buddy> { |
| 34 | + self |
| 35 | + .buddies |
| 36 | + .iter() |
| 37 | + .find(|buddy| buddy.alias == alias) |
| 38 | + .map(|buddy| (buddy)) |
| 39 | + } |
| 40 | + |
| 41 | + pub fn has(&self, alias: &str) -> bool { |
| 42 | + match self.get(alias) { |
| 43 | + Some(_) => true, |
| 44 | + None => false, |
43 | 45 | } |
44 | | - |
45 | | - pub fn get_buddy_by_email(&self, email: &str) -> Option<&Buddy> { |
46 | | - self.buddies |
47 | | - .iter() |
48 | | - .find(|buddy| buddy.email == email) |
49 | | - .map(|buddy| (buddy)) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn get_buddy_by_email(&self, email: &str) -> Option<&Buddy> { |
| 49 | + self |
| 50 | + .buddies |
| 51 | + .iter() |
| 52 | + .find(|buddy| buddy.email == email) |
| 53 | + .map(|buddy| (buddy)) |
| 54 | + } |
| 55 | + |
| 56 | + pub fn add(&mut self, buddy: Buddy) -> Result<()> { |
| 57 | + if self.has(&buddy.alias) { |
| 58 | + anyhow::bail!("Buddy with alias '{}' already exists", buddy.alias); |
50 | 59 | } |
51 | 60 |
|
52 | | - pub fn add(&mut self, buddy: Buddy) -> Result<()> { |
53 | | - if self.has(&buddy.alias) { |
54 | | - anyhow::bail!("Buddy with alias '{}' already exists", buddy.alias); |
55 | | - } |
| 61 | + self.buddies.push(buddy); |
| 62 | + Ok(()) |
| 63 | + } |
56 | 64 |
|
57 | | - self.buddies.push(buddy); |
58 | | - Ok(()) |
| 65 | + pub fn forget(&mut self, alias: &str) -> Result<()> { |
| 66 | + if let Some(index) = |
| 67 | + self.buddies.iter().position(|buddy| buddy.alias == alias) |
| 68 | + { |
| 69 | + self.buddies.swap_remove(index); |
| 70 | + return Ok(()); |
59 | 71 | } |
60 | 72 |
|
61 | | - pub fn forget(&mut self, alias: &str) -> Result<()> { |
62 | | - if let Some(index) = self.buddies.iter().position(|buddy| buddy.alias == alias) { |
63 | | - self.buddies.swap_remove(index); |
64 | | - return Ok(()); |
65 | | - } |
66 | | - |
67 | | - anyhow::bail!("Buddy with alias '{}' doesn't exist", alias); |
68 | | - } |
| 73 | + anyhow::bail!("Buddy with alias '{}' doesn't exist", alias); |
| 74 | + } |
69 | 75 | } |
70 | 76 |
|
71 | 77 | #[cfg(test)] |
72 | 78 | mod tests { |
73 | | - use super::*; |
74 | | - |
75 | | - #[test] |
76 | | - fn test_get_buddy_by_email() { |
77 | | - let mut buddies = Buddies::default(); |
78 | | - let _ = buddies.add(Buddy { |
79 | | - alias: "peter".to_string(), |
80 | | - name: "Peter Pan".to_string(), |
81 | | - email: "peter.pan@example.com".to_string(), |
82 | | - }); |
83 | | - |
84 | | - let result = buddies.get_buddy_by_email("peter.pan@example.com"); |
85 | | - assert!(result.is_some()); |
86 | | - let buddy = result.unwrap(); |
87 | | - assert_eq!(buddy.alias, "peter"); |
88 | | - assert_eq!(buddy.name, "Peter Pan"); |
89 | | - |
90 | | - // Non-existent email |
91 | | - // |
92 | | - let result = buddies.get_buddy_by_email("captain.hook@example.com"); |
93 | | - assert!(result.is_none()); |
94 | | - } |
95 | | - |
96 | | - #[test] |
97 | | - fn test_format_co_author() { |
98 | | - let buddy = Buddy { |
99 | | - alias: "peter".to_string(), |
100 | | - name: "Peter Pan".to_string(), |
101 | | - email: "peter.pan@example.com".to_string(), |
102 | | - }; |
103 | | - |
104 | | - let co_author = buddy.format_co_author(); |
105 | | - assert_eq!( |
106 | | - co_author, |
107 | | - "Co-authored-by: Peter Pan <peter.pan@example.com>".to_string() |
108 | | - ); |
109 | | - } |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_get_buddy_by_email() { |
| 83 | + let mut buddies = Buddies::default(); |
| 84 | + let _ = buddies.add(Buddy { |
| 85 | + alias: "peter".to_string(), |
| 86 | + name: "Peter Pan".to_string(), |
| 87 | + email: "peter.pan@example.com".to_string(), |
| 88 | + }); |
| 89 | + |
| 90 | + let result = buddies.get_buddy_by_email("peter.pan@example.com"); |
| 91 | + assert!(result.is_some()); |
| 92 | + let buddy = result.unwrap(); |
| 93 | + assert_eq!(buddy.alias, "peter"); |
| 94 | + assert_eq!(buddy.name, "Peter Pan"); |
| 95 | + |
| 96 | + // Non-existent email |
| 97 | + // |
| 98 | + let result = buddies.get_buddy_by_email("captain.hook@example.com"); |
| 99 | + assert!(result.is_none()); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_format_co_author() { |
| 104 | + let buddy = Buddy { |
| 105 | + alias: "peter".to_string(), |
| 106 | + name: "Peter Pan".to_string(), |
| 107 | + email: "peter.pan@example.com".to_string(), |
| 108 | + }; |
| 109 | + |
| 110 | + let co_author = buddy.format_co_author(); |
| 111 | + assert_eq!( |
| 112 | + co_author, |
| 113 | + "Co-authored-by: Peter Pan <peter.pan@example.com>".to_string() |
| 114 | + ); |
| 115 | + } |
110 | 116 | } |
0 commit comments