ChatMessage

Struct ChatMessage 

Source
pub struct ChatMessage {
    pub origin_node: u32,
    pub timestamp: u64,
    pub is_broadcast: bool,
    pub requires_ack: bool,
    pub reply_to_node: u32,
    pub reply_to_timestamp: u64,
    /* private fields */
}
Expand description

A single chat message in the mesh

Messages are uniquely identified by (origin_node, timestamp). This allows deduplication across mesh sync while preserving message ordering.

Fields§

§origin_node: u32

Node that originated this message

§timestamp: u64

Timestamp when message was created (ms since epoch)

§is_broadcast: bool

Whether this is a broadcast message (vs directed)

§requires_ack: bool

Whether ACK is requested

§reply_to_node: u32

Reply-to: origin node of the message being replied to (0 = not a reply)

§reply_to_timestamp: u64

Reply-to: timestamp of the message being replied to (0 = not a reply)

Implementations§

Source§

impl ChatMessage

Source

pub fn new(origin_node: u32, timestamp: u64, sender: &str, text: &str) -> Self

Create a new chat message

Source

pub fn set_sender(&mut self, sender: &str)

Set the sender name (truncated to 12 bytes)

Source

pub fn sender(&self) -> &str

Get the sender name as a string

Source

pub fn set_text(&mut self, text: &str)

Set the message text (truncated to 128 bytes)

Source

pub fn text(&self) -> &str

Get the message text as a string

Source

pub fn set_reply_to(&mut self, node: u32, timestamp: u64)

Set reply-to information

Source

pub fn is_reply(&self) -> bool

Check if this is a reply to another message

Source

pub fn message_id(&self) -> u64

Get the unique message ID (combines origin_node and timestamp)

Format: (origin_node as u64) << 32 | (timestamp & 0xFFFFFFFF) This provides a sortable key where messages from same node are ordered by time.

Source

pub fn encode(&self) -> Vec<u8>

Encode to bytes for transmission

Wire format:

origin_node:       4 bytes (LE)
timestamp:         8 bytes (LE)
sender_len:        1 byte
sender:            sender_len bytes
text_len:          1 byte
text:              text_len bytes
flags:             1 byte (bit 0: is_broadcast, bit 1: requires_ack)
reply_to_node:     4 bytes (LE)
reply_to_timestamp: 8 bytes (LE)
Source

pub fn decode(data: &[u8]) -> Option<(Self, usize)>

Decode from bytes with strict validation

Returns None if the data is malformed or fails validation checks. This is a security-critical function - malformed messages could be spoofed or part of an attack.

§Validation checks:
  • origin_node must be non-zero
  • timestamp must be non-zero and within reasonable bounds
  • sender must be non-empty and valid UTF-8
  • text must be valid UTF-8 (can be empty for ACK messages)
  • All length fields must be within bounds

Trait Implementations§

Source§

impl Clone for ChatMessage

Source§

fn clone(&self) -> ChatMessage

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ChatMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ChatMessage

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for ChatMessage

Source§

fn eq(&self, other: &ChatMessage) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ChatMessage

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.