umsh_cli/mac_cmd.rs
1//! Helper for encoding a `MacCommand` as an outbound payload without
2//! touching the node crate's public API.
3//!
4//! Rationale: adding `PeerConnection::send_mac_command` to the node crate
5//! would duplicate send-plumbing for a single use case. Instead, we prefix
6//! the existing `mac_command::encode` output with `PayloadType::MacCommand`
7//! and call the normal `PeerConnection::send`.
8
9use umsh_core::PayloadType;
10use umsh_node::{AppEncodeError, MacCommand};
11
12/// Encode `cmd` into `out` as a complete MAC-command payload
13/// (`PayloadType::MacCommand` byte + encoded command body). Returns the
14/// number of bytes written.
15pub fn encode_mac_command(cmd: &MacCommand<'_>, out: &mut [u8]) -> Result<usize, AppEncodeError> {
16 if out.is_empty() {
17 return Err(AppEncodeError::BufferTooSmall);
18 }
19 out[0] = PayloadType::MacCommand as u8;
20 let n = umsh_node::mac_command::encode(cmd, &mut out[1..])?;
21 Ok(n + 1)
22}