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: u16The numeric message ID (0-8835).
operation: OperationThe 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
impl Command
Sourcepub fn parse(msg_bytes: &[u8]) -> Result<Command, &'static str>
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.)
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
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 IDTrait Implementations§
Source§impl Display for Command
impl Display for Command
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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 汉字🐼");