Skip to main content

Module codec

Module codec 

Source
Expand description

The unified, reversible surface codec.

VIEW_4 collapses the separate View (encode: Value -> Scene) and Editor (decode: (Value, Intent) -> Draft, then Draft -> Operation) halves into ONE contract, SurfaceCodec, so a lens renders and parses from one object and the two directions cannot drift. Rendering runs the codec forward (capability-aware projection); editing runs it backward.

The headline invariant is a CHECKABLE roundtrip property: decoding a REAL edit-field that sets the value to a distinct sentinel yields a committable Draft that actually proposes that sentinel, and the draft commits. A lossy editor – one that drops the edit and re-proposes the base – fails the property. roundtrip_holds verifies it for any codec + value; noop_roundtrip_holds keeps the weaker no-op check (a cancel proposes no change).

Projection (SurfaceCodec::encode) is deterministic for a given (value, caps): reduce_for_caps fits the rendered Scene to the surface’s display density (glance/compact/regular/dense) by a fixed strategy, so the same inputs always yield the same Scene – the basis for replay and tests.

§Example

use sim_kernel::{Cx, DefaultFactory, EagerPolicy, Expr};
use sim_lib_view::{codec::{PairCodec, SurfaceCodec, roundtrip_holds}, surface, UniversalView, UniversalEditor};
use std::sync::Arc;

let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
let codec = PairCodec::new(Arc::new(UniversalView), Arc::new(UniversalEditor::writable()));
let value = Expr::String("hello".to_owned());
// A real edit applied through the codec is faithfully reproduced -- the
// reversibility property (a lossy editor would make this false).
assert!(roundtrip_holds(&mut cx, &codec, &value).unwrap());
// Projection to a glance surface still yields a valid Scene.
let watch = surface::preset("watch").unwrap();
let scene = codec.encode(&mut cx, &value, &watch).unwrap();
assert!(sim_lib_scene::validate_scene(&scene).is_ok());

Structs§

PairCodec
Adapts a (View, Editor) pair to the unified SurfaceCodec.

Constants§

UNIVERSAL_SURFACE_CODEC_ID
The id under which the universal default surface codec is registered.

Traits§

SurfaceCodec
One reversible surface codec: encode a value to a projected Scene, decode an Intent to a Draft, and commit a Draft to a checked Operation.

Functions§

noop_intent
Builds the canonical no-op Intent (intent/cancel): it proposes no change.
noop_roundtrip_holds
The weaker no-op reversibility check: decoding a noop_intent proposes no change. True for any editor that honors cancel, so it cannot stand in for roundtrip_holds – keep both.
reduce_for_caps
Deterministically fits a Scene to a surface’s display density.
roundtrip_holds
Verifies the reversibility property for codec and value with a REAL edit.
universal_surface_codec_symbol
Symbol form of UNIVERSAL_SURFACE_CODEC_ID.