braillify 2.1.0

Rust 기반 크로스플랫폼 한국어 점역 라이브러리
Documentation
use crate::split;

pub fn build_char(choseong: char, jungseong: char, jongseong: Option<char>) -> char {
    let choseong_list = [
        '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
        '', '', '', '',
    ];
    let jongseong_list = [
        '\0', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
        '', '', '', '', '', '', '', '', '', '', '', '', '',
    ];
    let choseong_index = choseong_list.iter().position(|&c| c == choseong).unwrap();
    let jungseong_index = jungseong as usize - 0x314F;
    let jongseong_index = if let Some(jongseong) = jongseong {
        jongseong_list.iter().position(|&c| c == jongseong).unwrap()
    } else {
        0
    };
    let hangul_code =
        0xAC00 + (choseong_index * 21 * 28) + (jungseong_index * 28) + jongseong_index;
    char::from_u32(hangul_code as u32).unwrap()
}

pub fn has_choseong_o(ch: char) -> bool {
    if let Ok(split) = split::split_korean_char(ch) {
        return split[0].get_char() == '';
    }
    false
}

pub fn is_korean_char(c: char) -> bool {
    (c as u32 >= 0x3131 && c as u32 <= 0x318E) || (0xAC00 <= c as u32 && c as u32 <= 0xD7A3)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_build_char() {
        assert_eq!(build_char('', '', Some('')), '');
        assert_eq!(build_char('', '', Some('')), '');
    }

    #[test]
    fn test_has_choseong_o() {
        assert!(has_choseong_o(''));
        assert!(!has_choseong_o(''));
        assert!(has_choseong_o(''));
        assert!(!has_choseong_o(''));
        assert!(has_choseong_o(''));
    }

    #[rstest::rstest]
    #[case::first_jamo('ㄱ', true)]
    #[case::last_jamo('ㆎ', true)]
    #[case::before_jamo('㄰', false)]
    #[case::after_jamo('㆏', false)]
    #[case::first_hangul_syllable('가', true)]
    #[case::last_hangul_syllable('힣', true)]
    #[case::before_hangul_syllable('꯿', false)]
    #[case::after_hangul_syllable('힤', false)]
    fn test_is_korean_char(#[case] input: char, #[case] expected: bool) {
        assert_eq!(is_korean_char(input), expected);
    }
}