pub struct Msg {
pub header: Option<Header>,
pub body: Option<Body>,
}
Fields§
§header: Option<Header>
§body: Option<Body>
Implementations§
Source§impl Msg
Implementation of some extension methods for Msg
s
impl Msg
Implementation of some extension methods for Msg
s
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Tries to decode a slice of bytes containing a Protobuf encoded USP Message
This function also performs additional checks required by the USP specification, see also
Msg::check_validity
§Arguments
bytes
- A slice of bytes containing the Protobuf encoded USP Message
§Example
use rusp_lib::usp::Msg;
let msg =
Msg::from_bytes(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
])
.unwrap();
Sourcepub fn msg_id(&self) -> &str
pub fn msg_id(&self) -> &str
Retrieves the message ID from a Msg structure
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
]);
assert_eq!(msg.unwrap().msg_id(), "AXSS-1544114045.442596");
Sourcepub const fn is_request(&self) -> bool
pub const fn is_request(&self) -> bool
Checks whether the body contains a message of type request
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert_eq!(msg.is_request(), true);
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
]).unwrap();
assert_eq!(msg.is_request(), false);
Sourcepub const fn is_notify_request(&self) -> bool
pub const fn is_notify_request(&self) -> bool
Checks whether the body contains a message of type notify (request)
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert_eq!(msg.is_notify_request(), true);
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
]).unwrap();
assert_eq!(msg.is_notify_request(), false);
Sourcepub const fn get_notify_request(&self) -> Option<&Notify>
pub const fn get_notify_request(&self) -> Option<&Notify>
Retrieves the notify request from the Msg
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert!(msg.get_notify_request().is_some());
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
]).unwrap();
assert!(msg.get_notify_request().is_none());
Sourcepub const fn is_response(&self) -> bool
pub const fn is_response(&self) -> bool
Checks whether the body contains a message of type response
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert_eq!(msg.is_response(), false);
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
]).unwrap();
assert_eq!(msg.is_response(), true);
Sourcepub fn is_error(&self) -> bool
pub fn is_error(&self) -> bool
Checks whether the body contains a message of type response
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert_eq!(msg.is_error(), false);
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
]).unwrap();
assert_eq!(msg.is_error(), false);
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x05, 0x0a, 0x03, 0x65, 0x72, 0x72, 0x12,
0x17, 0x1a, 0x15, 0x0d, 0x5b, 0x1b, 0x00, 0x00,
0x12, 0x0e, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
0x61, 0x6c, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72,
]).unwrap();
assert_eq!(msg.is_error(), true);
Sourcepub fn get_error(&self) -> Option<Error>
pub fn get_error(&self) -> Option<Error>
Retrieves the notify request from the Msg
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert!(msg.get_error().is_none());
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x1a, 0x0a, 0x16, 0x41, 0x58, 0x53, 0x53, 0x2d, 0x31, 0x35, 0x34,
0x34, 0x31, 0x31, 0x34, 0x30, 0x34, 0x35, 0x2e, 0x34, 0x34, 0x32, 0x35,
0x39, 0x36, 0x10, 0x02, 0x12, 0x46, 0x12, 0x44, 0x0a, 0x42, 0x0a, 0x40,
0x0a, 0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4c, 0x6f, 0x63,
0x61, 0x6c, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x2e, 0x4d, 0x54, 0x50, 0x2e,
0x31, 0x2e, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2e,
0x15, 0x62, 0x1b, 0x00, 0x00, 0x1a, 0x15, 0x55, 0x6e, 0x73, 0x75, 0x70,
0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x20, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x65, 0x74, 0x65, 0x72
]).unwrap();
assert!(msg.get_error().is_none());
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x05, 0x0a, 0x03, 0x65, 0x72, 0x72, 0x12,
0x17, 0x1a, 0x15, 0x0d, 0x5b, 0x1b, 0x00, 0x00,
0x12, 0x0e, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
0x61, 0x6c, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72,
]).unwrap();
assert!(msg.get_error().is_some());
Sourcepub fn check_validity(&self) -> Result<()>
pub fn check_validity(&self) -> Result<()>
Checks the validity of this Msg
according to the USP specification
Although the type itself guarantees its validity against the protobuf schema, the USP specification places additional constrains on the values used
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x09, 0x0a, 0x07, 0x6e, 0x6f, 0x2d, 0x62,
0x6f, 0x64, 0x79, 0x12, 0x00,
]).unwrap();
assert!(msg.check_validity().is_err());
Source§impl Msg
Implementation of some extension methods for Msg
s
impl Msg
Implementation of some extension methods for Msg
s
Sourcepub fn to_json(&self) -> Result<String>
pub fn to_json(&self) -> Result<String>
Encode the Msg as JSON format
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert_eq!(msg.to_json().unwrap(), "{\"Header\":{\"msg_id\":\"test\",\"msg_type\":\"NOTIFY\"},\"Body\":{\"Request\":{\"Notify\":{\"subscription_id\":\"notif\",\"send_resp\":true,\"on_board_req\":{\"oui\":\"0044FF\",\"product_class\":\"Foo\",\"serial_number\":\"01234\",\"agent_supported_protocol_versions\":\"1.3\"}}}}}");
Sourcepub fn to_json_pretty(&self) -> Result<String>
pub fn to_json_pretty(&self) -> Result<String>
Encode the Msg in pretty printed JSON format
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let msg =
try_decode_msg(&[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
]).unwrap();
assert_eq!(msg.to_json_pretty().unwrap(), "{\n \"Header\": {\n \"msg_id\": \"test\",\n \"msg_type\": \"NOTIFY\"\n },\n \"Body\": {\n \"Request\": {\n \"Notify\": {\n \"subscription_id\": \"notif\",\n \"send_resp\": true,\n \"on_board_req\": {\n \"oui\": \"0044FF\",\n \"product_class\": \"Foo\",\n \"serial_number\": \"01234\",\n \"agent_supported_protocol_versions\": \"1.3\"\n }\n }\n }\n }\n}");
Sourcepub fn to_vec(&self) -> Result<Vec<u8>>
pub fn to_vec(&self) -> Result<Vec<u8>>
Encode the Msg into a Protobuf byte stream returned as Vec<[u8]>
§Arguments
self
- A decoded USP Msg structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let bytes = &[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
];
let msg = try_decode_msg(bytes).unwrap();
assert_eq!(msg.to_vec().unwrap(), bytes);
Sourcepub fn to_c_str(&self) -> Result<String>
pub fn to_c_str(&self) -> Result<String>
Render the Msg
into a raw C string representation
§Arguments
self
- A USPMsg
structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let bytes = &[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
];
let msg = try_decode_msg(bytes).unwrap();
assert_eq!(msg.to_c_str().unwrap(), "\"\\x0a\\x08\\x0a\\x04test\\x10\\x03\\x12(\\x0a\\x26B\\x24\\x0a\\x05notif\\x10\\x01B\\x19\\x0a\\x060044FF\\x12\\x03Foo\\x1a\\x0501234\\x22\\x031.3\"\n");
Sourcepub fn to_c_array(&self) -> Result<String>
pub fn to_c_array(&self) -> Result<String>
Render the Msg
into a raw C array representation
§Arguments
self
- A USPMsg
structure
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let bytes = &[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
];
let msg = try_decode_msg(bytes).unwrap();
assert_eq!(msg.to_c_array().unwrap(), "unsigned int pb_len = 52;\nconst char pb[] = {\n 0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, /* ____test */\n 0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24, /* ___(__B_ */\n 0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10, /* __notif_ */\n 0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34, /* _B___004 */\n 0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f, /* 4FF__Foo */\n 0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22, /* __01234\" */\n 0x03, 0x31, 0x2e, 0x33, /* _1.3 */\n};\n");
Sourcepub fn to_c_array_custom(&self, name: &str) -> Result<String>
pub fn to_c_array_custom(&self, name: &str) -> Result<String>
Render the Msg
into a raw C array representation with a custom name
§Arguments
self
- A USPMsg
structurename
- The variable name prefix used in the rendered output
§Example
use rusp_lib::usp_decoder::try_decode_msg;
let bytes = &[
0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74,
0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24,
0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10,
0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34,
0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f,
0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22,
0x03, 0x31, 0x2e, 0x33,
];
let msg = try_decode_msg(bytes).unwrap();
assert_eq!(msg.to_c_array_custom("msg").unwrap(), "unsigned int msg_len = 52;\nconst char msg[] = {\n 0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, /* ____test */\n 0x10, 0x03, 0x12, 0x28, 0x0a, 0x26, 0x42, 0x24, /* ___(__B_ */\n 0x0a, 0x05, 0x6e, 0x6f, 0x74, 0x69, 0x66, 0x10, /* __notif_ */\n 0x01, 0x42, 0x19, 0x0a, 0x06, 0x30, 0x30, 0x34, /* _B___004 */\n 0x34, 0x46, 0x46, 0x12, 0x03, 0x46, 0x6f, 0x6f, /* 4FF__Foo */\n 0x1a, 0x05, 0x30, 0x31, 0x32, 0x33, 0x34, 0x22, /* __01234\" */\n 0x03, 0x31, 0x2e, 0x33, /* _1.3 */\n};\n");
Trait Implementations§
Source§impl<'a> MessageRead<'a> for Msg
impl<'a> MessageRead<'a> for Msg
Source§fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self>
fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self>
Self
by reading from the given bytes
via the given reader. Read more