Skip to content

Commit

Permalink
Merge pull request #4 from yjh0502/master
Browse files Browse the repository at this point in the history
Fix an error on Choseong::from_jamo
  • Loading branch information
bekker committed Nov 6, 2020
2 parents 32fa5cb + df6a593 commit e750463
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 231 deletions.
9 changes: 5 additions & 4 deletions examples/decompose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
// particle: 이 follows consonants, and 가 follows vowels.
let post_position = match hangeul::ends_in_consonant(subject).unwrap() {
true => "이",
false => "가"
false => "가",
};

// -> A wild pikachu has appeared!
Expand All @@ -17,9 +17,10 @@ fn main() {

// get_lead is an alias of get_choseong, to get the first character
// of a Hangeul syllable.
let sentence_in_choseong = sentence.chars()
.map(|c| hangeul::get_lead(&c).unwrap_or(c))
.collect::<String>();
let sentence_in_choseong = sentence
.chars()
.map(|c| hangeul::get_lead(&c).unwrap_or(c))
.collect::<String>();

println!("{}", sentence_in_choseong); // ㅇㅅㅇ ㅍㅋㅊㄱ ㄴㅌㄴㄷ!
}
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{fmt, result};
use std::error;
use std::{fmt, result};

pub type Result<T> = result::Result<T, HangeulError>;

Expand Down
39 changes: 14 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod constants;
pub mod constants;
pub mod errors;
pub mod models;

use crate::constants::*;
use crate::models::*;
use crate::errors::*;
use crate::models::*;

/// Check if the u32 is a finished/composed Hangeul syllable.
/// Returns true for the `0xAC00` to `0xD7A3` range.
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn to_hangeul_u32(c: &char) -> Result<u32> {
pub fn get_choseong(c: &char) -> Result<char> {
match Choseong::from_char(c) {
Some(cho) => Ok(cho.to_char()),
None => Err(HangeulError::JamoNotFound)
None => Err(HangeulError::JamoNotFound),
}
}
/// Alias for get_choseong.
Expand All @@ -144,7 +144,7 @@ pub use self::get_choseong as get_lead;
pub fn get_jungseong(c: &char) -> Result<char> {
match Jungseong::from_char(c) {
Some(jung) => Ok(jung.to_char()),
None => Err(HangeulError::JamoNotFound)
None => Err(HangeulError::JamoNotFound),
}
}
/// Alias for get_jungseong.
Expand All @@ -164,7 +164,7 @@ pub use self::get_jungseong as get_middle;
pub fn get_jongseong(c: &char) -> Result<char> {
match Jongseong::from_char(c) {
Some(jong) => Ok(jong.to_char()),
None => Err(HangeulError::JamoNotFound)
None => Err(HangeulError::JamoNotFound),
}
}
/// Alias for get_jongseong.
Expand Down Expand Up @@ -263,10 +263,7 @@ type Decomposed = (char, char, Option<char>);
/// assert_eq!(decomposed, decompose(dae_han));
/// ```
pub fn decompose(content: &str) -> Vec<Result<Decomposed>> {
content
.chars()
.map(|c| decompose_char(&c))
.collect()
content.chars().map(|c| decompose_char(&c)).collect()
}

/// Attempts to decompose a char. Errors if the first and second glyphs
Expand All @@ -288,14 +285,9 @@ pub fn decompose_char(c: &char) -> Result<Decomposed> {
None => None,
};

Ok((
choseong,
jungseong,
jongseong
))
Ok((choseong, jungseong, jongseong))
}


/// Attempts to compose a Hangeul character (in the `Hangeul Syllable` unicode range,
/// `AC00`–`D7A3`). See [Hangeul Syllables](https://en.wikipedia.org/wiki/Hangul_Syllables).
///
Expand All @@ -311,26 +303,23 @@ pub fn decompose_char(c: &char) -> Result<Decomposed> {
pub fn compose_char(choseong: &char, jungseong: &char, jongseong: Option<&char>) -> Result<char> {
let cho_code = match Choseong::from_char(choseong) {
Some(cho) => cho.composable_u32(),
None => return Err(HangeulError::Uncomposable)
None => return Err(HangeulError::Uncomposable),
};

let jung_code = match Jungseong::from_char(jungseong) {
Some(jung) => jung.composable_u32(),
None => return Err(HangeulError::Uncomposable)
None => return Err(HangeulError::Uncomposable),
};

let jong_code = match jongseong {
Some(c) => {
match Jongseong::from_char(c) {
Some(jong) => jong.composable_u32(),
None => 0,
}
Some(c) => match Jongseong::from_char(c) {
Some(jong) => jong.composable_u32(),
None => 0,
},
None => 0
None => 0,
};

let code = cho_code + jung_code + jong_code + HANGEUL_OFFSET;

std::char::from_u32(code)
.ok_or_else(|| HangeulError::Uncomposable)
std::char::from_u32(code).ok_or_else(|| HangeulError::Uncomposable)
}
Loading

0 comments on commit e750463

Please sign in to comment.