Crate loro_protocol

Crate loro_protocol 

Source
Expand description

§Loro Syncing Protocol (Rust)

Rust encoder/decoder for the Loro wire format described in /protocol.md It mirrors the TypeScript implementation in packages/loro-protocol and is shared by the Rust WebSocket server/client crates.

§Guarantees

  • Enforces the 256 KiB per-message limit during encoding
  • Validates CRDT magic bytes and room id length (<= 128 bytes)
  • Uses compact, allocation-friendly varint encoding for strings/bytes

§Crate layout

  • protocol: message enums/constants and the ProtocolMessage enum
  • encoding: encode, decode, and try_decode helpers
  • bytes: BytesWriter/BytesReader for varint-friendly buffers

§Quick start

use loro_protocol::{encode, decode, ProtocolMessage, CrdtType};

// Build a JoinRequest
let msg = ProtocolMessage::JoinRequest {
    crdt: CrdtType::Loro,
    room_id: "room-123".to_string(),
    auth: vec![10,20,30],
    version: vec![40,50,60],
};

// Encode to bytes and decode back
let bytes = encode(&msg).unwrap();
let roundtrip = decode(&bytes).unwrap();
assert_eq!(roundtrip, msg);

§Streaming decode

use loro_protocol::{encode, try_decode, ProtocolMessage, CrdtType};

// Prepare any valid message buffer
let buf = encode(&ProtocolMessage::Leave {
    crdt: CrdtType::Loro,
    room_id: "room-123".into(),
}).unwrap();

let maybe = try_decode(&buf);
assert!(maybe.is_some());

See protocol.md and protocol-e2ee.md for the full field ordering and validation rules.

Re-exports§

pub use bytes::BytesReader;
pub use bytes::BytesWriter;
pub use encoding::decode;
pub use encoding::encode;
pub use encoding::try_decode;
pub use elo::*;
pub use protocol::*;

Modules§

bytes
elo
%ELO container and record header parsing (no crypto). Mirrors the TypeScript implementation in packages/loro-protocol/src/e2ee.ts for container decode and plaintext header parsing. This module is intentionally crypto-free; consumers can use the parsed aad (exact header bytes) and iv with their own AES-GCM bindings if desired. NOTE: %ELO support on the Rust side is work-in-progress; only the container and header parsing surface is considered stable today.
encoding
Binary encoder/decoder for the Loro protocol wire format.
protocol
Protocol types and constants mirrored from protocol.md and the TS package.