Skip to main content

peat_lite/protocol/
message_type.rs

1//! Peat-Lite message type identifiers.
2
3/// Message types for the gossip protocol.
4///
5/// Marked `#[non_exhaustive]` so future protocol amendments can add
6/// variants without breaking exhaustive-match consumers in downstream
7/// crates (peat-mesh, peat-btle, peat-atak-plugin). Match arms must
8/// include a `_ =>` fall-through to compile against the latest
9/// peat-lite.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[non_exhaustive]
12#[repr(u8)]
13pub enum MessageType {
14    /// Announce presence and capabilities.
15    Announce = 0x01,
16    /// Heartbeat / keep-alive.
17    Heartbeat = 0x02,
18    /// Data update (CRDT state).
19    Data = 0x03,
20    /// Query for specific state.
21    Query = 0x04,
22    /// Acknowledge receipt.
23    Ack = 0x05,
24    /// Leave notification.
25    Leave = 0x06,
26    /// Universal Document carrier (peat_mesh::Document, transport-agnostic).
27    ///
28    /// Distinct from [`MessageType::Data`]'s typed-CRDT-primitive payload —
29    /// `Document` carries an opaque envelope: collection name + doc id +
30    /// timestamp + length-prefixed body bytes whose interpretation is owned
31    /// by peat-mesh (or any consumer). Adding a new collection on the
32    /// network requires zero codec changes downstream of peat-lite.
33    /// See `protocol::document` for the wire layout.
34    Document = 0x07,
35    /// OTA firmware offer (Full -> Lite).
36    OtaOffer = 0x10,
37    /// OTA accept (Lite -> Full).
38    OtaAccept = 0x11,
39    /// OTA data chunk (Full -> Lite).
40    OtaData = 0x12,
41    /// OTA chunk acknowledgement (Lite -> Full).
42    OtaAck = 0x13,
43    /// OTA transfer complete (Full -> Lite).
44    OtaComplete = 0x14,
45    /// OTA result (Lite -> Full).
46    OtaResult = 0x15,
47    /// OTA abort (either direction).
48    OtaAbort = 0x16,
49}
50
51impl MessageType {
52    /// Convert a raw byte to a `MessageType`, if valid.
53    pub fn from_u8(v: u8) -> Option<Self> {
54        match v {
55            0x01 => Some(Self::Announce),
56            0x02 => Some(Self::Heartbeat),
57            0x03 => Some(Self::Data),
58            0x04 => Some(Self::Query),
59            0x05 => Some(Self::Ack),
60            0x06 => Some(Self::Leave),
61            0x07 => Some(Self::Document),
62            0x10 => Some(Self::OtaOffer),
63            0x11 => Some(Self::OtaAccept),
64            0x12 => Some(Self::OtaData),
65            0x13 => Some(Self::OtaAck),
66            0x14 => Some(Self::OtaComplete),
67            0x15 => Some(Self::OtaResult),
68            0x16 => Some(Self::OtaAbort),
69            _ => None,
70        }
71    }
72}