Skip to main content

lekhani_parser/
lib.rs

1//! # Lekhani Parser
2//!
3//! Ultra-fast, zero-allocation pure Rust Bengali phonetic grammar parser and transliterator.
4//! High-performance replacement for `rupantor` with Longest-Prefix L1 Trie matching,
5//! branchless byte-level condition evaluations, and canonical diacritic sequencing.
6
7pub mod compiler;
8pub mod engine;
9pub mod rule;
10pub mod tables;
11pub mod trie;
12
13#[cfg(feature = "wasm")]
14pub mod wasm;
15
16pub use compiler::CompiledLayout;
17pub use engine::LekhaniParser;
18pub use rule::{ConditionScope, MatchType, Pattern, PatternRule, RuleCondition};
19
20use std::sync::{Arc, OnceLock};
21
22/// Cached singleton of the compiled default Avro Phonetic layout.
23/// Initialized once, zero allocations and zero disk I/O on subsequent accesses.
24static DEFAULT_AVRO_LAYOUT: OnceLock<Arc<CompiledLayout>> = OnceLock::new();
25
26/// Embedded standard Avro Phonetic layout JSON
27const EMBEDDED_AVRO_JSON: &str = include_str!("../data/avrophonetic.json");
28
29/// Retrieve or initialize the shared singleton of the compiled default Avro layout.
30pub fn get_default_avro_layout() -> Arc<CompiledLayout> {
31    DEFAULT_AVRO_LAYOUT
32        .get_or_init(|| {
33            let json: serde_json::Value = serde_json::from_str(EMBEDDED_AVRO_JSON)
34                .expect("Corrupt embedded avrophonetic.json layout");
35            let layout = CompiledLayout::from_json(&json)
36                .expect("Failed to compile embedded avrophonetic.json");
37            Arc::new(layout)
38        })
39        .clone()
40}
41
42/// Create a new `LekhaniParser` with the default Avro Phonetic layout.
43pub fn default_avro_parser() -> LekhaniParser {
44    LekhaniParser::new(get_default_avro_layout())
45}