Skip to main content

BlockComposer

Struct BlockComposer 

Source
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

Source

pub fn new() -> Self

Creates a new, empty BlockComposer.

Source

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.

Source

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
);
Source

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
);
Source

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,
   }))
);
Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BlockComposer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for BlockComposer

Source§

impl PartialEq for BlockComposer

Source§

fn eq(&self, other: &BlockComposer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for BlockComposer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.