loro_protocol/
lib.rs

1//! # Loro Syncing Protocol (Rust)
2//!
3//! Rust encoder/decoder for the Loro wire format described in `/protocol.md`
4//! It mirrors the TypeScript implementation in `packages/loro-protocol` and
5//! is shared by the Rust WebSocket server/client crates.
6//!
7//! ## Guarantees
8//! - Enforces the 256 KiB per-message limit during encoding
9//! - Validates CRDT magic bytes and room id length (<= 128 bytes)
10//! - Uses compact, allocation-friendly varint encoding for strings/bytes
11//!
12//! ## Crate layout
13//! - `protocol`: message enums/constants and the `ProtocolMessage` enum
14//! - `encoding`: `encode`, `decode`, and `try_decode` helpers
15//! - `bytes`: `BytesWriter`/`BytesReader` for varint-friendly buffers
16//!
17//! ## Quick start
18//!
19//! ```
20//! use loro_protocol::{encode, decode, ProtocolMessage, CrdtType};
21//!
22//! // Build a JoinRequest
23//! let msg = ProtocolMessage::JoinRequest {
24//!     crdt: CrdtType::Loro,
25//!     room_id: "room-123".to_string(),
26//!     auth: vec![10,20,30],
27//!     version: vec![40,50,60],
28//! };
29//!
30//! // Encode to bytes and decode back
31//! let bytes = encode(&msg).unwrap();
32//! let roundtrip = decode(&bytes).unwrap();
33//! assert_eq!(roundtrip, msg);
34//! ```
35//!
36//! ## Streaming decode
37//!
38//! ```
39//! use loro_protocol::{encode, try_decode, ProtocolMessage, CrdtType};
40//!
41//! // Prepare any valid message buffer
42//! let buf = encode(&ProtocolMessage::Leave {
43//!     crdt: CrdtType::Loro,
44//!     room_id: "room-123".into(),
45//! }).unwrap();
46//!
47//! let maybe = try_decode(&buf);
48//! assert!(maybe.is_some());
49//! ```
50//!
51//! See `protocol.md` and `protocol-e2ee.md` for the full field ordering and
52//! validation rules.
53
54pub mod bytes;
55pub mod elo;
56pub mod encoding;
57pub mod protocol;
58
59pub use bytes::{BytesReader, BytesWriter};
60pub use elo::*;
61pub use encoding::{decode, encode, try_decode};
62pub use protocol::*;