Skip to main content

engenho_sui_typescape/
lib.rs

1//! # engenho-sui-typescape
2//!
3//! The **bridge** between sui's untyped Nix value tree and engenho's
4//! typed substrate primitives.
5//!
6//! ## Why this crate exists
7//!
8//! sui (pleme-io's pure-Rust Nix replacement, bytecode VM, 3× CppNix)
9//! evaluates Nix expressions into a `sui_eval::Value` tree with shape
10//! `{ Null | Bool | Int | Float | String | Path | List | Attrs | Lambda
11//! | Builtin | Thunk }`. The whole shape is single-threaded — every
12//! `Rc<...>` inside makes `sui_eval::Value` `!Send`.
13//!
14//! engenho's typescape — the catalog of typed Rust primitives registered
15//! via `#[derive(TataraDomain)]` — is `Send + Sync` because every
16//! primitive flows across:
17//!
18//!   - engenho-revoada's Raft log (cross-thread, cross-node)
19//!   - tameshi+sekiban admission attestation (background workers)
20//!   - mirante's `ObservationChannel` (broadcast to subscribers)
21//!   - the Viggy controller's reconcile loop (tokio multi-thread runtime)
22//!
23//! The two worlds cannot meet directly. This crate is the bridge.
24//!
25//! ## The TypescapeValue mirror
26//!
27//! [`TypescapeValue`] is a Send+Sync, thread-portable mirror of the
28//! sui `Value` shape:
29//!
30//!   - Lambda / Builtin / Thunk variants ARE INTENTIONALLY ABSENT —
31//!     thunks must be forced before crossing the bridge; lambdas + builtins
32//!     are eval-time machinery that do not survive serialization
33//!   - `Rc<...>` becomes `Arc<...>` everywhere
34//!   - The bridge is one-way at the variant level (sui::Value can degrade
35//!     to TypescapeValue; TypescapeValue can lift back to sui::Value
36//!     LOSSLESSLY because no Lambda/Builtin/Thunk ever appears)
37//!
38//! ## The Typescape trait
39//!
40//! Every typed primitive that participates in the live-config loop
41//! implements [`Typescape`]:
42//!
43//! ```rust
44//! use engenho_sui_typescape::{Typescape, TypescapeValue, TypescapeError};
45//!
46//! struct MyDomain { n: u64 }
47//!
48//! impl Typescape for MyDomain {
49//!     fn to_typescape_value(&self) -> TypescapeValue {
50//!         TypescapeValue::int(self.n as i64)
51//!     }
52//!     fn from_typescape_value(v: &TypescapeValue) -> Result<Self, TypescapeError> {
53//!         Ok(Self { n: v.as_int()? as u64 })
54//!     }
55//! }
56//! ```
57//!
58//! Round-trip property: for every well-formed `T`,
59//! `T::from_typescape_value(&T::to_typescape_value(&t))? == t`.
60//!
61//! ## Why a hand-defined `TypescapeValue` (not sui's `Value`)
62//!
63//! Building the bridge against sui's evolving public surface today
64//! means rebuilding the bridge on every sui release. Instead the
65//! bridge speaks `TypescapeValue` (this crate's stable surface);
66//! `with-sui-eval` (future, gated behind a Cargo feature) adapts
67//! `sui_eval::Value → TypescapeValue` once sui's API hardens. The
68//! adapter is a single ~50-line file; nothing in engenho-fonte /
69//! engenho-controllers / the TataraDomains depends on sui directly.
70//!
71//! ## What plugs into this
72//!
73//!   - **engenho-fonte** consumes [`Typescape`]: a `(defsistema …)`
74//!     form parsed via tlisp → TypescapeValue → `Sistema` typed value
75//!     → Raft propose → reconcile.
76//!   - **arch-synthesizer typescape** registers [`Typescape`] impls for
77//!     every TataraDomain alongside the existing Serialize/Deserialize.
78//!   - **caixa / pangea / magma / viggy promessa** all gain [`Typescape`]
79//!     so a `(defsistema)` can refer to them by name without losing
80//!     type-safety at the bridge.
81
82#![forbid(unsafe_code)]
83#![warn(missing_docs)]
84
85mod error;
86mod ext;
87#[cfg(feature = "with-sui-eval")]
88mod sui_adapter;
89mod value;
90
91pub use error::TypescapeError;
92pub use ext::Typescape;
93#[cfg(feature = "with-sui-eval")]
94pub use sui_adapter::{eval_nix_str, from_sui_value};
95pub use value::{TypescapeAttrs, TypescapeList, TypescapeString, TypescapeValue};