bevy-react 0.4.0

Drive bevy_ui from a React app over an embedded V8 runtime.
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
//! Decode tests for the shape wire types. Warn assertions use the devtools
//! decode sink, which is thread-local — draining it per-test is
//! parallel-safe (the `filters::wire` test precedent).

use bevy::color::Srgba;
use bevy::math::Vec2;

use super::{
    FillRuleKind, LinecapKind, LinejoinKind, ShapeAttrs, ShapePaint, ShapeTransform, ViewBox,
};
use crate::protocol::animatable::AnimatableField;

fn attrs(json: serde_json::Value) -> ShapeAttrs {
    serde_json::from_value(json).expect("shape attrs decode never fails the batch")
}

#[cfg(all(feature = "devtools", debug_assertions))]
fn drain_warn_kinds() -> Vec<&'static str> {
    crate::diag::take_decode_warnings()
        .into_iter()
        .map(|w| w.kind)
        .collect()
}

/// Typical attr sets of each shape decode into the expected fields, leaving
/// the rest `None`.
#[test]
fn per_shape_round_trips() {
    let circle = attrs(serde_json::json!({ "cx": 50, "cy": 40.5, "r": 10, "fill": "#f00" }));
    assert_eq!(
        (
            circle.cx.static_val(),
            circle.cy.static_val(),
            circle.r.static_val()
        ),
        (Some(50.0), Some(40.5), Some(10.0))
    );
    assert_eq!(circle.fill, Some(ShapePaint::Color(Srgba::RED)));
    assert_eq!(circle.stroke, None, "absent attrs stay None");

    let rect = attrs(serde_json::json!({ "x": 1, "y": 2, "width": 30, "height": 40, "rx": 4 }));
    assert_eq!(
        (
            rect.x.static_val(),
            rect.y.static_val(),
            rect.width.static_val(),
            rect.height.static_val(),
            rect.rx.static_val(),
            rect.ry.static_val(),
        ),
        (
            Some(1.0),
            Some(2.0),
            Some(30.0),
            Some(40.0),
            Some(4.0),
            None
        )
    );

    let line = attrs(serde_json::json!({
        "x1": 0, "y1": 0, "x2": 10, "y2": 5,
        "stroke": "#00ff00", "strokeWidth": 2.5,
    }));
    assert_eq!(
        (
            line.x1.static_val(),
            line.y1.static_val(),
            line.x2.static_val(),
            line.y2.static_val()
        ),
        (Some(0.0), Some(0.0), Some(10.0), Some(5.0))
    );
    assert_eq!(line.stroke, Some(ShapePaint::Color(Srgba::GREEN)));
    assert_eq!(
        line.stroke_width.static_val(),
        Some(2.5),
        "camelCase wire name decodes"
    );

    let polygon = attrs(serde_json::json!({ "points": [0, 0, 10, 5, 20, 0] }));
    assert_eq!(
        polygon.points.as_deref(),
        Some(
            &[
                Vec2::new(0.0, 0.0),
                Vec2::new(10.0, 5.0),
                Vec2::new(20.0, 0.0)
            ][..]
        )
    );

    let path = attrs(serde_json::json!({ "d": "M0 0 L10 10", "fillRule": "evenodd" }));
    assert_eq!(path.d.as_ref().map(|p| p.0.len()), Some(2));
    assert_eq!(path.fill_rule, Some(FillRuleKind::EvenOdd));
}

/// An odd-length `points` array warns and drops the field; the rest of the
/// object still decodes.
#[test]
fn odd_points_warn_and_drop() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({ "points": [0, 0, 10], "cx": 5 }));
    assert_eq!(a.points, None);
    assert_eq!(
        a.cx.static_val(),
        Some(5.0),
        "sibling fields survive the drop"
    );
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["shapePoints"]);
}

/// Paints: a CSS color parses, `"none"` is the explicit don't-paint keyword
/// (distinct from absent), garbage warns and drops.
#[test]
fn paints_decode_none_keyword_and_drop_garbage() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({ "fill": "none", "stroke": "#f00" }));
    assert_eq!(a.fill, Some(ShapePaint::None));
    assert_eq!(a.stroke, Some(ShapePaint::Color(Srgba::RED)));
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(
        drain_warn_kinds(),
        Vec::<&str>::new(),
        "clean decode is silent"
    );

    let bad = attrs(serde_json::json!({ "fill": "notacolor" }));
    assert_eq!(bad.fill, None);
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["shapePaint"]);
}

/// Keyword enums decode; an unrecognized keyword warns (`shapeEnum`) and
/// drops the field.
#[test]
fn keyword_enums_decode_and_drop_garbage() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({
        "fillRule": "nonzero",
        "strokeLinecap": "square",
        "strokeLinejoin": "bevel",
    }));
    assert_eq!(a.fill_rule, Some(FillRuleKind::NonZero));
    assert_eq!(a.stroke_linecap, Some(LinecapKind::Square));
    assert_eq!(a.stroke_linejoin, Some(LinejoinKind::Bevel));

    let bad = attrs(serde_json::json!({ "strokeLinecap": "banana" }));
    assert_eq!(bad.stroke_linecap, None);
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["shapeEnum"]);
}

/// A garbage `d` warns (`shapePath`) and drops the whole path — never a
/// partial segment list.
#[test]
fn garbage_path_warns_and_drops() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({ "d": "M10 10 L nope" }));
    assert_eq!(a.d, None);
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["shapePath"]);
}

/// Arcs are unsupported in v1: the whole path drops with the `shapePath`
/// warning naming the limitation.
#[test]
fn arc_path_warns_and_drops() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({ "d": "M0 0 A5 5 0 0 1 10 10" }));
    assert_eq!(a.d, None);
    #[cfg(all(feature = "devtools", debug_assertions))]
    {
        let warns = crate::diag::take_decode_warnings();
        assert_eq!(warns.len(), 1);
        assert_eq!(warns[0].kind, "shapePath");
        assert!(
            warns[0].message.contains("arc segments unsupported"),
            "{}",
            warns[0].message
        );
    }
}

/// A numeric attr accepts the inline `{ animated: …, seed? }` wrapper (the
/// style-field wire shape, seed sibling to `animated` inside the wrapper):
/// the field decodes Animated carrying the binding, the optional seed is
/// readable as the static value in the wrapper's place (`static_or_seed`),
/// and a seed-less wrapper reads as absent until the driver writes.
#[test]
fn animated_wrapper_decodes_on_numeric_attrs() {
    use crate::animations::protocol::Binding;
    let a = attrs(serde_json::json!({
        "r": { "animated": { "id": 7 }, "seed": 4 },
        "cx": { "animated": { "id": 3 } },
        "cy": 5,
    }));
    assert_eq!(a.r.binding(), Some(&Binding::Shared { id: 7 }));
    assert_eq!(
        a.r.static_val(),
        None,
        "an animated attr has no static value"
    );
    assert_eq!(
        a.r.static_or_seed(),
        Some(4.0),
        "the seed reads in its place"
    );
    assert_eq!(a.cx.binding(), Some(&Binding::Shared { id: 3 }));
    assert_eq!(
        a.cx.static_or_seed(),
        None,
        "seed-less wrapper reads as absent (attr default applies)"
    );
    assert_eq!(a.cy.static_val(), Some(5.0));
    assert_eq!(a.cy.static_or_seed(), Some(5.0));
}

/// A malformed `seed` (non-numeric on an `f32` attr) warns (`styleBinding`)
/// and drops to `None` — the binding itself is KEPT (the driver still runs;
/// only the static stand-in is lost).
#[test]
fn malformed_seed_warns_and_keeps_binding() {
    use crate::animations::protocol::Binding;
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({
        "r": { "animated": { "id": 7 }, "seed": "garbage" },
    }));
    assert_eq!(a.r.binding(), Some(&Binding::Shared { id: 7 }));
    assert_eq!(
        a.r.static_or_seed(),
        None,
        "bad seed drops, reads as absent"
    );
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["styleBinding"]);
}

/// `viewBox` is not animatable: an object value (an `{ animated }` wrapper or
/// any other) warns (`viewBox`) and drops the field instead of hard-erroring
/// the containing struct (= aborting the op batch). Same never-fail rule as
/// the shape-field visitors.
#[test]
fn view_box_object_warns_and_drops() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    #[derive(serde::Deserialize, Default)]
    #[serde(default)]
    struct Holder {
        #[serde(deserialize_with = "super::de_view_box")]
        vb: Option<ViewBox>,
        w: f32,
    }
    let h: Holder = serde_json::from_value(serde_json::json!({
        "vb": { "animated": { "id": 7 } },
        "w": 7.0,
    }))
    .expect("an object viewBox must not abort the containing struct");
    assert_eq!(h.vb, None);
    assert_eq!(h.w, 7.0, "sibling fields survive the drop");
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["viewBox"]);
}

/// Wire compat pin: a PLAIN number decodes exactly as before the wrapper
/// support — `Animatable::Static`, visible through `static_val` — with no JS
/// change needed.
#[test]
fn plain_number_still_decodes_static() {
    let a = attrs(serde_json::json!({ "r": 4 }));
    assert_eq!(
        a.r,
        Some(crate::protocol::animatable::Animatable::Static(4.0))
    );
    assert_eq!(a.r.static_val(), Some(4.0));
    // PartialEq holds across the wrapper type (the compare-before-write
    // discipline in `update_shape_attrs` relies on it).
    let b = attrs(serde_json::json!({ "r": 4.0 }));
    assert_eq!(a, b);
    let c = attrs(serde_json::json!({ "r": { "animated": { "id": 7 } } }));
    assert_ne!(a, c);
}

/// A wrapper on a NON-numeric field is not animatable: it must warn (the
/// field's own kind) and drop the field — never silently accept, never fail
/// the batch.
#[test]
fn animated_wrapper_on_non_numeric_warns_and_drops() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let wrapper = serde_json::json!({ "animated": { "id": 7 } });
    let a = attrs(serde_json::json!({
        "fill": wrapper, "d": wrapper, "points": wrapper,
        "strokeLinecap": wrapper, "transform": wrapper,
    }));
    assert_eq!(a.fill, None);
    assert_eq!(a.d, None);
    assert_eq!(a.points, None);
    assert_eq!(a.stroke_linecap, None);
    assert_eq!(a.transform, None);
    #[cfg(all(feature = "devtools", debug_assertions))]
    {
        let mut kinds = drain_warn_kinds();
        kinds.sort_unstable();
        assert_eq!(
            kinds,
            vec![
                "shapeEnum",
                "shapePaint",
                "shapePath",
                "shapePoints",
                "shapeTransform"
            ]
        );
    }
}

fn assert_mat_approx(got: ShapeTransform, want: [f32; 6]) {
    for (i, (g, w)) in got.0.iter().zip(want).enumerate() {
        assert!(
            (g - w).abs() < 1e-5,
            "matrix[{i}]: got {g}, want {w} ({got:?})"
        );
    }
}

/// `translate(5 10) rotate(90)` composes in list order: T·R.
#[test]
fn transform_composes_in_list_order() {
    let a = attrs(serde_json::json!({ "transform": "translate(5 10) rotate(90)" }));
    assert_mat_approx(a.transform.unwrap(), [0.0, 1.0, -1.0, 0.0, 5.0, 10.0]);
    // rotate about a point (svgtypes splits it into T·R·T): rotating (2, 0)
    // by 90° about (1, 0) lands on (1, 1) — spot-check via the matrix.
    let a = attrs(serde_json::json!({ "transform": "rotate(90 1 0)" }));
    let m = a.transform.unwrap().0;
    let (x, y) = (2.0f32, 0.0f32);
    let px = m[0] * x + m[2] * y + m[4];
    let py = m[1] * x + m[3] * y + m[5];
    assert!(
        (px - 1.0).abs() < 1e-5 && (py - 1.0).abs() < 1e-5,
        "({px}, {py})"
    );
}

/// `scale(s)` uses the one-arg uniform form; identity decode for an empty
/// list.
#[test]
fn transform_scale_and_empty() {
    let a = attrs(serde_json::json!({ "transform": "scale(2)" }));
    assert_mat_approx(a.transform.unwrap(), [2.0, 0.0, 0.0, 2.0, 0.0, 0.0]);
    let a = attrs(serde_json::json!({ "transform": "" }));
    assert_eq!(a.transform, Some(ShapeTransform::default()));
}

/// Unsupported transform functions (`skewX`, `matrix`) warn
/// (`shapeTransform`) and drop the field whole.
#[test]
fn unsupported_transform_warns_and_drops() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({ "transform": "skewX(3)" }));
    assert_eq!(a.transform, None);
    let b = attrs(serde_json::json!({ "transform": "matrix(1 0 0 1 0 0)" }));
    assert_eq!(b.transform, None);
    let c = attrs(serde_json::json!({ "transform": "translate(nope)" }));
    assert_eq!(c.transform, None);
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(
        drain_warn_kinds(),
        vec!["shapeTransform", "shapeTransform", "shapeTransform"]
    );
}

/// `ViewBox` parses both whitespace- and comma-separated forms; a
/// non-positive size or garbage warns (`viewBox`) and drops. (Wire-name and
/// merge behavior of the `viewBox` *prop* are covered in
/// `crate::protocol::merge::tests`.)
#[test]
fn view_box_parses_and_validates() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let expected = ViewBox {
        min: Vec2::ZERO,
        size: Vec2::new(100.0, 50.0),
    };
    assert_eq!(ViewBox::parse("0 0 100 50").unwrap(), expected);
    assert_eq!(ViewBox::parse("0,0,100,50").unwrap(), expected);
    assert!(
        ViewBox::parse("0 0 -1 5").is_err(),
        "negative size rejected"
    );
    assert!(
        ViewBox::parse("0 0 100").is_err(),
        "too few numbers rejected"
    );

    // Through the field deserializer: warn + drop, not a decode failure.
    #[derive(serde::Deserialize, Default)]
    #[serde(default)]
    struct Holder {
        #[serde(deserialize_with = "super::de_view_box")]
        vb: Option<ViewBox>,
        w: f32,
    }
    let h: Holder = serde_json::from_value(serde_json::json!({ "vb": "0 0 -1 5", "w": 7.0 }))
        .expect("bad viewBox must not abort the containing struct");
    assert_eq!(h.vb, None);
    assert_eq!(h.w, 7.0);
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["viewBox"]);
}

/// `transition` rides the shape object: a per-attr map of timing specs keyed
/// by the numeric attr wire names. Present → `Some` spec with the named
/// entries decoded (`ChannelTransition`, the style-transition timing type);
/// absent → `None`.
#[test]
fn shape_transition_decodes_per_attr_specs() {
    let a = attrs(serde_json::json!({
        "cx": 30.0,
        "transition": {
            "cx": { "duration": 200 },
            "strokeWidth": { "stiffness": 120.0 },
        },
    }));
    let spec = a.transition.as_ref().expect("transition decodes");
    let cx = spec.for_attr("cx").expect("cx entry present");
    assert_eq!(
        cx.duration.map(crate::protocol::units::Time::seconds),
        Some(0.2)
    );
    assert!(
        spec.for_attr("strokeWidth")
            .is_some_and(|s| s.stiffness == Some(120.0)),
        "wire names key the map (strokeWidth, not stroke_width)"
    );
    assert!(spec.for_attr("r").is_none(), "unlisted attrs have no entry");

    // Absent → None; spec participates in PartialEq (a spec-only change is a
    // real attrs change — atomic replace + repaint).
    let b = attrs(serde_json::json!({ "cx": 30.0 }));
    assert_eq!(b.transition, None);
    assert_ne!(a, b);
}

/// Unknown or non-numeric keys (`fill` isn't easeable) and malformed spec
/// values warn (`shapeTransition`) and drop THAT KEY — the rest of the spec
/// survives (per-key drop, not whole-spec).
#[test]
fn shape_transition_unknown_key_warns_and_drops_that_key() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({
        "transition": {
            "cx": { "duration": 100 },
            "fill": { "duration": 100 },
            "r": "garbage",
        },
    }));
    let spec = a.transition.as_ref().expect("valid keys survive");
    assert!(spec.for_attr("cx").is_some());
    assert!(spec.for_attr("fill").is_none(), "non-numeric key dropped");
    assert!(spec.for_attr("r").is_none(), "malformed spec value dropped");
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(
        drain_warn_kinds(),
        vec!["shapeTransition", "shapeTransition"]
    );
}

/// A non-object `transition` value warns and drops the whole field — never
/// fails the batch (the module's never-fail rule).
#[test]
fn shape_transition_non_object_warns_and_drops() {
    #[cfg(all(feature = "devtools", debug_assertions))]
    let _ = crate::diag::take_decode_warnings();
    let a = attrs(serde_json::json!({ "cx": 5.0, "transition": "fast" }));
    assert_eq!(a.transition, None);
    assert_eq!(a.cx.static_val(), Some(5.0), "sibling attrs survive");
    #[cfg(all(feature = "devtools", debug_assertions))]
    assert_eq!(drain_warn_kinds(), vec!["shapeTransition"]);
}