# zwire-protocol

> Zero-dependency deterministic RFC-8259 JSON hydration, StatePatch, ExternalSource, and SyncBridge in Rust.

Zero-dependency, `#![forbid(unsafe_code)]`, `#![no_std]` (`alloc`) **open state wire protocols** for [`zwire-core`](https://github.com/kjmin-dev/zwire/blob/main/rust/crates/zwire-core/README.md) with 100% JSON schema compatibility with [`@zwire/core/protocol`](https://github.com/kjmin-dev/zwire/blob/main/ts/packages/core/README.md) and [`spec/protocol.md`](https://github.com/kjmin-dev/zwire/blob/main/spec/protocol.md).

## Primitives

- **`WireValue` + `WireCodec`:** Zero-dependency RFC-8259 JSON serializer/parser for cross-language state transfer (`Tauri`, `Axum`, `WebSocket`, `Cloudflare Workers`).
- **Out-of-order Hydration (`hydratable`, `dehydrate`, `hydrate`, `dehydrate_json`, `hydrate_json`):** Serialize committed base state from a Rust scope and hydrate directly into `@zwire/core/protocol` (or vice versa), with delete-on-consume buffering for lazily registered atoms.
- **`ExternalSource<T>` & `from_external_source()`:** First-class `F_EXTERNAL` reactive signals with unwatched snapshot polling, lazy subscription attachment, stale post-unsubscribe callback protection, and STM read-verify support.
- **`SyncAdapter` & `SyncBridge`:** Batched local `StatePatch` emission per scope flush, key filtering, remote echo suppression, and remote patch application that automatically rebases active `zwire-tx` optimistic layers.
- **`InspectorHook` & `attach_inspector()`:** Zero-cost multiplexed tracing (`WriteTraceEvent`, `ComputeTraceEvent`, `FlushTraceEvent`, `ErrorSource`).

The bridge retains the latest 64 inbound and 64 outbound transaction IDs independently. The default ID generator uses a randomized per-bridge prefix on standard-library hosts with process entropy. On `no_std` or deterministic WASM hosts, configure `SyncBridgeOptions::create_tx_id` with a unique connection namespace and increasing sequence when connecting separate peers.

## Quick Start

```rust
use zwire_core::Scope;
use zwire_protocol::{dehydrate_json, hydratable, hydrate_json};

// Server scope: dehydrate keyed atoms into deterministic JSON
let server = Scope::new();
let server_count = hydratable(&server, "count", 42_i64);
assert_eq!(server.get(server_count), 42);
let json = dehydrate_json(&server);

// Client scope: out-of-order hydration before atoms are registered
let client = Scope::new();
hydrate_json(&client, &json).unwrap();
let client_count = hydratable(&client, "count", 0_i64);
assert_eq!(client.get(client_count), 42);
```

## Typed wire objects

The optional `derive` feature provides `#[derive(WireCodec)]` for concrete named-field structs. Each field uses its own codec, preserving the decimal string representation of large integers. The decoder accepts extra fields and reports missing fields through `WireError::MissingField`. Maps with string keys and tuples of one to four items also implement the trait.

```rust
# #[cfg(feature = "derive")]
# {
use zwire_protocol::WireCodec;

#[derive(PartialEq, Debug, WireCodec)]
struct Position { sequence: u64, point: (i16, i16) }

let position = Position { sequence: u64::MAX, point: (2, 3) };
assert_eq!(Position::from_wire(&position.to_wire()), Ok(position));
# }
```
