hangul-cd
(hangul compose and decompose)
Rust helpers for composing and decomposing Hangul syllable blocks and words. This crate xposes small, focused modules for combining jamo into syllables, grouping syllables into words, and mixing Hangul with arbitrary text while enforcing valid Unicode Hangul composition rules.
Modules
hangul-cd focuses heavily on composition through a modular wrapper approach. There are four modules, each providing an API wrapper layer over the previous one:
jamo- The Jamo layer provides utilities for working directly with individual Hangul Jamo; Jamo are stored as enum values, allowing for instantiation from and conversion to either Modern or Compatibility Unicode codepoints, as well as helpers for classification of Jamo.block- The Block layer provides aHangulBlockstruct for composing syllable blocks from individual Jamo, as well as aBlockComposerwhich allows callers to use a simple stack-like push-pop interface to add to or remove from a given block, and functions to convert blocks to Unicode codepoints, as well as take completed blocks and decompose them into individual Modern or Compatibility Jamo codepoints.word- The Word layer wraps the Block layer by keeping track of a list of Hangul blocks and extends the push-pop mechanism from the Block layer, allowing callers to create full Hangul words composed of multiple syllable blocks simply by pushing Jamo repeatedly and print to Unicode codepoints.string- The String layer allows for mixing of Hangul and non-Hangul text and continues to make use of the push-pop mechanism from previous layers.
jamo
Work with individual Hangul letters, normalize compatibility codepoints, and compose or decompose composite Jamo.
use ;
// Convert compatibility to modern codepoints or classify any char
assert_eq!;
assert!;
// Build and inspect Jamo values directly; works with either
// compatibility or modern Jamo Unicode codepoints
let jamo = from_compatibility_jamo.unwrap;
assert_eq!;
// Compose composite consonants programmatically
let double = Giyeok.combine_for_initial;
assert_eq!;
block
Compose and decompose single syllable blocks.
use ;
use ;
// Use `BlockComposer` to easily work with `HangulBlock`s
let mut composer = new;
// Push directly using Jamo or with chars
composer.push;
composer.push;
composer.push_char;
// Complete `BlockComposer` blocks as `HangulBlock` structs
let block = match composer.try_as_complete_block.unwrap ;
// Convert `HangulBlock`s to syllables
assert_eq!;
// Decompose `HangulBlock`s into their constituent characters
let opts = HangulBlockDecompositionOptions ;
assert_eq!;
word
Compose multiple syllables into a word with push/pop semantics.
use ;
// Use `HangulWordComposer` to push and pop Jamo to words
let mut word = new;
for c in "ㅇㅏㄴㄴㅕㅇ".chars
assert_eq!;
// Backspace-like pops remove Jamo from a word one at a time
word.pop.unwrap;
assert_eq!;
string
Mix Hangul words with arbitrary text.
use StringComposer;
// Use a `StringComposer` to push and pop both Hangul and non-Hangul chars
let mut s = new;
for c in "ㅎㅏㄴㄱㅡㄹ rocks".chars
assert_eq!;
s.pop.unwrap; // remove 's'
s.push_char.unwrap;
assert_eq!;
Quick start
Add the crate to your project (for a local path):
[]
= { = "lib" }
Compose a single block or whole words:
use HangulBlock;
use StringComposer;
use Jamo;
// Manual block construction
let block = HangulBlock ;
assert_eq!;
// Stream jamo into a string
let mut composer = new;
for c in "ㅎㅏㄴㄱㅡㄹ ㅇㅏㄴㄴㅕㅇ!".chars
assert_eq!;
// Pop behaves like backspace
composer.pop.unwrap; // removes '!'
assert_eq!;
Work directly with jamo or compatibility jamo:
use ;
assert_eq!; // ㄱ -> modern jamo
assert_eq!;
assert!;
Testing
From lib/, run the library test suite: