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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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,
}

#[derive(Debug)]
pub struct Address {
  pub address: 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)
  }
}

impl PartialEq for Address {
  fn eq(&self, other: &Self) -> bool {
    self.address == other.address
  }
}

pub mod name;
pub mod email;
pub mod text;
pub mod address;

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())
}

pub fn create_address(address: &'static str, loop_by: i32) -> IoResult<Vec<Address>> {
  Ok(address::create_address(address, loop_by).unwrap())
}

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

  #[test]
  fn test_create_names() {
    let first_name: &'static str = "Adiatma";
    let last_name: &'static str = "Kamarudin";
    let actual = create_names(first_name, last_name, 2).unwrap();
    let expected = [
      Name{
        first_name: first_name.to_string(),
        last_name: last_name.to_string(),
      },
      Name{
        first_name: first_name.to_string(),
        last_name: last_name.to_string(),
      },
    ];

    assert_eq!(actual, expected)
  }

  #[test]
  fn test_create_name() {
    let first_name: &'static str = "Adiatma";
    let last_name: &'static str = "Kamarudin";
    let actual = create_name(first_name, last_name).unwrap();
    let expected = Name{
      first_name: first_name.to_string(),
      last_name: last_name.to_string()
    };

    assert_eq!(actual, expected)
  }

  #[test]
  fn test_fullname() {
    let first_name: &'static str = "Adiatma";
    let last_name: &'static str = "Kamarudin";
    let actual = create_name(first_name, last_name).unwrap().fullname();
    let expected = "Adiatma Kamarudin";

    assert_eq!(actual, expected)
  }

  #[test]
  fn test_reverse_name() {
    let first_name: &'static str = "A";
    let last_name: &'static str = "b";
    let actual = create_name(first_name, last_name).unwrap().reverse().unwrap();
    let expected = "b A";

    assert_eq!(actual, expected)
  }

  #[test]
  fn test_create_emails() {
    let email: &'static str = "adiatma@gmail.com";
    let actual = create_emails(email, 2).unwrap();
    let expected = [
      Email{
        email: email.to_string(),
      },
      Email{
        email: email.to_string(),
      },
    ];

    assert_eq!(actual, expected)
  }

  #[test]
  fn test_create_email() {
    let email: &'static str = "adiatma@gmail.com";
    let actual = create_email(email).unwrap();
    let expected = Email{ email: email.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)
  }

  #[test]
  fn test_create_address() {
    let address: &'static str = "Street Foo";
    let actual = create_address(address, 2).unwrap();
    let expected = [Address{ address: address.to_string() }, Address{ address: address.to_string() }];

    assert_eq!(actual, expected)
  }
}