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
use std::fmt;
use rand::{Rng, thread_rng};
use yaml_rust::yaml::{Yaml, YamlLoader};

use super::Item;

lazy_static! {
  static ref ADDRESSES: Vec<Yaml> =
    YamlLoader::load_from_str(include_str!("data/addresses.yml")).unwrap();
}

#[derive(Debug)]
pub struct Address {
  pub prefecture: Item,
  pub city: Item,
  pub town: Item,
}

impl Address {
  pub fn new() -> Address {
    let mut r = thread_rng();

    let prefecture =
      r.choose(ADDRESSES[0]["addresses"]["prefecture"].as_vec().unwrap()).unwrap();
    let city = r.choose(ADDRESSES[0]["addresses"]["city"].as_vec().unwrap()).unwrap();
    let town = r.choose(ADDRESSES[0]["addresses"]["town"].as_vec().unwrap()).unwrap();

    Address {
      prefecture:
        Item::new(
          prefecture[0].as_str().unwrap_or(""),
          prefecture[1].as_str().unwrap_or(""),
          prefecture[2].as_str().unwrap_or("")),
      city:
        Item::new(
          city[0].as_str().unwrap_or(""),
          city[1].as_str().unwrap_or(""),
          city[2].as_str().unwrap_or("")),
      town:
        Item::new(
          town[0].as_str().unwrap_or(""),
          town[1].as_str().unwrap_or(""),
          town[2].as_str().unwrap_or("")),
    }
  }

  pub fn to_kanji(&self) -> String {
    format!("{}{}{}", self.prefecture.kanji, self.city.kanji, self.town.kanji)
  }

  pub fn to_hiragana(&self) -> String {
    format!("{}{}{}", self.prefecture.hiragana, self.city.hiragana, self.town.hiragana)
  }

  pub fn to_katakana(&self) -> String {
    format!("{}{}{}", self.prefecture.katakana, self.city.katakana, self.town.katakana)
  }
}

impl fmt::Display for Address {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}", self.to_kanji())
  }
}