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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
use std::io::Result as IoResult; use std::cmp::PartialEq; #[derive(Debug)] pub struct Name { pub first_name: String, pub last_name: String, } #[derive(Debug)] pub struct Email { pub email: String, } #[derive(Debug)] pub struct Text { pub title: String, pub description: String, } impl PartialEq for Name { fn eq(&self, other: &Self) -> bool { (self.first_name == other.first_name) && (self.last_name == other.last_name) } } impl Name { pub fn fullname(self) -> String { format!("{} {}", self.first_name, self.last_name) } pub fn reverse(self) -> IoResult<String> { let mut result = String::new(); for name in self.fullname().chars().rev() { result.push(name) } Ok(result) } } impl PartialEq for Email { fn eq(&self, other: &Self) -> bool { self.email == other.email } } impl PartialEq for Text { fn eq(&self, other: &Self) -> bool { (self.title == other.title) && (self.description == other.description) } } pub mod name; pub mod email; pub mod text; pub fn create_names(first_name: &'static str, last_name: &'static str, loop_by: i32) -> IoResult<Vec<Name>> { Ok(name::create_names(first_name, last_name, loop_by).unwrap()) } pub fn create_name(first_name: &'static str, last_name: &'static str) -> IoResult<Name> { Ok(name::create_name(first_name, last_name).unwrap()) } pub fn create_emails(email: &'static str, loop_by: i32) -> IoResult<Vec<Email>> { Ok(email::create_emails(email, loop_by).unwrap()) } pub fn create_email(email: &'static str) -> IoResult<Email> { Ok(email::create_email(email).unwrap()) } pub fn create_text(title: &'static str, description: &'static str, loop_by: i32) -> IoResult<Vec<Text>> { Ok(text::create_text(title, description, loop_by).unwrap()) } #[cfg(test)] mod tests { use super::*; #[test] fn test_create_names() { let actual = create_names("Adiatma", "Kamarudin", 2).unwrap(); let expected = [ Name{ first_name: "Adiatma".to_string(), last_name: "Kamarudin".to_string(), }, Name{ first_name: "Adiatma".to_string(), last_name: "Kamarudin".to_string(), }, ]; assert_eq!(actual, expected) } #[test] fn test_create_name() { let actual = create_name("Adiatma", "Kamarudin").unwrap(); let expected = Name{ first_name: "Adiatma".to_string(), last_name: "Kamarudin".to_string() }; assert_eq!(actual, expected) } #[test] fn test_create_emails() { let actual = create_emails("adiatma@gmail.com", 2).unwrap(); let expected = [ Email{ email: "adiatma@gmail.com".to_string(), }, Email{ email: "adiatma@gmail.com".to_string(), }, ]; assert_eq!(actual, expected) } #[test] fn test_create_email() { let actual = create_email("adiatma@gmail.com").unwrap(); let expected = Email{ email: "adiatma@gmail.com".to_string() }; assert_eq!(actual, expected) } #[test] fn text_create_text() { let title: &'static str = "Hello"; let description: &'static str = "World"; let actual = create_text(title, description, 2).unwrap(); let expected = [ Text{ title: title.to_string(), description: description.to_string() }, Text{ title: title.to_string(), description: description.to_string() }, ]; assert_eq!(actual, expected) } }