moq_json/lib.rs
1//! JSON publishing over [`moq-net`](moq_net) tracks, in three modes:
2//!
3//! - [`snapshot`]: **lossy**. One JSON value updated over time; a consumer only gets the most
4//! recent value. Intermediate updates are collapsed and older groups are dropped.
5//! - [`stream`]: **lossless**. An ordered append-log of self-contained records; every record is
6//! preserved and delivered in order, nothing is ever superseded.
7//! - [`window`]: **bounded**. An ordered run of records appended to the back and dropped from the
8//! front, which a reader can join at any point.
9//!
10//! Pick [`snapshot`] when consumers care about "what is the value now" (a catalog, a status
11//! document), [`stream`] when they care about every record of an unbounded log, and [`window`] when
12//! the publisher retires old records and a late reader should start from what is still retained.
13//!
14//! Each mode comes in two layers. `Producer`/`Consumer` own a [`moq_net`] track and manage its
15//! groups. `Encoder`/`Decoder` are the same logic without the track: values in, frame payloads out
16//! (and back), with the encoder saying where the group boundaries fall. Reach for the codec layer
17//! when something else already owns the track, such as a `moq_mux::container::Producer` also
18//! managing a timeline and a catalog estimate.
19
20mod diff;
21pub mod snapshot;
22pub mod stream;
23pub mod window;
24
25pub use crate::diff::{Diff, diff};
26
27/// Errors produced while publishing or consuming JSON.
28#[derive(thiserror::Error, Debug, Clone)]
29#[non_exhaustive]
30pub enum Error {
31 /// An error from the underlying track.
32 #[error(transparent)]
33 Net(#[from] moq_net::Error),
34
35 /// A value failed to serialize, deserialize, or apply as a merge patch.
36 ///
37 /// Stored as a string since [`serde_json::Error`] is not [`Clone`].
38 #[error("json: {0}")]
39 Json(String),
40
41 /// A compressed frame could not be decoded (malformed, truncated, or oversized).
42 #[error(transparent)]
43 Flate(#[from] moq_flate::Error),
44
45 /// A merge patch arrived with no snapshot to apply it to.
46 ///
47 /// Every group opens with a full snapshot, so this means frames reached
48 /// [`snapshot::Decoder`] out of order, or a group's first frame was routed as a delta.
49 #[error("delta before snapshot")]
50 MissingSnapshot,
51
52 /// A compressed [`stream`] frame was encoded but never written, so the shared DEFLATE window is
53 /// ahead of what the consumer holds and nothing later in this group can be decoded.
54 ///
55 /// Unlike [`snapshot`], a stream has no keyframe to resynchronize on, so the encoder refuses to
56 /// continue rather than emit frames that cannot be read. Recover by rolling a new group and
57 /// calling [`stream::Encoder::reset`].
58 #[error("compression desynchronized: a frame was encoded but never written")]
59 Desync,
60}
61
62impl From<serde_json::Error> for Error {
63 fn from(err: serde_json::Error) -> Self {
64 Error::Json(err.to_string())
65 }
66}
67
68/// A [`Result`](std::result::Result) using this crate's [`Error`].
69pub type Result<T> = std::result::Result<T, Error>;