Skip to main content

bevy_react/
protocol.rs

1//! The wire protocol shared between the JS reconciler and the Bevy side.
2//!
3//! Everything here derives `serde` so deno_core's `serde_v8` can convert
4//! directly between the plain JS objects the reconciler builds and these Rust
5//! types — no JSON strings on the hot path. Ops only ever flow JS -> Rust, so
6//! they need `Deserialize` only; `UiEvent` flows Rust -> JS and is `Serialize`.
7//!
8//! Wire strings are decoded **once, here at the serde boundary** — never
9//! re-parsed on apply. The unit-bearing types (`Length`/`Angle`/`Time`/
10//! `FontSize`) parse into their own wire types, and the enum-like style fields
11//! (`display`/`align*`/`flex*`/grid tracks/…) decode directly into the
12//! `bevy_ui`/`bevy_text` values they drive, via field-level `deserialize_with`
13//! (which sidesteps the orphan rule), so applying a style in [`crate::ui_map`]
14//! is a plain field copy. A malformed string must **not** fail the whole batch
15//! (one typo would abort the entire commit and trigger a reload), so every
16//! deserializer falls back to the bevy default and emits a
17//! `tracing::warn!` naming the bad value (`tracing` reaches the same log sink
18//! `bevy_log` drains). In dev builds with devtools those fallbacks are also
19//! collected as structured `crate::diag` entries (`decode_warn` +
20//! [`op::OpBatch`]'s per-op attribution) so the inspector can flag the offending
21//! style/prop rows.
22
23pub mod animatable;
24pub mod background_image;
25pub mod grid;
26pub mod keywords;
27mod merge;
28pub mod op;
29pub mod outbound;
30pub mod props;
31pub mod style;
32pub mod transform;
33pub mod units;
34pub mod visual;
35
36/// Stable identity for a node, assigned by the JS reconciler. `0` is reserved
37/// for the root container (the Bevy UI root entity).
38pub type NodeId = u32;
39
40pub const ROOT_ID: NodeId = 0;
41
42/// Emit a decode-fallback warning: the log line every malformed wire value
43/// already produced, plus (in dev builds with devtools) a structured
44/// [`crate::diag`] entry so the inspector can flag the offending row. `kind`
45/// names the value's domain (`"length"`, `"rect"`, a keyword field's kind, …);
46/// `value` is the raw offending wire string.
47pub(crate) fn decode_warn(kind: &'static str, value: &str, message: &str) {
48    tracing::warn!(target: "bevy_react", "{message}");
49    crate::diag::decode_report(kind, value, message);
50}