1use std::io::Result as IoResult;
2use std::cmp::PartialEq;
3
4#[derive(Debug)]
5pub struct Name {
6 pub first_name: String,
7 pub last_name: String,
8}
9
10#[derive(Debug)]
11pub struct Email {
12 pub email: String,
13}
14
15#[derive(Debug)]
16pub struct Text {
17 pub title: String,
18 pub description: String,
19}
20
21#[derive(Debug)]
22pub struct Address {
23 pub address: String,
24}
25
26impl PartialEq for Name {
27 fn eq(&self, other: &Self) -> bool {
28 (self.first_name == other.first_name) && (self.last_name == other.last_name)
29 }
30}
31
32impl Name {
33 pub fn fullname(self) -> String {
34 format!("{} {}", self.first_name, self.last_name)
35 }
36
37 pub fn reverse(self) -> IoResult<String> {
38 let mut result = String::new();
39
40 for name in self.fullname().chars().rev() {
41 result.push(name)
42 }
43
44 Ok(result)
45 }
46}
47
48impl PartialEq for Email {
49 fn eq(&self, other: &Self) -> bool {
50 self.email == other.email
51 }
52}
53
54impl PartialEq for Text {
55 fn eq(&self, other: &Self) -> bool {
56 (self.title == other.title) && (self.description == other.description)
57 }
58}
59
60impl PartialEq for Address {
61 fn eq(&self, other: &Self) -> bool {
62 self.address == other.address
63 }
64}
65
66pub mod name;
67pub mod email;
68pub mod text;
69pub mod address;
70
71pub fn create_names(first_name: &'static str, last_name: &'static str, loop_by: i32) -> IoResult<Vec<Name>> {
72 Ok(name::create_names(first_name, last_name, loop_by).unwrap())
73}
74
75pub fn create_name(first_name: &'static str, last_name: &'static str) -> IoResult<Name> {
76 Ok(name::create_name(first_name, last_name).unwrap())
77}
78
79pub fn create_emails(email: &'static str, loop_by: i32) -> IoResult<Vec<Email>> {
80 Ok(email::create_emails(email, loop_by).unwrap())
81}
82
83pub fn create_email(email: &'static str) -> IoResult<Email> {
84 Ok(email::create_email(email).unwrap())
85}
86
87pub fn create_text(title: &'static str, description: &'static str, loop_by: i32) -> IoResult<Vec<Text>> {
88 Ok(text::create_text(title, description, loop_by).unwrap())
89}
90
91pub fn create_address(address: &'static str, loop_by: i32) -> IoResult<Vec<Address>> {
92 Ok(address::create_address(address, loop_by).unwrap())
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn test_create_names() {
101 let first_name: &'static str = "Adiatma";
102 let last_name: &'static str = "Kamarudin";
103 let actual = create_names(first_name, last_name, 2).unwrap();
104 let expected = [
105 Name{
106 first_name: first_name.to_string(),
107 last_name: last_name.to_string(),
108 },
109 Name{
110 first_name: first_name.to_string(),
111 last_name: last_name.to_string(),
112 },
113 ];
114
115 assert_eq!(actual, expected)
116 }
117
118 #[test]
119 fn test_create_name() {
120 let first_name: &'static str = "Adiatma";
121 let last_name: &'static str = "Kamarudin";
122 let actual = create_name(first_name, last_name).unwrap();
123 let expected = Name{
124 first_name: first_name.to_string(),
125 last_name: last_name.to_string()
126 };
127
128 assert_eq!(actual, expected)
129 }
130
131 #[test]
132 fn test_fullname() {
133 let first_name: &'static str = "Adiatma";
134 let last_name: &'static str = "Kamarudin";
135 let actual = create_name(first_name, last_name).unwrap().fullname();
136 let expected = "Adiatma Kamarudin";
137
138 assert_eq!(actual, expected)
139 }
140
141 #[test]
142 fn test_reverse_name() {
143 let first_name: &'static str = "A";
144 let last_name: &'static str = "b";
145 let actual = create_name(first_name, last_name).unwrap().reverse().unwrap();
146 let expected = "b A";
147
148 assert_eq!(actual, expected)
149 }
150
151 #[test]
152 fn test_create_emails() {
153 let email: &'static str = "adiatma@gmail.com";
154 let actual = create_emails(email, 2).unwrap();
155 let expected = [
156 Email{
157 email: email.to_string(),
158 },
159 Email{
160 email: email.to_string(),
161 },
162 ];
163
164 assert_eq!(actual, expected)
165 }
166
167 #[test]
168 fn test_create_email() {
169 let email: &'static str = "adiatma@gmail.com";
170 let actual = create_email(email).unwrap();
171 let expected = Email{ email: email.to_string() };
172
173 assert_eq!(actual, expected)
174 }
175
176 #[test]
177 fn text_create_text() {
178 let title: &'static str = "Hello";
179 let description: &'static str = "World";
180 let actual = create_text(title, description, 2).unwrap();
181 let expected = [
182 Text{
183 title: title.to_string(),
184 description: description.to_string()
185 },
186 Text{
187 title: title.to_string(),
188 description: description.to_string()
189 },
190 ];
191
192 assert_eq!(actual, expected)
193 }
194
195 #[test]
196 fn test_create_address() {
197 let address: &'static str = "Street Foo";
198 let actual = create_address(address, 2).unwrap();
199 let expected = [Address{ address: address.to_string() }, Address{ address: address.to_string() }];
200
201 assert_eq!(actual, expected)
202 }
203}
204