lekhani_parser/
compiler.rs1use serde_json::Value;
4use super::rule::{ConditionScope, MatchType, Pattern, PatternRule, RuleCondition};
5use super::trie::PatternTrie;
6
7#[derive(Debug, Clone)]
8pub struct CompiledLayout {
9 pub patterns: Vec<Pattern>,
10 pub trie: PatternTrie,
11}
12
13impl CompiledLayout {
14 pub fn from_json(val: &Value) -> Result<Self, &'static str> {
15 let layout_obj = if let Some(l) = val.get("layout") {
16 l
17 } else {
18 val
19 };
20
21 let patterns_val = layout_obj
22 .get("patterns")
23 .and_then(|p| p.as_array())
24 .ok_or("Missing patterns array in layout JSON")?;
25
26 let mut patterns = Vec::with_capacity(patterns_val.len());
27 let mut trie = PatternTrie::new();
28
29 for p_val in patterns_val {
30 let find = p_val
31 .get("find")
32 .and_then(|f| f.as_str())
33 .ok_or("Pattern missing 'find' field")?;
34 let default_replace = p_val
35 .get("replace")
36 .and_then(|r| r.as_str())
37 .ok_or("Pattern missing 'replace' field")?;
38
39 let rules_val = p_val.get("rules").and_then(|r| r.as_array());
40 let mut pattern_rules = Vec::new();
41
42 if let Some(r_arr) = rules_val {
43 for r_item in r_arr {
44 let replace = r_item
45 .get("replace")
46 .and_then(|rep| rep.as_str())
47 .ok_or("Rule missing 'replace' field")?;
48 let matches_val = r_item
49 .get("matches")
50 .and_then(|m| m.as_array())
51 .ok_or("Rule missing 'matches' field")?;
52
53 let mut conditions = Vec::with_capacity(matches_val.len());
54 for m_item in matches_val {
55 let type_str = m_item
56 .get("type")
57 .and_then(|t| t.as_str())
58 .ok_or("Match missing 'type' field")?;
59 let mut scope_str = m_item
60 .get("scope")
61 .and_then(|s| s.as_str())
62 .ok_or("Match missing 'scope' field")?;
63
64 let match_type = match type_str {
65 "prefix" => MatchType::Prefix,
66 "suffix" => MatchType::Suffix,
67 _ => return Err("Invalid match type"),
68 };
69
70 let is_negative = if scope_str.starts_with('!') {
71 scope_str = &scope_str[1..];
72 true
73 } else {
74 false
75 };
76
77 let scope = match scope_str {
78 "vowel" => ConditionScope::Vowel,
79 "consonant" => ConditionScope::Consonant,
80 "punctuation" => ConditionScope::Punctuation,
81 "number" => ConditionScope::Number,
82 "exact" => ConditionScope::Exact,
83 _ => return Err("Unknown condition scope"),
84 };
85
86 let exact_val = m_item
87 .get("value")
88 .and_then(|v| v.as_str())
89 .unwrap_or_default();
90
91 conditions.push(RuleCondition {
92 match_type,
93 scope,
94 is_negative,
95 exact_value: exact_val.into(),
96 });
97 }
98
99 pattern_rules.push(PatternRule {
100 conditions,
101 replace: replace.into(),
102 });
103 }
104 }
105
106 let may_have_combining = default_replace.contains('\u{09BC}')
107 || default_replace.contains('ঁ')
108 || pattern_rules
109 .iter()
110 .any(|r| r.replace.contains('\u{09BC}') || r.replace.contains('ঁ'));
111
112 let pat_idx = patterns.len() as u16;
113 trie.insert(find.as_bytes(), pat_idx);
114
115 patterns.push(Pattern {
116 find: find.into(),
117 default_replace: default_replace.into(),
118 rules: pattern_rules,
119 may_have_combining,
120 });
121 }
122
123 Ok(Self { patterns, trie })
124 }
125}