zaun_community_protocol/envelope.rs
1//! Envelopes (specification §7) and error bodies (§11).
2//!
3//! Bodies stay raw JSON ([`RawValue`]) here: the `command` and `event` modules parse them by
4//! `type`, and the idempotency body hash (§7.3) is taken over the body text exactly as received.
5//!
6//! - [`parse_command`] turns a whole or reassembled envelope text into a [`CommandEnvelope`] or
7//! a [`Rejection`] carrying the `BAD_REQUEST` / `PAYLOAD_TOO_LARGE` /
8//! `UNSUPPORTED_VERSION` response to send.
9//! - [`ResponseEnvelope`] and [`ErrorBody`] (one constructor per [`ErrorCode`], fixed messages,
10//! no caller-supplied free text) build every response.
11//! - [`EventEnvelope`] validates the stream/sequence rules of §7.4; [`EventsBatch`] the
12//! `events` batch of §10.5.
13//! - [`is_mutating`], [`IdempotencyKey`] and [`BodyHash`] implement §7.3.
14//!
15//! [`RawValue`]: serde_json::value::RawValue
16//! [`ErrorCode`]: crate::error_code::ErrorCode
17
18mod command_envelope;
19mod error_body;
20mod error_enums;
21mod error_extras;
22mod errors;
23mod event_envelope;
24mod events_batch;
25mod idempotency;
26mod inbound;
27mod raw;
28mod response;
29mod version;
30
31use serde::{Deserialize, Serialize};
32
33pub use command_envelope::CommandEnvelope;
34pub use error_body::{ErrorBody, ErrorExtras};
35pub use error_enums::{RateCategory, RateScope, Reason};
36pub use event_envelope::{EventEnvelope, EventError, MAX_JSON_INTEGER};
37pub use events_batch::{BatchError, EVENTS_TYPE, EventsBatch};
38pub use idempotency::{BodyHash, IdempotencyKey, Replay, is_mutating, is_recorded, replay};
39pub use inbound::{Rejection, command_limit, parse_command};
40pub use response::{Outcome, ResponseEnvelope};
41pub use version::{check_version, negotiate};
42
43/// The `type` of every response envelope.
44pub const RESPONSE_TYPE: &str = "response";
45
46/// An inclusive protocol version range (`session.hello.versions`, `UNSUPPORTED_VERSION`).
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
48pub struct VersionRange {
49 /// Lowest supported version.
50 pub min: u16,
51 /// Highest supported version.
52 pub max: u16,
53}
54
55impl VersionRange {
56 /// The range this crate implements: exactly [`crate::limits::PROTOCOL_VERSION`].
57 pub const CURRENT: Self = Self {
58 min: crate::limits::PROTOCOL_VERSION,
59 max: crate::limits::PROTOCOL_VERSION,
60 };
61
62 /// Whether `v` lies in the range.
63 #[must_use]
64 pub const fn contains(&self, v: u16) -> bool {
65 self.min <= v && v <= self.max
66 }
67}
68
69/// An event stream (§7.4, §9.1); also the `stream` of `RESYNC_REQUIRED`.
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
71#[serde(rename_all = "snake_case")]
72pub enum Stream {
73 /// A channel's gapless `channel_seq` stream.
74 Channel,
75 /// The member's gapless `member_seq` stream.
76 Member,
77 /// Unsequenced live events (signals, `member.removed`, …).
78 Live,
79}