plushie-core 0.4.0

Extension SDK for Plushie
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Debug-mode prop validation.
//!
//! When enabled, [`validate_props`] checks each node's props against a
//! schema of expected prop names and types per widget type. Unexpected
//! names or type mismatches are logged as warnings.
//!
//! Enabled unconditionally in debug builds. In release builds, the host
//! can opt in via `validate_props: true` in the Settings message.

use std::sync::OnceLock;

use crate::protocol::TreeNode;
use serde_json::Value;

/// Props accepted by all widget types. Checked before widget-specific
/// schemas so they don't appear as "unexpected" in validation warnings.
const UNIVERSAL_PROPS: &[&str] = &["a11y", "event_rate", "id"];

/// Global flag to enable prop validation in release builds.
/// Set via `set_validate_props(true)` during settings init.
/// In debug builds, validation always runs regardless of this flag.
static VALIDATE_PROPS: OnceLock<bool> = OnceLock::new();

/// Enable or disable prop validation at runtime. Called once during
/// settings initialization. Returns false if already set.
pub fn set_validate_props(enabled: bool) -> bool {
    VALIDATE_PROPS.set(enabled).is_ok()
}

/// Returns true if prop validation is enabled (debug build OR explicit opt-in).
pub fn is_validate_props_enabled() -> bool {
    cfg!(debug_assertions) || *VALIDATE_PROPS.get().unwrap_or(&false)
}

/// Prop type expectations for validation.
#[derive(Debug, Clone, Copy)]
enum PropType {
    Str,
    Number,
    Bool,
    Length,
    Color,
    Array,
    Any,
}

fn prop_type_matches(val: &Value, expected: PropType) -> bool {
    match expected {
        PropType::Str => val.is_string(),
        PropType::Number => val.is_number() || val.is_string(), // numeric strings accepted
        PropType::Bool => val.is_boolean(),
        PropType::Length => val.is_number() || val.is_string() || val.is_object(),
        PropType::Color => val.is_string(),
        PropType::Array => val.is_array(),
        PropType::Any => true,
    }
}

/// Collect prop validation warnings for a node without logging them.
///
/// Returns a list of human-readable warning strings. Useful for testing
/// and for callers that want to inspect warnings programmatically.
pub(crate) fn collect_prop_warnings(node: &TreeNode) -> Vec<String> {
    use PropType::*;

    let expected: &[(&str, PropType)] = match node.type_name.as_str() {
        "button" => &[
            ("label", Str),
            ("content", Str),
            ("style", Any),
            ("width", Length),
            ("height", Length),
            ("padding", Any),
            ("clip", Bool),
            ("disabled", Bool),
            ("enabled", Bool),
        ],
        "text" => &[
            ("content", Str),
            ("size", Number),
            ("color", Color),
            ("font", Any),
            ("width", Length),
            ("height", Length),
            ("align_x", Str),
            ("align_y", Str),
            ("line_height", Number),
            ("shaping", Str),
            ("wrapping", Str),
            ("ellipsis", Str),
            ("style", Str),
        ],
        "column" => &[
            ("spacing", Number),
            ("padding", Any),
            ("width", Length),
            ("height", Length),
            ("max_width", Number),
            ("align_x", Str),
            ("clip", Bool),
            ("wrap", Bool),
        ],
        "row" => &[
            ("spacing", Number),
            ("padding", Any),
            ("width", Length),
            ("height", Length),
            ("max_width", Number),
            ("align_y", Str),
            ("clip", Bool),
            ("wrap", Bool),
        ],
        "container" => &[
            ("padding", Any),
            ("width", Length),
            ("height", Length),
            ("max_width", Number),
            ("max_height", Number),
            ("center", Bool),
            ("align_x", Str),
            ("align_y", Str),
            ("clip", Bool),
            ("style", Any),
            ("background", Any),
            ("color", Color),
            ("border", Any),
            ("shadow", Any),
        ],
        "text_input" => &[
            ("value", Str),
            ("placeholder", Str),
            ("font", Any),
            ("width", Length),
            ("padding", Any),
            ("size", Number),
            ("line_height", Number),
            ("secure", Bool),
            ("style", Any),
            ("icon", Any),
            ("disabled", Bool),
            ("on_submit", Any),
            ("on_paste", Bool),
            ("align_x", Str),
            ("placeholder_color", Color),
            ("selection_color", Color),
            ("ime_purpose", Str),
        ],
        "slider" => &[
            ("value", Number),
            ("range", Array),
            ("step", Number),
            ("width", Length),
            ("height", Number),
            ("style", Any),
            ("shift_step", Number),
            ("default", Number),
            ("rail_color", Color),
            ("rail_width", Number),
            ("circular_handle", Bool),
            ("handle_radius", Number),
            ("label", Str),
        ],
        "checkbox" => &[
            ("label", Str),
            ("checked", Bool),
            ("size", Number),
            ("font", Any),
            ("text_size", Number),
            ("spacing", Number),
            ("width", Length),
            ("style", Any),
            ("icon", Any),
            ("disabled", Bool),
            ("line_height", Number),
            ("wrapping", Str),
            ("shaping", Str),
        ],
        "toggler" => &[
            ("label", Str),
            ("is_toggled", Bool),
            ("size", Number),
            ("font", Any),
            ("text_size", Number),
            ("spacing", Number),
            ("width", Length),
            ("style", Any),
            ("disabled", Bool),
            ("line_height", Number),
            ("wrapping", Str),
            ("shaping", Str),
        ],
        "progress_bar" => &[
            ("value", Number),
            ("range", Array),
            ("width", Length),
            ("height", Length),
            ("style", Any),
            ("vertical", Bool),
            ("label", Str),
        ],
        "image" => &[
            ("source", Any),
            ("width", Length),
            ("height", Length),
            ("content_fit", Str),
            ("filter_method", Str),
            ("rotation", Any),
            ("opacity", Number),
            ("border_radius", Number),
            ("expand", Bool),
            ("scale", Number),
            ("alt", Str),
            ("description", Str),
            ("decorative", Bool),
            ("crop", Any),
        ],
        "svg" => &[
            ("source", Str),
            ("width", Length),
            ("height", Length),
            ("content_fit", Str),
            ("rotation", Any),
            ("opacity", Number),
            ("color", Color),
            ("alt", Str),
            ("description", Str),
            ("decorative", Bool),
        ],
        "scrollable" => &[
            ("width", Length),
            ("height", Length),
            ("direction", Str),
            ("style", Any),
            ("anchor", Str),
            ("spacing", Number),
            ("scrollbar_width", Number),
            ("scrollbar_margin", Number),
            ("scroller_width", Number),
            ("scrollbar_color", Color),
            ("scroller_color", Color),
            ("auto_scroll", Bool),
            ("on_scroll", Bool),
        ],
        "grid" => &[
            ("columns", Number),
            ("spacing", Number),
            ("width", Number),
            ("height", Number),
            ("column_width", Length),
            ("row_height", Length),
            ("fluid", Number),
        ],
        "radio" => &[
            ("label", Str),
            ("value", Str),
            ("selected", Any),
            ("size", Number),
            ("font", Any),
            ("text_size", Number),
            ("spacing", Number),
            ("width", Length),
            ("style", Any),
            ("group", Str),
            ("line_height", Number),
            ("wrapping", Str),
            ("shaping", Str),
        ],
        "tooltip" => &[
            ("tip", Str),
            ("position", Str),
            ("gap", Number),
            ("padding", Number),
            ("snap_within_viewport", Bool),
            ("delay", Number),
            ("style", Any),
        ],
        "mouse_area" => &[
            ("on_middle_press", Bool),
            ("on_right_press", Bool),
            ("on_right_release", Bool),
            ("on_middle_release", Bool),
            ("on_double_click", Bool),
            ("on_enter", Bool),
            ("on_exit", Bool),
            ("on_move", Bool),
            ("on_scroll", Bool),
            ("cursor", Str),
        ],
        "sensor" => &[("delay", Number), ("anticipate", Number)],
        "space" => &[("width", Length), ("height", Length)],
        "rule" => &[
            ("direction", Str),
            ("width", Number),
            ("height", Number),
            ("thickness", Number),
            ("style", Any),
        ],
        "pick_list" => &[
            ("options", Array),
            ("selected", Str),
            ("placeholder", Str),
            ("width", Length),
            ("padding", Any),
            ("text_size", Number),
            ("font", Any),
            ("menu_height", Number),
            ("line_height", Number),
            ("shaping", Str),
            ("handle", Any),
            ("ellipsis", Str),
            ("menu_style", Any),
            ("style", Any),
            ("on_open", Bool),
            ("on_close", Bool),
        ],
        "combo_box" => &[
            ("selected", Str),
            ("placeholder", Str),
            ("options", Array),
            ("width", Length),
            ("padding", Any),
            ("size", Number),
            ("font", Any),
            ("line_height", Number),
            ("shaping", Str),
            ("menu_height", Number),
            ("icon", Any),
            ("on_option_hovered", Bool),
            ("on_open", Bool),
            ("on_close", Bool),
            ("ellipsis", Str),
            ("menu_style", Any),
            ("style", Any),
        ],
        "text_editor" => &[
            ("content", Str),
            ("placeholder", Str),
            ("height", Length),
            ("width", Number),
            ("size", Number),
            ("font", Any),
            ("line_height", Number),
            ("padding", Number),
            ("min_height", Number),
            ("max_height", Number),
            ("wrapping", Str),
            ("key_bindings", Array),
            ("style", Any),
            ("highlight_syntax", Str),
            ("highlight_theme", Str),
            ("placeholder_color", Color),
            ("selection_color", Color),
            ("ime_purpose", Str),
        ],
        "overlay" => &[
            ("position", Str),
            ("gap", Number),
            ("offset_x", Number),
            ("offset_y", Number),
            ("flip", Bool),
            ("align", Str),
        ],
        "themer" => &[("theme", Any)],
        "stack" => &[("width", Length), ("height", Length), ("clip", Bool)],
        "pin" => &[
            ("x", Number),
            ("y", Number),
            ("width", Length),
            ("height", Length),
        ],
        "keyed_column" => &[
            ("spacing", Number),
            ("padding", Any),
            ("width", Length),
            ("height", Length),
            ("max_width", Number),
        ],
        "float" => &[
            ("translate_x", Number),
            ("translate_y", Number),
            ("scale", Number),
        ],
        "responsive" => &[("width", Length), ("height", Length)],
        "rich_text" => &[
            ("spans", Array),
            ("size", Number),
            ("font", Any),
            ("color", Color),
            ("width", Length),
            ("height", Length),
            ("line_height", Number),
            ("wrapping", Str),
            ("ellipsis", Str),
        ],
        "vertical_slider" => &[
            ("value", Number),
            ("range", Array),
            ("step", Number),
            ("width", Number),
            ("height", Length),
            ("style", Any),
            ("shift_step", Number),
            ("default", Number),
            ("rail_color", Color),
            ("rail_width", Number),
            ("label", Str),
        ],
        "table" => &[
            ("columns", Array),
            ("rows", Array),
            ("width", Length),
            ("header", Bool),
            ("padding", Any),
            ("sort_by", Str),
            ("sort_order", Str),
            ("header_text_size", Number),
            ("row_text_size", Number),
            ("cell_spacing", Number),
            ("row_spacing", Number),
            ("separator_thickness", Number),
            ("separator_color", Color),
            ("separator", Bool),
        ],
        "pane_grid" => &[
            ("spacing", Number),
            ("width", Length),
            ("height", Length),
            ("min_size", Number),
            ("leeway", Number),
            ("divider_color", Color),
            ("divider_width", Number),
            ("split_axis", Str),
        ],
        "markdown" => &[
            ("content", Str),
            ("text_size", Number),
            ("h1_size", Number),
            ("h2_size", Number),
            ("h3_size", Number),
            ("code_size", Number),
            ("spacing", Number),
            ("width", Length),
            ("link_color", Color),
            ("code_theme", Str),
        ],
        "canvas" => &[
            ("layers", Any),
            ("shapes", Any),
            ("background", Color),
            ("width", Length),
            ("height", Length),
            ("interactive", Any),
            ("on_press", Bool),
            ("on_release", Bool),
            ("on_move", Bool),
            ("on_scroll", Bool),
            ("alt", Str),
            ("description", Str),
        ],
        "qr_code" => &[
            ("data", Str),
            ("cell_size", Number),
            ("error_correction", Str),
            ("cell_color", Color),
            ("background_color", Color),
            ("alt", Str),
            ("description", Str),
        ],
        "window" => &[
            ("padding", Any),
            ("width", Length),
            ("height", Length),
            ("scale_factor", Number),
        ],
        _ => return Vec::new(), // Unknown widget type -- skip validation
    };

    let props = match node.props.as_object() {
        Some(p) => p,
        None => return Vec::new(),
    };

    let expected_names: Vec<&str> = expected.iter().map(|(name, _)| *name).collect();
    let mut warnings = Vec::new();

    for (key, val) in props {
        // Skip props accepted by all widget types.
        if UNIVERSAL_PROPS.contains(&key.as_str()) {
            continue;
        }
        match expected.iter().find(|(name, _)| name == key) {
            Some((_, expected_type)) => {
                if !prop_type_matches(val, *expected_type) {
                    warnings.push(format!(
                        "widget '{}' ({}): prop '{}' has unexpected type {:?} (expected {:?})",
                        node.id, node.type_name, key, val, expected_type
                    ));
                }
            }
            None => {
                warnings.push(format!(
                    "widget '{}' ({}): unexpected prop '{}' (known: {:?})",
                    node.id, node.type_name, key, expected_names
                ));
            }
        }
    }

    warnings
}

/// Validate props for known widget types. Only active in debug builds.
/// Logs warnings for unexpected prop names or mismatched types.
pub(crate) fn validate_props(node: &TreeNode) {
    for warning in collect_prop_warnings(node) {
        log::warn!("{warning}");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn make_node(type_name: &str, props: serde_json::Value) -> TreeNode {
        crate::testing::node_with_props(&format!("test-{type_name}"), type_name, props)
    }

    /// Verify validate_props doesn't panic for every supported widget type,
    /// including with an empty props object and with representative props.
    #[test]
    fn validate_all_supported_types_no_panic() {
        let types_with_sample_props: Vec<(&str, serde_json::Value)> = vec![
            ("button", json!({"label": "ok", "disabled": false})),
            ("text", json!({"content": "hello", "size": 14})),
            ("column", json!({"spacing": 8})),
            ("row", json!({"spacing": 4, "wrap": true})),
            (
                "container",
                json!({"padding": 10, "width": "fill", "clip": false}),
            ),
            ("text_input", json!({"value": "", "placeholder": "type..."})),
            ("slider", json!({"value": 50, "range": [0, 100]})),
            ("checkbox", json!({"label": "agree", "checked": true})),
            (
                "toggler",
                json!({"label": "dark mode", "is_toggled": false}),
            ),
            ("progress_bar", json!({"value": 75, "range": [0, 100]})),
            ("image", json!({"source": "test.png"})),
            ("svg", json!({"source": "icon.svg"})),
            ("scrollable", json!({"direction": "vertical"})),
            ("grid", json!({"columns": 3, "spacing": 4})),
            (
                "radio",
                json!({"label": "opt", "value": "a", "group": "g1"}),
            ),
            ("tooltip", json!({"tip": "help", "position": "top"})),
            (
                "mouse_area",
                json!({"on_enter": true, "on_exit": true, "cursor": "pointer"}),
            ),
            ("sensor", json!({"delay": 100})),
            ("space", json!({"width": 10, "height": 10})),
            ("rule", json!({"direction": "horizontal", "thickness": 2})),
            ("pick_list", json!({"options": ["a", "b"], "selected": "a"})),
            (
                "combo_box",
                json!({"placeholder": "search...", "width": "fill"}),
            ),
            (
                "text_editor",
                json!({"placeholder": "code here", "height": 200}),
            ),
            ("overlay", json!({"position": "below", "gap": 4})),
            ("themer", json!({"theme": {"background": "#000"}})),
            ("stack", json!({"width": "fill", "clip": false})),
            ("pin", json!({"x": 10, "y": 20})),
            ("keyed_column", json!({"spacing": 8, "max_width": 400})),
            ("float", json!({"translate_x": 5, "translate_y": 10})),
            ("responsive", json!({"width": "fill", "height": "fill"})),
            ("rich_text", json!({"spans": [{"text": "hi"}], "size": 16})),
            (
                "vertical_slider",
                json!({"value": 50, "range": [0, 100], "height": "fill"}),
            ),
            (
                "table",
                json!({"columns": [{"key": "name", "label": "Name"}], "rows": []}),
            ),
            ("pane_grid", json!({"spacing": 2})),
            ("markdown", json!({"content": "# Hello", "text_size": 16})),
            (
                "canvas",
                json!({"width": "fill", "height": 200, "interactive": true}),
            ),
            ("qr_code", json!({"data": "hello", "cell_size": 4})),
            ("window", json!({"padding": 8})),
        ];

        for (type_name, props) in &types_with_sample_props {
            let node = make_node(type_name, props.clone());
            validate_props(&node); // must not panic

            // Also test with empty props
            let empty_node = make_node(type_name, json!({}));
            validate_props(&empty_node);
        }
    }

    /// Unknown widget types are silently skipped (no panic).
    #[test]
    fn unknown_type_skipped() {
        let node = make_node("antimatter_widget", json!({"flux": 42}));
        validate_props(&node);
    }

    /// Null props are handled gracefully.
    #[test]
    fn null_props_no_panic() {
        let node = make_node("button", json!(null));
        validate_props(&node);
    }

    /// prop_type_matches covers all variants correctly.
    #[test]
    fn prop_type_matching() {
        use PropType::*;

        assert!(prop_type_matches(&json!("hello"), Str));
        assert!(!prop_type_matches(&json!(42), Str));

        assert!(prop_type_matches(&json!(42), Number));
        assert!(prop_type_matches(&json!("42"), Number)); // numeric strings OK
        assert!(!prop_type_matches(&json!(true), Number));

        assert!(prop_type_matches(&json!(true), Bool));
        assert!(!prop_type_matches(&json!("true"), Bool));

        assert!(prop_type_matches(&json!(100), Length));
        assert!(prop_type_matches(&json!("fill"), Length));
        assert!(prop_type_matches(&json!({"portion": 2}), Length));
        assert!(!prop_type_matches(&json!(true), Length));

        assert!(prop_type_matches(&json!("#ff0000"), Color));
        assert!(!prop_type_matches(&json!(42), Color));

        assert!(prop_type_matches(&json!([1, 2, 3]), Array));
        assert!(!prop_type_matches(&json!("nope"), Array));

        // Any matches everything
        assert!(prop_type_matches(&json!(null), Any));
        assert!(prop_type_matches(&json!(42), Any));
        assert!(prop_type_matches(&json!("x"), Any));
    }

    // -- collect_prop_warnings --

    #[test]
    fn warnings_for_unexpected_prop_name() {
        let node = make_node("button", json!({"label": "ok", "bogus_prop": 42}));
        let warnings = collect_prop_warnings(&node);
        assert_eq!(warnings.len(), 1);
        assert!(
            warnings[0].contains("unexpected prop 'bogus_prop'"),
            "warning should name the bad prop, got: {}",
            warnings[0]
        );
    }

    #[test]
    fn warnings_for_type_mismatch() {
        // "label" expects Str, passing a number should trigger a type warning.
        let node = make_node("button", json!({"label": 42}));
        let warnings = collect_prop_warnings(&node);
        assert_eq!(warnings.len(), 1);
        assert!(
            warnings[0].contains("unexpected type"),
            "warning should mention type mismatch, got: {}",
            warnings[0]
        );
    }

    #[test]
    fn no_warnings_for_valid_props() {
        let node = make_node("button", json!({"label": "ok", "disabled": false}));
        let warnings = collect_prop_warnings(&node);
        assert!(
            warnings.is_empty(),
            "expected no warnings, got: {warnings:?}"
        );
    }

    #[test]
    fn no_warnings_for_universal_props() {
        // "a11y", "id", and "event_rate" are universal props, should never trigger warnings.
        let node = make_node(
            "button",
            json!({"a11y": {"role": "button"}, "id": "btn1", "event_rate": 30}),
        );
        let warnings = collect_prop_warnings(&node);
        assert!(
            warnings.is_empty(),
            "universal props should not warn, got: {warnings:?}"
        );
    }

    #[test]
    fn no_warnings_for_unknown_widget_type() {
        let node = make_node("antimatter_widget", json!({"flux": 42}));
        let warnings = collect_prop_warnings(&node);
        assert!(
            warnings.is_empty(),
            "unknown types should produce no warnings"
        );
    }

    #[test]
    fn multiple_warnings_for_multiple_bad_props() {
        // "content" expects Str but gets bool -> type mismatch
        // "bogus" is not a known prop -> unexpected prop
        let node = make_node("text", json!({"content": true, "bogus": 1}));
        let warnings = collect_prop_warnings(&node);
        assert_eq!(warnings.len(), 2, "expected 2 warnings, got: {warnings:?}");
    }
}