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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
//! Josa helps you append [josa] to a string in idiomatic way. //! //! It has three approaches: //! //! - [`push_josa`] method on [`String`] //! - `+`, `+=` operator overloading //! - A pure function [`select`] that selects appropriate josa //! //! # Example: [`push_josa`] method //! //! [`push_josa`] method works the same way as [`push_str`]. //! It appends a given [`Josa`] onto the end of this [`String`]. //! Note that it does mutate the string, //! so you need to declare the [`String`] as mutable. //! //! ``` //! use josa::JosaExt; //! use josa::{EulReul, EunNeun}; //! //! let mut user = "나".to_owned(); //! let mut you = "님".to_owned(); //! //! user.push_josa(EulReul); //! you.push_josa(EunNeun); //! //! assert_eq!( //! format!("{} 버리고 가시는 {}", user, you), //! "나를 버리고 가시는 님은" //! ); //! ``` //! //! # Example: `+`, `+=` operator overloading //! //! `+`, `+=` concatenates [`String`] with appropriate [`Josa`]. //! //! `+` consumes the [`String`] on the left-hand. //! This is done to avoid allocating a new [`String`] and copying the entire contents. //! //! ``` //! use josa::{EunNeun, IGa}; //! //! let user = "유진".to_owned(); //! let mackerel = "고등어".to_owned(); //! //! assert_eq!( //! format!("{} {} 먹고싶다", user + EunNeun, mackerel + IGa), //! "유진은 고등어가 먹고싶다" //! ); //! ``` //! //! # Example: A pure function that selects appropriate josa //! //! Sometimes we need to append a josa to formatted text like //! `<span class="bold">곡괭이</span>`. //! In that case, last character can be part of tag, which is not a Hangul Syllable. //! [`select`] is used to get only josa, instead of appending it. //! //! ``` //! use josa::select; //! use josa::Eu; //! # use josa::Error; //! //! let pick = "곡괭이"; //! //! assert_eq!( //! format!( //! r#"<span class="bold">{}</span>{}로 채취하세요."#, //! pick, //! select(pick, Eu)? //! ), //! r#"<span class="bold">곡괭이</span>로 채취하세요."# //! ); //! //! let hand = "손"; //! //! assert_eq!( //! format!( //! r#"<span class="bold">{}</span>{}로 채취하세요."#, //! hand, //! select(hand, Eu)? //! ), //! r#"<span class="bold">손</span>으로 채취하세요."# //! ); //! //! # Ok::<(), Error>(()) //! ``` //! //! # Edge cases //! //! For [`push_josa`], `+`, and `+=`, since they are infallible, //! they handle edge cases in their own way. //! //! ### Empty String //! //! If given [`String`] is empty, it does not push any josa. //! //! ``` //! use josa::{JosaExt, IGa}; //! //! let mut empty = "".to_owned(); //! empty.push_josa(IGa); //! //! assert_eq!(empty, ""); //! ``` //! //! ### Non Hangul Syllable character //! //! If given [`String`] ends with character other than Hangul Syllable, //! it pushes `이(가)` formatted josa. //! //! ``` //! use josa::{JosaExt, IGa, Eu}; //! //! let mut curry = "curry".to_owned(); //! curry.push_josa(IGa); //! //! assert_eq!(curry, "curry이(가)"); //! //! //! let mut pioneer = "pioneer".to_owned(); //! pioneer.push_josa(Eu); //! //! assert_eq!(pioneer, "pioneer(으)"); // you can append 로서 //! ``` //! //! # Supported josas //! //! Currently we supports: //! //! - 은/는 //! - 이/가 //! - 을/를 //! - 과/와 //! - 이/(empty) (이다/다, 이나/나, 이란/란, 이든가/든가, 이나마/나마, 이야말로/야말로, 이랑/랑, 이여/여, 이며/며) //! - 으/(empty) (으로/로, 으로서/로서, 으로써/로써, 으로부터/로부터) //! //! [josa]: https://en.wikipedia.org/wiki/Korean_postpositions //! [`push_josa`]: trait.JosaExt.html#tymethod.push_josa //! [`push_str`]: https://doc.rust-lang.org/std/string/struct.String.html#method.push_str //! [`select`]: fn.select.html //! [`String`]: https://doc.rust-lang.org/std/string/struct.String.html //! [`Josa`]: enum.Josa.html use std::convert::TryFrom; use std::ops::{Add, AddAssign}; use hangul::HangulExt; mod error; pub use error::{Error, Result}; pub use Josa::*; // First group const EUN: &str = "은"; const NEUN: &str = "는"; const I: &str = "이"; const GA: &str = "가"; const EUL: &str = "을"; const REUL: &str = "를"; const GWA: &str = "과"; const WA: &str = "와"; // Second group const EU: &str = "으"; enum JongseongKind { Open, Rieul, Closed } impl TryFrom<char> for JongseongKind { type Error = Error; fn try_from(value: char) -> Result<JongseongKind> { match value.jongseong()? { Some(jongseong) => match jongseong { 'ㄹ' => Ok(JongseongKind::Rieul), _ => Ok(JongseongKind::Closed) }, None => Ok(JongseongKind::Open) } } } /// Enum of [josas](https://en.wikipedia.org/wiki/Korean_grammar#Postpositions) that are selected depending on the string in front of it. #[derive(Clone, Copy)] pub enum Josa { /// 은/는 EunNeun, /// 이/가 IGa, /// 을/를 EulReul, /// 과/와 GwaWa, /// 이다/다, 이나/나, 이란/란, 이든가/든가, 이나마/나마, 이야말로/야말로, 이랑/랑, 이여/여, 이며/며 I, /// 으로/로, 으로서/로서, 으로써/로써, 으로부터/로부터 Eu } impl Josa { fn select(self, c: char) -> Result<&'static str> { match JongseongKind::try_from(c)? { JongseongKind::Open => Ok(self.open()), JongseongKind::Rieul => Ok(self.rieul()), JongseongKind::Closed => Ok(self.closed()) } } fn open(self) -> &'static str { match self { Josa::EunNeun => NEUN, Josa::IGa => GA, Josa::EulReul => REUL, Josa::GwaWa => WA, Josa::I => "", Josa::Eu => "" } } fn rieul(self) -> &'static str { match self { Josa::EunNeun => EUN, Josa::IGa => I, Josa::EulReul => EUL, Josa::GwaWa => GWA, Josa::I => I, Josa::Eu => "" } } fn closed(self) -> &'static str { match self { Josa::EunNeun => EUN, Josa::IGa => I, Josa::EulReul => EUL, Josa::GwaWa => GWA, Josa::I => I, Josa::Eu => EU } } fn both(self) -> &'static str { match self { Josa::EunNeun => "은(는)", Josa::IGa => "이(가)", Josa::EulReul => "을(를)", Josa::GwaWa => "와(과)", Josa::I => "(이)", Josa::Eu => "(으)" } } } /// Select appropriate josa for a string. /// /// It is useful when you are trying to append a josa to formatted text such as `<span>고양이</span>`. /// If you try to append a josa to `<span>고양이</span>`, it results in [`Error`](enum.Error.html) because of the last character `>`. /// With this method, you can first get an appropriate josa, and then format the text with the that: /// ```rust /// use josa::select; /// use josa::IGa; /// # use josa::Error; /// /// let cat = "고양이"; /// let josa = select(cat, IGa)?; /// /// let cat = format!(r#"<span class="bold">{}</span>{}"#, cat, josa); /// /// assert_eq!(cat, r#"<span class="bold">고양이</span>가"#); /// # Ok::<(), Error>(()) /// ``` /// /// # Errors /// If given String is an empty String or last character is not a Haugul Syllable, it returns [`Error`](enum.Error.html) /// /// # Example /// ``` /// use josa::select; /// use josa::EunNeun; /// # use josa::Error; /// /// assert_eq!(select("사냥꾼", EunNeun)?, "은"); /// # Ok::<(), Error>(()) /// ``` pub fn select(noun: &str, josa: Josa) -> Result<&'static str> { josa.select( noun.chars().last().ok_or(Error::EmptyStr)? ) } /// An extension trait to add [`push_josa`](trait.JosaExt.html#tymethod.push_josa) method to [`String`](https://doc.rust-lang.org/std/string/struct.String.html). pub trait JosaExt { fn push_josa(&mut self, josa: Josa); } impl JosaExt for String { /// Appends a given [`Josa`] onto the end of this [`String`]. /// /// Note that it has [edge cases](index.html#edge-cases). /// /// [`Josa`]: enum.Josa.html /// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html fn push_josa(&mut self, josa: Josa) { let josa = match select(self, josa) { Ok(josa) => josa, Err(err) => match err { Error::EmptyStr => "", Error::ParseSyllable(_) => josa.both() } }; self.push_str(josa); } } 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); } }