pub struct Engine { /* private fields */ }Expand description
The main stateful processor of the Vietnamese Input Method Engine.
It maintains an internal buffer of transformations and produces the correctly marked Vietnamese text. The engine uses a hybrid approach combining a Rule Engine with a Lazy JIT DFA for peak performance.
Implementations§
Source§impl Engine
impl Engine
Sourcepub fn new(input_method: InputMethod) -> Self
pub fn new(input_method: InputMethod) -> Self
Creates a new engine with the specified input method and default configuration.
Sourcepub fn with_config(input_method: InputMethod, config: Config) -> Self
pub fn with_config(input_method: InputMethod, config: Config) -> Self
Creates a new engine with a specific input method and configuration.
Sourcepub fn set_config(&mut self, config: Config)
pub fn set_config(&mut self, config: Config)
Updates the engine configuration.
Sourcepub fn input_method(&self) -> InputMethod
pub fn input_method(&self) -> InputMethod
Returns a copy of the current input method.
Sourcepub fn warm_up(&mut self)
👎Deprecated since 0.3.4: Engine::warm_up() is unstable and may be removed. It uses a Telex-biased heuristic and may regress cold-start or non-Telex workloads.
pub fn warm_up(&mut self)
Engine::warm_up() is unstable and may be removed. It uses a Telex-biased heuristic and may regress cold-start or non-Telex workloads.
Warms up the DFA by pre-compiling common Vietnamese syllables.
This API is intentionally unstable and currently uses a Telex-biased heuristic corpus. It can help long-lived Telex sessions, but may hurt cold-start latency or non-Telex/custom input methods.
Prefer relying on the default lazy JIT behavior unless you have benchmark data for your production workload.
Sourcepub fn process(&mut self, s: &str, mode: Mode) -> String
pub fn process(&mut self, s: &str, mode: Mode) -> String
Processes a string of characters and returns the resulting active word.
This is a convenience wrapper around Self::process_str and Self::output.
Sourcepub fn process_str(&mut self, s: &str, mode: Mode) -> &Self
pub fn process_str(&mut self, s: &str, mode: Mode) -> &Self
Processes a string of characters and returns a reference to the engine.
Sourcepub fn process_key_delta(
&mut self,
key: char,
mode: Mode,
) -> (usize, usize, &str)
pub fn process_key_delta( &mut self, key: char, mode: Mode, ) -> (usize, usize, &str)
Processes a single key and returns a 3-way diff for efficient text editor updates.
Instead of rewriting the entire preedit, the frontend only needs to apply:
- Keep the common prefix unchanged.
- Delete
backspace_countcharacters from the end of the previous preedit. - Append
inserted_suffix.
previous_preedit = [common_prefix] + [backspace_count chars to delete]
new_preedit = [common_prefix] + [inserted_suffix]The common prefix length is implicit: previous_preedit.len() - backspace_count
(in characters). The frontend does not need to compute LCP/LCS — the engine does it.
§Returns
(backspace_count, backspaces_bytes, inserted_suffix):
backspace_count: Number of characters to delete from the end of the previous preedit.backspaces_bytes: Number of UTF-8 bytes to delete (for byte-oriented editors).inserted_suffix: The new string to append after deletion.
§Example
use bamboo_core::{Engine, Mode, InputMethod};
let mut engine = Engine::new(InputMethod::telex());
let (bs, _, ins) = engine.process_key_delta('a', Mode::Vietnamese);
assert_eq!(bs, 0);
assert_eq!(ins, "a");
let (bs, _, ins) = engine.process_key_delta('s', Mode::Vietnamese);
// previous = "a", new = "á"
// keep prefix = 1 - 1 = 0, delete = 1 ("a"), insert = "á"
assert_eq!(bs, 1);
assert_eq!(ins, "á");Sourcepub fn process_key_delta_into(
&mut self,
key: char,
mode: Mode,
inserted: &mut String,
) -> usize
pub fn process_key_delta_into( &mut self, key: char, mode: Mode, inserted: &mut String, ) -> usize
Similar to Self::process_key_delta, but writes the inserted string into a provided buffer.
§Returns
backspace_count — number of characters to delete from the end of the previous preedit.
Sourcepub fn process_key(&mut self, key: char, mode: Mode)
pub fn process_key(&mut self, key: char, mode: Mode)
Processes a single character.
The mode determines whether to apply Vietnamese transformation rules.
Sourcepub fn commit(&mut self)
pub fn commit(&mut self)
Clears the active syllable buffer and appends it to the committed text.
Sourcepub fn get_processed_str(&self, options: OutputOptions) -> String
pub fn get_processed_str(&self, options: OutputOptions) -> String
Returns the processed string according to the specified options.
This can be used to get the full text (committed + active) or variations like toneless text.
Sourcepub fn is_valid(&self, input_is_full_complete: bool) -> bool
pub fn is_valid(&self, input_is_full_complete: bool) -> bool
Checks if the current composition forms a valid Vietnamese syllable.
Sourcepub fn restore_last_word(&mut self, to_vietnamese: bool)
pub fn restore_last_word(&mut self, to_vietnamese: bool)
Restores the last word in the composition to its un-transformed state.
If to_vietnamese is true, it attempts to re-apply Vietnamese transformations.
Sourcepub fn remove_last_char(&mut self, refresh_last_tone_target: bool)
pub fn remove_last_char(&mut self, refresh_last_tone_target: bool)
Removes the last character from the active composition.