axo-bridge 0.1.4

Lua↔Rust bridge for Axo Framework: serializes Lua UI trees to Rust nodes, registers device APIs and event callbacks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use mlua::prelude::*;

#[derive(Debug, Clone)]
pub struct UiNode {
    pub id: u64,
    pub node_type: String,
    pub content: String,
    pub style: StyleMap,
    pub children: Vec<UiNode>,
    pub on_change_text_id: String,
}

#[derive(Debug, Clone)]
pub struct StyleMap {
    pub width: Option<LengthValue>,
    pub height: Option<LengthValue>,
    pub min_width: Option<LengthValue>,
    pub max_width: Option<LengthValue>,
    pub min_height: Option<LengthValue>,
    pub max_height: Option<LengthValue>,
    pub background_color: Option<[f32; 4]>,
    pub margin: Option<RectAuto>,
    pub padding: Option<Rect>,
    pub flex_direction: Option<FlexDirection>,
    pub justify_content: Option<JustifyContent>,
    pub align_items: Option<AlignItems>,
    pub align_self: Option<AlignSelf>,
    pub align_content: Option<AlignContent>,
    pub flex_wrap: Option<FlexWrap>,
    pub flex_grow: Option<f32>,
    pub flex_shrink: Option<f32>,
    pub gap: Option<f32>,
    pub position: Option<PositionType>,
    pub top: Option<LengthValue>,
    pub left: Option<LengthValue>,
    pub right: Option<LengthValue>,
    pub bottom: Option<LengthValue>,
    pub border_width: Option<f32>,
    pub border_color: Option<[f32; 4]>,
    pub font_size: Option<f32>,
    pub color: Option<[f32; 4]>,
    pub border_radius: Option<f32>,
    pub on_click_id: String,
    pub hover_background_color: Option<[f32; 4]>,
    pub active_background_color: Option<[f32; 4]>,
    pub hover_color: Option<[f32; 4]>,
    pub active_color: Option<[f32; 4]>,
    pub disabled: bool,
}

#[derive(Debug, Clone)]
pub enum LengthValue {
    Pixels(f32),
    Percent(f32),
}

#[derive(Debug, Clone)]
pub struct RectAuto {
    pub left: AutoLength,
    pub right: AutoLength,
    pub top: AutoLength,
    pub bottom: AutoLength,
}

#[derive(Debug, Clone)]
pub enum AutoLength {
    Length(f32),
    Percent(f32),
    Auto,
}

#[derive(Debug, Clone)]
pub struct Rect {
    pub left: f32,
    pub right: f32,
    pub top: f32,
    pub bottom: f32,
}

#[derive(Debug, Clone)]
pub enum FlexDirection {
    Row,
    Column,
    RowReverse,
    ColumnReverse,
}

#[derive(Debug, Clone)]
pub enum JustifyContent {
    FlexStart,
    FlexEnd,
    Center,
    SpaceBetween,
    SpaceAround,
    SpaceEvenly,
}

#[derive(Debug, Clone)]
pub enum AlignItems {
    FlexStart,
    FlexEnd,
    Center,
    Stretch,
    Baseline,
}

#[derive(Debug, Clone)]
pub enum FlexWrap {
    NoWrap,
    Wrap,
    WrapReverse,
}

#[derive(Debug, Clone)]
pub enum AlignSelf {
    Auto,
    FlexStart,
    FlexEnd,
    Center,
    Stretch,
    Baseline,
}

#[derive(Debug, Clone)]
pub enum AlignContent {
    FlexStart,
    FlexEnd,
    Center,
    Stretch,
    SpaceBetween,
    SpaceAround,
}

#[derive(Debug, Clone)]
pub enum PositionType {
    Relative,
    Absolute,
}

impl Default for StyleMap {
    fn default() -> Self {
        Self::new()
    }
}

impl StyleMap {
    pub fn new() -> Self {
        StyleMap {
            width: None, height: None,
            min_width: None, max_width: None, min_height: None, max_height: None,
            background_color: None,
            margin: None, padding: None,
            flex_direction: None, justify_content: None, align_items: None,
            align_self: None, align_content: None, flex_wrap: None,
            flex_grow: None, flex_shrink: None, gap: None,
            position: None, top: None, left: None, right: None, bottom: None,
            border_width: None, border_color: None,
            font_size: None, color: None,
            border_radius: None,
            on_click_id: String::new(),
            hover_background_color: None,
            active_background_color: None,
            hover_color: None,
            active_color: None,
            disabled: false,
        }
    }
}

fn parse_length(s: &str) -> Option<LengthValue> {
    let s = s.trim();
    if let Some(stripped) = s.strip_suffix('%') {
        let num: f32 = stripped.trim().parse().ok()?;
        Some(LengthValue::Percent(num / 100.0))
    } else {
        let num: f32 = s.parse().ok()?;
        Some(LengthValue::Pixels(num))
    }
}

fn parse_hex_color(s: &str) -> Option<[f32; 4]> {
    let s = s.trim().strip_prefix('#')?;
    if s.len() == 6 {
        let r = u8::from_str_radix(&s[0..2], 16).ok()?;
        let g = u8::from_str_radix(&s[2..4], 16).ok()?;
        let b = u8::from_str_radix(&s[4..6], 16).ok()?;
        Some([r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0])
    } else if s.len() == 8 {
        let r = u8::from_str_radix(&s[0..2], 16).ok()?;
        let g = u8::from_str_radix(&s[2..4], 16).ok()?;
        let b = u8::from_str_radix(&s[4..6], 16).ok()?;
        let a = u8::from_str_radix(&s[6..8], 16).ok()?;
        Some([r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a as f32 / 255.0])
    } else {
        None
    }
}

fn parse_rect(s: &str) -> Option<Rect> {
    let parts: Vec<f32> = s.split_whitespace().filter_map(|p| p.parse().ok()).collect();
    match parts.len() {
        1 => Some(Rect { left: parts[0], right: parts[0], top: parts[0], bottom: parts[0] }),
        2 => Some(Rect { left: parts[1], right: parts[1], top: parts[0], bottom: parts[0] }),
        4 => Some(Rect { left: parts[3], right: parts[1], top: parts[0], bottom: parts[2] }),
        _ => None,
    }
}

fn parse_rect_auto(s: &str) -> Option<RectAuto> {
    let parts: Vec<&str> = s.split_whitespace().collect();
    let parse = |s: &str| -> AutoLength {
        if s == "auto" { AutoLength::Auto }
        else if let Some(stripped) = s.strip_suffix('%') { AutoLength::Percent(stripped.parse().unwrap_or(0.0) / 100.0) }
        else { AutoLength::Length(s.parse().unwrap_or(0.0)) }
    };
    match parts.len() {
        1 => { let v = parse(parts[0]); Some(RectAuto { left: v.clone(), right: v.clone(), top: v.clone(), bottom: v }) }
        2 => { let v = parse(parts[0]); let h = parse(parts[1]); Some(RectAuto { top: v.clone(), bottom: v, left: h.clone(), right: h }) }
        4 => Some(RectAuto { top: parse(parts[0]), right: parse(parts[1]), bottom: parse(parts[2]), left: parse(parts[3]) }),
        _ => None,
    }
}

fn parse_flex_direction(s: &str) -> Option<FlexDirection> {
    match s {
        "row" => Some(FlexDirection::Row),
        "column" => Some(FlexDirection::Column),
        "rowReverse" | "row-reverse" => Some(FlexDirection::RowReverse),
        "columnReverse" | "column-reverse" => Some(FlexDirection::ColumnReverse),
        _ => None,
    }
}

fn parse_justify_content(s: &str) -> Option<JustifyContent> {
    match s {
        "flexStart" | "flex-start" => Some(JustifyContent::FlexStart),
        "flexEnd" | "flex-end" => Some(JustifyContent::FlexEnd),
        "center" => Some(JustifyContent::Center),
        "spaceBetween" | "space-between" => Some(JustifyContent::SpaceBetween),
        "spaceAround" | "space-around" => Some(JustifyContent::SpaceAround),
        "spaceEvenly" | "space-evenly" => Some(JustifyContent::SpaceEvenly),
        _ => None,
    }
}

fn parse_flex_wrap(s: &str) -> Option<FlexWrap> {
    match s {
        "nowrap" | "no-wrap" => Some(FlexWrap::NoWrap),
        "wrap" => Some(FlexWrap::Wrap),
        "wrapReverse" | "wrap-reverse" => Some(FlexWrap::WrapReverse),
        _ => None,
    }
}

fn parse_align_self(s: &str) -> Option<AlignSelf> {
    match s {
        "auto" => Some(AlignSelf::Auto),
        "flexStart" | "flex-start" => Some(AlignSelf::FlexStart),
        "flexEnd" | "flex-end" => Some(AlignSelf::FlexEnd),
        "center" => Some(AlignSelf::Center),
        "stretch" => Some(AlignSelf::Stretch),
        "baseline" => Some(AlignSelf::Baseline),
        _ => None,
    }
}

fn parse_align_content(s: &str) -> Option<AlignContent> {
    match s {
        "flexStart" | "flex-start" => Some(AlignContent::FlexStart),
        "flexEnd" | "flex-end" => Some(AlignContent::FlexEnd),
        "center" => Some(AlignContent::Center),
        "stretch" => Some(AlignContent::Stretch),
        "spaceBetween" | "space-between" => Some(AlignContent::SpaceBetween),
        "spaceAround" | "space-around" => Some(AlignContent::SpaceAround),
        _ => None,
    }
}

fn parse_position(s: &str) -> Option<PositionType> {
    match s {
        "relative" => Some(PositionType::Relative),
        "absolute" => Some(PositionType::Absolute),
        _ => None,
    }
}

fn parse_align_items(s: &str) -> Option<AlignItems> {
    match s {
        "flexStart" | "flex-start" => Some(AlignItems::FlexStart),
        "flexEnd" | "flex-end" => Some(AlignItems::FlexEnd),
        "center" => Some(AlignItems::Center),
        "stretch" => Some(AlignItems::Stretch),
        "baseline" => Some(AlignItems::Baseline),
        _ => None,
    }
}

fn style_get_string(table: &LuaTable, key: &str) -> Option<String> {
    let v: LuaValue = table.get(key).ok()?;
    if let Ok(s) = v.to_string() { Some(s.to_string()) } else { None }
}

fn parse_style(table: &LuaTable) -> LuaResult<StyleMap> {
    let mut style = StyleMap::new();

    if let Some(s) = style_get_string(table, "width") { style.width = parse_length(&s); }
    if let Some(s) = style_get_string(table, "height") { style.height = parse_length(&s); }
    if let Some(s) = style_get_string(table, "minWidth") { style.min_width = parse_length(&s); }
    if let Some(s) = style_get_string(table, "maxWidth") { style.max_width = parse_length(&s); }
    if let Some(s) = style_get_string(table, "minHeight") { style.min_height = parse_length(&s); }
    if let Some(s) = style_get_string(table, "maxHeight") { style.max_height = parse_length(&s); }
    if let Some(s) = style_get_string(table, "backgroundColor") { style.background_color = parse_hex_color(&s); }
    if let Some(s) = style_get_string(table, "margin") { style.margin = parse_rect_auto(&s); }
    if let Some(s) = style_get_string(table, "padding") { style.padding = parse_rect(&s); }
    if let Some(s) = style_get_string(table, "flexDirection") { style.flex_direction = parse_flex_direction(&s); }
    if let Some(s) = style_get_string(table, "justifyContent") { style.justify_content = parse_justify_content(&s); }
    if let Some(s) = style_get_string(table, "alignItems") { style.align_items = parse_align_items(&s); }
    if let Some(s) = style_get_string(table, "alignSelf") { style.align_self = parse_align_self(&s); }
    if let Some(s) = style_get_string(table, "alignContent") { style.align_content = parse_align_content(&s); }
    if let Some(s) = style_get_string(table, "flexWrap") { style.flex_wrap = parse_flex_wrap(&s); }
    if let Some(s) = style_get_string(table, "position") { style.position = parse_position(&s); }
    if let Some(s) = style_get_string(table, "top") { style.top = parse_length(&s); }
    if let Some(s) = style_get_string(table, "left") { style.left = parse_length(&s); }
    if let Some(s) = style_get_string(table, "right") { style.right = parse_length(&s); }
    if let Some(s) = style_get_string(table, "bottom") { style.bottom = parse_length(&s); }
    if let Some(s) = style_get_string(table, "gap") { if let Ok(n) = s.parse() { style.gap = Some(n); } }
    if let Some(s) = style_get_string(table, "flexGrow") { if let Ok(n) = s.parse() { style.flex_grow = Some(n); } }
    if let Some(s) = style_get_string(table, "flexShrink") { if let Ok(n) = s.parse() { style.flex_shrink = Some(n); } }
    if let Some(s) = style_get_string(table, "borderWidth") { if let Ok(n) = s.parse() { style.border_width = Some(n); } }
    if let Some(s) = style_get_string(table, "borderColor") { style.border_color = parse_hex_color(&s); }
    if let Some(s) = style_get_string(table, "color") { style.color = parse_hex_color(&s); }
    if let Some(s) = style_get_string(table, "fontSize") { if let Ok(n) = s.parse() { style.font_size = Some(n); } }
    if let Some(s) = style_get_string(table, "borderRadius") { if let Ok(n) = s.parse() { style.border_radius = Some(n); } }

    Ok(style)
}

pub fn table_to_ui_node(table: &LuaTable, lua: &Lua) -> LuaResult<UiNode> {
    let node_type: String = table.get("type").unwrap_or_default();
    let content: String = table.get("content").unwrap_or_default();

    let style_table: LuaTable = match table.get("style") {
        Ok(t) => t,
        Err(_) => lua.create_table()?,
    };
    let mut style = parse_style(&style_table)?;

    // Parse onClick as a string function name (e.g., "handleClick")
    if let Ok(on_click) = table.get::<LuaValue>("onClick") {
        match on_click {
            LuaValue::String(s) => {
                style.on_click_id = s.to_string_lossy().to_string();
            }
            LuaValue::Function(f) => {
                let name = format!("__axo_cb_{}", f.to_pointer() as u64);
                let globals = lua.globals();
                if let Ok(callbacks) = globals.get::<LuaTable>("_AXO_CALLBACKS") {
                    callbacks.set(name.as_str(), f)?;
                }
                style.on_click_id = name;
            }
            _ => {}
        }
    }

    // Parse hoverStyle, activeStyle, disabled from props
    if let Ok(hover_table) = table.get::<LuaTable>("hoverStyle") {
        style.hover_background_color = style_get_string(&hover_table, "backgroundColor")
            .and_then(|s| parse_hex_color(&s));
        style.hover_color = style_get_string(&hover_table, "color")
            .and_then(|s| parse_hex_color(&s));
    }
    if let Ok(active_table) = table.get::<LuaTable>("activeStyle") {
        style.active_background_color = style_get_string(&active_table, "backgroundColor")
            .and_then(|s| parse_hex_color(&s));
        style.active_color = style_get_string(&active_table, "color")
            .and_then(|s| parse_hex_color(&s));
    }
    if let Ok(disabled_val) = table.get::<LuaValue>("disabled") {
        style.disabled = match disabled_val {
            LuaValue::Boolean(b) => b,
            LuaValue::String(s) => s.to_string_lossy() == "true",
            _ => false,
        };
    }

    let on_change_text_id: String = match table.get::<LuaValue>("onChangeText") {
        Ok(LuaValue::String(s)) => s.to_string_lossy().to_string(),
        Ok(LuaValue::Function(f)) => {
            let name = format!("__axo_cb_{}", f.to_pointer() as u64);
            let globals = lua.globals();
            if let Ok(callbacks) = globals.get::<LuaTable>("_AXO_CALLBACKS") {
                let _ = callbacks.set(name.as_str(), f);
            }
            name
        }
        _ => String::new(),
    };

    let children_raw: LuaTable = match table.get("children") {
        Ok(t) => t,
        Err(_) => lua.create_table()?,
    };

    let mut children = Vec::new();
    for pair in children_raw.pairs::<LuaValue, LuaTable>() {
        let (_, child_table) = pair?;
        children.push(table_to_ui_node(&child_table, lua)?);
    }

    Ok(UiNode { id: 0, node_type, content, style, children, on_change_text_id })
}