pub struct Header { /* private fields */ }Expand description
Decoded wire header. magic and header_crc32 are not stored: magic is always
b"WEIR" after a successful decode (storing it invites confusion about validity),
and CRC is computed on encode rather than held as state.
Fields are private and read-only (accessors below): a header is only ever
produced by Header::new (which pins version to WIRE_VERSION) or by
Header::decode (which validates every field). This makes it impossible
for a caller to desync payload_len from the real payload or set version
off WIRE_VERSION after construction.
Implementations§
Source§impl Header
impl Header
Sourcepub fn new(message_type: MessageType, durability: Durability, flags: u8) -> Self
pub fn new(message_type: MessageType, durability: Durability, flags: u8) -> Self
Constructs a header for outbound frames. Version is always WIRE_VERSION.
payload_len is NOT a parameter: it is set to 0 here and becomes
authoritative only when the header is wrapped in an Envelope, which
derives it from the actual payload (see Envelope::new). This makes a
header whose declared length disagrees with its payload unrepresentable —
a bare Header::encode always declares payload_len = 0, so it is only
valid for the genuinely-empty-payload frames (Ack / HealthCheck) that use
it (F50).
In wire v1 flags MUST be 0. Any other value encodes a frame that
Header::decode rejects with DecodeError::ReservedFlagsSet (and the
daemon Nacks then closes the connection); the parameter is kept non-zero-
capable only so the codec can construct reject-path test frames.
Sourcepub fn version(&self) -> u8
pub fn version(&self) -> u8
Wire protocol version. Always WIRE_VERSION for a header built by
Header::new; a decoded header only exists if its version matched.
Sourcepub fn message_type(&self) -> MessageType
pub fn message_type(&self) -> MessageType
The frame’s MessageType.
Sourcepub fn durability(&self) -> Durability
pub fn durability(&self) -> Durability
The frame’s Durability tier.
Sourcepub fn flags(&self) -> u8
pub fn flags(&self) -> u8
Reserved flags byte (zero in v1; preserved verbatim through the codec).
Sourcepub fn payload_len(&self) -> u32
pub fn payload_len(&self) -> u32
Declared payload length in bytes. For an Envelope this always equals
the actual payload length (see Envelope::new); on a bare decoded
header it is the wire-declared length used to read the payload.
Sourcepub fn encode(&self) -> [u8; 16]
pub fn encode(&self) -> [u8; 16]
Serialises the header to exactly HEADER_LEN bytes. CRC is computed here;
the caller does not need to know the CRC coverage range.
Sourcepub fn decode(buf: &[u8; 16]) -> Result<Self, DecodeError>
pub fn decode(buf: &[u8; 16]) -> Result<Self, DecodeError>
Decodes and validates a 16-byte header buffer.
Validation order is deliberate:
- Magic — cheapest check; eliminates non-weir traffic before any further work.
- Version — before CRC, so a version-mismatched frame from a v2 client receives
VersionMismatch(with the daemon’s version in the Nack payload) rather than a confusingHeaderCrcMismatchif the frame layout shifted between versions. - Header CRC — validates the remaining bytes are uncorrupted before parsing them.
- Typed field parsing — only after all integrity checks pass.