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
//! # Example
//!
//! ```
//! use malhada::Josa::{EunNeun, IGa};
//!
//! let user = "유진";
//! let mackerel = "고등어";
//!
//! let sentence = format!("{} {} 먹고싶다", user + EunNeun, mackerel + IGa);
//!
//! assert_eq!(sentence, "유진은 고등어가 먹고싶다");
//! ```

use std::ops::Add;

use hangul::HangulExt;

/// Enum of [josas](https://en.wikipedia.org/wiki/Korean_grammar#Postpositions) that are selected depending on the noun in front of it
pub enum Josa {
  /// 은, 는
  EunNeun,
  /// 이, 가
  IGa,
}

use Josa::{EunNeun, IGa};

impl Josa {
  fn open(self) -> String {
    match self {
      EunNeun => "는",
      IGa => "가",
    }
    .to_owned()
  }

  fn closed(self) -> String {
    match self {
      EunNeun => "은",
      IGa => "이",
    }
    .to_owned()
  }
}

impl Add<Josa> for &str {
  type Output = String;

  fn add(self, josa: Josa) -> String {
    match self.chars().last().unwrap().is_open() {
      Ok(true) => self.to_owned() + &josa.open(),
      Ok(false) => self.to_owned() + &josa.closed(),
      Err(_) => self.to_owned() + &josa.open(),
    }
  }
}