Skip to main content

Crate bamboo_core

Crate bamboo_core 

Source
Expand description

§Bamboo Core Rust

A high-performance Vietnamese input method engine (IME) core, ported from the original bamboo-core in Go.

This crate provides the foundational logic for processing Vietnamese text, supporting various input methods like Telex, VNI, and VIQR. It is designed to be extremely fast, memory-efficient (zero-allocation in core processing), and easy to integrate into UI applications or other text processing tools.

§Core Features

  • Hybrid Engine: Combines rule-based flexibility with DFA-based speed.
  • Zero-Allocation: Core processing path avoids heap allocations for maximum performance.
  • Lazy JIT DFA: Learns and caches syllable states on-the-fly.
  • WASM Ready: Optimized for web and constrained environments.

§Core Concepts

  • Engine: The main stateful processor. You feed it characters/strings, and it maintains the internal composition state to produce the correctly marked Vietnamese text.
  • InputMethod: Defines the rules for transformations (e.g., how “as” becomes “á”). Built-in methods include InputMethod::telex(), InputMethod::vni(), etc.
  • Mode: Determines if the engine should process characters as Vietnamese or treat them as plain English.
  • OutputOptions: A bitmask to customize the flattened string output (e.g., lowercase, toneless, etc.).

§Quick Start

use bamboo_core::{Engine, Mode, InputMethod, OutputOptions};

// Create an engine with the standard Telex input method
let mut engine = Engine::new(InputMethod::telex());

// No explicit warm-up needed: the DFA learns on demand (lazy JIT)

// Process a string and get the output immediately
let word = engine.process("tieengs", Mode::Vietnamese);
assert_eq!(word, "tiếng");

// Reset for a new word
engine.reset();
let word2 = engine.process("vieetj", Mode::Vietnamese);
assert_eq!(word2, "việt");

§Advanced Usage

§Customizing Output

You can use OutputOptions to transform the result on the fly:

use bamboo_core::{Engine, Mode, InputMethod, OutputOptions};

let mut engine = Engine::new(InputMethod::telex());
engine.process_str("Trangws", Mode::Vietnamese);

// Get toneless version
let options = OutputOptions::TONE_LESS;
assert_eq!(engine.get_processed_str(options), "Trăng");

§Handling Backspaces

The engine supports removing the last transformation while maintaining DFA consistency:

engine.process_str("chuyeenr", Mode::Vietnamese);
assert_eq!(engine.output(), "chuyển");

// remove_last_char removes the last physical keystroke and its effects
engine.remove_last_char(true);
assert_eq!(engine.output(), "chuyên");

Modules§

advanced
Advanced types for low-level interaction with the engine.
ffi
C-Compatible FFI Layer for Bamboo Core.
wasm
WebAssembly (WASM) bindings for Bamboo Core.

Structs§

Config
Configuration options for the Bamboo engine.
Engine
The main stateful processor of the Vietnamese Input Method Engine.
InputMethod
A collection of rules defining how keys transform text.
OutputOptions
Customization options for the flattened string output.
Transformation
Represents a single keypress or a transformation derived from it (e.g., adding a mark or tone).
TransformationStack
A stack-allocated buffer for transformations to avoid heap allocations in the hot path.

Enums§

Mode
Represents the processing mode of the engine.