pub struct BlockComposer { /* private fields */ }Expand description
A composer for a single Hangul syllable block. Used to build a block by pushing and popping Jamo letters.
API:
use hangul_cd::block::{BlockComposer, BlockPushResult};
use hangul_cd::jamo::{Jamo, JamoConsonantSingular, JamoVowelSingular};
let mut composer = BlockComposer::new();
// Push letters to form the syllable '강'
assert_eq!(
composer.push(&Jamo::Consonant(JamoConsonantSingular::Giyeok)),
BlockPushResult::Success
);
assert_eq!(
composer.push(&Jamo::Vowel(JamoVowelSingular::A)),
BlockPushResult::Success
);
assert_eq!(
composer.push(&Jamo::Consonant(JamoConsonantSingular::Ieung)),
BlockPushResult::Success
);
// Try to push another character that would not fit in the current block
assert_eq!(
composer.push(&Jamo::Vowel(JamoVowelSingular::A)),
BlockPushResult::PopAndStartNewBlock
);
// Get the composed block as a character
let block_char = composer.block_as_string().unwrap();
assert_eq!(block_char, Some('강'));Implementations§
Source§impl BlockComposer
impl BlockComposer
Sourcepub fn push(&mut self, letter: &Jamo) -> BlockPushResult
pub fn push(&mut self, letter: &Jamo) -> BlockPushResult
Tries to push a Jamo letter into the BlockComposer.
Returns a BlockPushResult indicating the outcome of the operation.
If the letter could not be pushed, the state of the current block will
remain unchanged.
Sourcepub fn push_char(&mut self, c: char) -> Result<BlockPushResult, BlockError>
pub fn push_char(&mut self, c: char) -> Result<BlockPushResult, BlockError>
Tries to push a character into the BlockComposer. If the character
corresponds to a Hangul Jamo letter, it is pushed into the composer.
If the character is not Hangul, BlockPushResult::NonHangul is returned.
Example:
use hangul_cd::block::{BlockComposer, BlockPushResult};
use hangul_cd::jamo::{Jamo, JamoConsonantSingular, JamoVowelSingular};
let mut composer = BlockComposer::new();
// Push letters to form the syllable '강'
assert_eq!(
composer.push_char('ㄱ').unwrap(),
BlockPushResult::Success
);
assert_eq!(
composer.push_char('ㅏ').unwrap(),
BlockPushResult::Success
);
assert_eq!(
composer.push_char('ㅇ').unwrap(),
BlockPushResult::Success
);
// Try to push another character that would not fit in the current block
assert_eq!(
composer.push_char('ㅏ').unwrap(),
BlockPushResult::PopAndStartNewBlock
);Sourcepub fn pop(&mut self) -> BlockPopStatus
pub fn pop(&mut self) -> BlockPopStatus
Pops a Jamo letter from the BlockComposer. Returns a BlockPopStatus
indicating the outcome of the operation, with values:
PoppedAndNonEmpty(Jamo): A Jamo letter was popped and the block still has letters remaining.PoppedAndEmpty(Jamo): A Jamo letter was popped and the block is now empty.None: The block is already empty; no letters to pop.
Example:
use hangul_cd::block::{BlockComposer, BlockPopStatus};
use hangul_cd::jamo::{Jamo, JamoConsonantSingular, JamoVowelSingular};
let mut composer = BlockComposer::new();
composer.push(&Jamo::from_compatibility_jamo('ㄱ').unwrap());
composer.push(&Jamo::from_compatibility_jamo('ㅏ').unwrap());
assert_eq!(
composer.pop(),
BlockPopStatus::PoppedAndNonEmpty(Jamo::Vowel(JamoVowelSingular::A))
);
assert_eq!(
composer.pop(),
BlockPopStatus::PoppedAndEmpty(Jamo::Consonant(JamoConsonantSingular::Giyeok))
);
assert_eq!(
composer.pop(),
BlockPopStatus::None
);Sourcepub fn try_as_complete_block(&self) -> Result<BlockCompletionStatus, BlockError>
pub fn try_as_complete_block(&self) -> Result<BlockCompletionStatus, BlockError>
Attempts to convert the current state of the BlockComposer
into a complete HangulBlock. If the block is incomplete,
it returns an Incomplete status with the last Jamo character
added. If the block is empty, it returns an Empty status.
Example:
use hangul_cd::block::{BlockComposer, BlockCompletionStatus, HangulBlock};
use hangul_cd::jamo::{Jamo, JamoConsonantSingular, JamoVowelSingular};
let mut composer = BlockComposer::new();
composer.push(&Jamo::from_compatibility_jamo('ㄱ').unwrap());
// Attempt to complete incomplete block
assert_eq!(
composer.try_as_complete_block(),
Ok(BlockCompletionStatus::Incomplete(Jamo::Consonant(JamoConsonantSingular::Giyeok)))
);
composer.push(&Jamo::from_compatibility_jamo('ㅏ').unwrap());
// Get the complete block now that a vowel has been added
assert_eq!(
composer.try_as_complete_block(),
Ok(BlockCompletionStatus::Complete(HangulBlock {
initial: Jamo::Consonant(JamoConsonantSingular::Giyeok),
vowel: Jamo::Vowel(JamoVowelSingular::A),
final_optional: None,
}))
);Sourcepub fn block_as_string(&self) -> Result<Option<char>, BlockError>
pub fn block_as_string(&self) -> Result<Option<char>, BlockError>
Returns the composed Hangul syllable character as an Option<char>
wrapped in a Result. If the block is complete, it returns the composed
character. If the block is incomplete, it returns the Jamo currently in
the block (in modern Unicode form, not compatibility form). If the block is empty,
it returns None.
Sourcepub fn from_composed_block(block: &HangulBlock) -> Result<Self, BlockError>
pub fn from_composed_block(block: &HangulBlock) -> Result<Self, BlockError>
Creates a BlockComposer from an existing HangulBlock,
decomposing it into its constituent Jamo characters.
Returns an error if decomposition fails.
Trait Implementations§
Source§impl Debug for BlockComposer
impl Debug for BlockComposer
Source§impl Default for BlockComposer
impl Default for BlockComposer
impl Eq for BlockComposer
Source§impl PartialEq for BlockComposer
impl PartialEq for BlockComposer
Source§fn eq(&self, other: &BlockComposer) -> bool
fn eq(&self, other: &BlockComposer) -> bool
self and other values to be equal, and is used by ==.