Skip to main content

StringComposer

Struct StringComposer 

Source
pub struct StringComposer { /* private fields */ }
Expand description

A composer struct that manages the composition of strings of text consisting of multiple words, including both Hangul words and non-Hangul text.

The StringComposer maintains both a string of completed text and a HangulWordComposer for the current word being composed. If the currently active word is a Hangul word that has not yet been completed, pushing or popping characters will interact with the HangulWordComposer and directly update syllable blocks. Otherwise, Unicode characters will be directly added to or removed from the completed string.

API:

use hangul_cd::string::StringComposer;

let mut composer = StringComposer::new();

// Push characters to form Hangul syllables
composer.push_char('ㅎ').unwrap();
composer.push_char('ㅏ').unwrap();

// Get the composed string
let result = composer.as_string().unwrap();
assert_eq!(result, "하".to_string());

// Push non-Hangul characters
composer.push_char(' ').unwrap();
composer.push_char('!').unwrap();
assert_eq!(composer.as_string().unwrap(), "하 !".to_string());

// Popping non-Hangul characters removes them from the completed string
composer.pop().unwrap(); // removes '!'
composer.pop().unwrap(); // removes ' '
assert_eq!(composer.as_string().unwrap(), "하".to_string());

// Popping Hangul characters after they've been completed removes entire syllables
composer.pop().unwrap(); // removes '하'
assert_eq!(composer.as_string().unwrap(), "".to_string());

// Popping characters while a Hangul word is active removes jamo
composer.push_char('ㅂ').unwrap();
composer.push_char('ㅏ').unwrap();
composer.push_char('ㅂ').unwrap();
composer.pop().unwrap(); // removes 'ㅂ'
assert_eq!(composer.as_string().unwrap(), "바".to_string());

Implementations§

Source§

impl StringComposer

Source

pub fn new() -> Self

Creates a new, empty StringComposer.

Source

pub fn push_char(&mut self, c: char) -> Result<(), StringError>

Pushes a character to the StringComposer.

If the character is part of a Hangul word, it will be composed into syllables. Otherwise, it will be added directly to the completed string.

Source

pub fn as_string(&self) -> Result<String, StringError>

Returns the composed string, combining completed text and the current word.

Source

pub fn pop(&mut self) -> Result<Option<char>, StringError>

Pops the last character from the StringComposer and returns it wrapped within a Result and Option.

If the current word is a Hangul word with uncompleted syllables, it will remove the last jamo from the current syllable block. Otherwise, it will remove the last character from the completed string.

Trait Implementations§

Source§

impl Debug for StringComposer

Source§

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

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

impl Default for StringComposer

Source§

fn default() -> Self

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

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.