Skip to main content

Command

Struct Command 

Source
pub struct Command {
    pub msg_id: u16,
    pub operation: Operation,
    pub object: Option<String>,
    pub data: Option<Vec<u8>>,
}
Expand description

Represents a parsed or to-be-sent PK Command.

A command consists of a 2-character base-94 msg_id, a 5-character operation, an optional 5-character object, and optional variable-length data.

Fields§

§msg_id: u16

The numeric message ID (0-8835).

§operation: Operation

The operation to be performed.

§object: Option<String>

The target object of the operation (e.g., variable or method name).

§data: Option<Vec<u8>>

Optional payload data.

Implementations§

Source§

impl Command

Source

pub fn parse(msg_bytes: &[u8]) -> Result<Command, &'static str>

Parses a byte slice into a Command struct.

The input must follow the protocol format: [ID][OP] [OBJ] [DATA].

§Arguments
  • msg_bytes: The raw bytes received from the transport layer.
§Returns

A Result containing the parsed Command or an error message.

§Errors

Returns an error if the byte slice is not a valid PK Command. (For example, the length is too short, the MSG ID is invalid, or the operation name is unrecognized.)

Source

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

Serializes the Command into a Vec<u8> for transmission.

This method ensures the output matches the fixed-length field requirements of the PK Command protocol.

§Panics

Panics if the msg_id is out of the valid 0-8835 range. This usually indicates a tragic programming error.

§Examples
use pk_command::types::{Command, Operation};
let cmd = Command {
    msg_id: 2,
    operation: Operation::SendVariable,
    object: Some("VARIA".to_string()),
    data: Some(b"payload".to_vec()),
};
assert_eq!(cmd.to_bytes(), b"!#SENDV VARIA payload".to_vec());
use pk_command::types::{Command, Operation};
let cmd = Command {
    msg_id: 9000, // Invalid MSG ID (greater than 8835)
    operation: Operation::SendVariable,
    object: Some("VARIA".to_string()),
    data: Some(b"payload".to_vec()),
};
cmd.to_bytes(); // This should panic due to invalid MSG ID

Trait Implementations§

Source§

impl Clone for Command

Source§

fn clone(&self) -> Command

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 Command

Source§

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

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

impl Display for Command

Source§

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

Formats the command for debugging or logging purposes.

The output mimics the protocol format, but ensures non-printable data is handled gracefully.

§Example
use pk_command::types::{Command, Operation};
let cmd = Command {
    msg_id: 0,
    operation: Operation::Error,
    object: Some("ERROR".to_string()),
    data: Some(b"Some error description".to_vec()),
};
assert_eq!(format!("{}", cmd), "  ERROR ERROR Some error description");

let cmd_non_printable = Command {
    msg_id: 1145,
    operation: Operation::Data,
    object: Some("QUERY".to_string()),
    data: Some(vec![0xFF, 0x00, 0xAB]),
};

// "-2" is the base-94 encoding of 1145, and the data is non-printable,
// so it should show as "<BINARY DATA>".
assert_eq!(
    format!("{}", cmd_non_printable),
    "-2SDATA QUERY <BINARY DATA: 3 bytes>"
);

let cmd_utf8 = Command {
    msg_id: 1145,
    operation: Operation::Data,
    object: Some("QUERY".to_string()),
    data: Some("汉字🐼".as_bytes().to_vec()),
};
// The data is valid UTF-8, so it should be displayed as is.
assert_eq!(format!("{}", cmd_utf8), "-2SDATA QUERY 汉字🐼");
Source§

impl PartialEq for Command

Source§

fn eq(&self, other: &Command) -> 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 Command

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.