Skip to main content

relate_engine_api/redo/
wal_format_json.rs

1//! The WAL's on-disk JSON codec for redo property values (ADR-0078).
2//!
3//! The tagged `{"t":..., "v":...}` encoding itself is canonical on
4//! [`Value`] in `relate-core` (`Value: Serialize`/`Deserialize`), shared
5//! with the Parquet checkpoint codec. This module is the redo-side glue: the
6//! `props_json` / `props_from_json` helpers for the one cold path that still
7//! carries a raw `serde_json::Value` (`CatalogIndexDetail.options`). The
8//! Parquet checkpoint loader decodes its properties cells through
9//! `props_from_json` too, so both refuse a non-object (ADR-0183).
10//!
11//! A future binary WAL format replaces this module; `redo` itself is
12//! format-agnostic.
13
14use std::collections::BTreeMap;
15
16use relate_core::Value;
17
18/// Tagged-JSON object form of a property map. Retained for the cold
19/// `CatalogIndexDetail.options` field (a raw `serde_json::Value`); the hot
20/// create/set paths serialize typed values directly via `Value: Serialize`.
21pub fn props_json(map: &BTreeMap<String, Value>) -> serde_json::Value {
22    let obj: serde_json::Map<String, serde_json::Value> = map
23        .iter()
24        .map(|(k, v)| (k.clone(), v.to_json_tagged()))
25        .collect();
26    serde_json::Value::Object(obj)
27}
28
29/// Decode a tagged-JSON object into a typed property map. Strict: a non-object
30/// payload is an error, never a silently dropped value.
31pub fn props_from_json(v: &serde_json::Value) -> relate_core::Result<BTreeMap<String, Value>> {
32    let serde_json::Value::Object(map) = v else {
33        return Err(relate_core::Error::storage(
34            "property JSON must be an object",
35        ));
36    };
37    let mut out = BTreeMap::new();
38    for (k, val) in map {
39        out.insert(k.clone(), Value::from_json_tagged(val)?);
40    }
41    Ok(out)
42}
43
44/// Tagged-JSON form of a single value. Production serialization uses
45/// `Value: Serialize` directly; this remains for tests and for the
46/// legacy-shape DTOs in the byte-compat golden test.
47#[cfg(test)]
48pub fn value_json(v: &Value) -> serde_json::Value {
49    v.to_json_tagged()
50}