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
use std::error::Error;
use std::fmt;
use std::ops::{Add, AddAssign};
use hangul::HangulExt;
use hangul::ParseSyllableError;
const EUN: &str = "은";
const NEUN: &str = "는";
const I: &str = "이";
const GA: &str = "가";
const EUL: &str = "을";
const REUL: &str = "를";
const GWA: &str = "과";
const WA: &str = "와";
pub enum Josa {
EunNeun,
IGa,
EulReul,
GwaWa,
}
use Josa::{EunNeun, IGa, EulReul, GwaWa};
impl Josa {
fn open(self) -> &'static str {
match self {
EunNeun => NEUN,
IGa => GA,
EulReul => REUL,
GwaWa => WA,
}
}
fn closed(self) -> &'static str {
match self {
EunNeun => EUN,
IGa => I,
EulReul => EUL,
GwaWa => GWA,
}
}
}
#[derive(Debug)]
pub enum JosaError {
EmptyStr,
NotHangulSyllable(char),
}
use JosaError::{EmptyStr, NotHangulSyllable};
impl fmt::Display for JosaError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
EmptyStr => "Empty string given to josa selector".to_owned(),
NotHangulSyllable(c) => format!("{} is not a Hangul Syllable", c),
}
)
}
}
impl Error for JosaError {}
impl From<ParseSyllableError> for JosaError {
fn from(e: ParseSyllableError) -> JosaError {
NotHangulSyllable(e.0)
}
}
pub fn select(noun: &str, josa: Josa) -> Result<&str, JosaError> {
if noun.chars().last().ok_or(EmptyStr)?.is_open()? {
Ok(josa.open())
} else {
Ok(josa.closed())
}
}
pub trait JosaExt {
fn push_josa(&mut self, josa: Josa);
}
impl JosaExt for String {
fn push_josa(&mut self, josa: Josa) {
self.push_str(match self.chars().last() {
Some(last) => match last.is_open() {
Ok(true) => josa.open(),
Ok(false) => josa.closed(),
Err(_) => josa.open(),
},
None => "",
});
}
}
impl Add<Josa> for String {
type Output = String;
fn add(mut self, josa: Josa) -> String {
self.push_josa(josa);
self
}
}
impl AddAssign<Josa> for String {
fn add_assign(&mut self, josa: Josa) {
self.push_josa(josa);
}
}