diff --git a/src/main.rs b/src/main.rs index 1e853b7..1b89104 100644 --- a/src/main.rs +++ b/src/main.rs @@ -680,7 +680,7 @@ fn get_out_stream(filename: Option) -> Result> { fn write_msg(msg: &rusp::usp::Msg, mut out: W, format: &OutputFormat) -> Result<()> { match format { OutputFormat::Json => { - out.write_all(&msg.to_json()?.into_bytes())?; + out.write_all(&msg.to_json_pretty()?.into_bytes())?; writeln!(out) } OutputFormat::CStr => out.write_all(&msg.to_c_str()?.into_bytes()), @@ -699,7 +699,7 @@ fn write_record( ) -> Result<()> { match format { OutputFormat::Json => { - out.write_all(&record.to_json()?.into_bytes())?; + out.write_all(&record.to_json_pretty()?.into_bytes())?; writeln!(out) } OutputFormat::CStr => out.write_all(&record.to_c_str()?.into_bytes()), diff --git a/src/usp_encoder.rs b/src/usp_encoder.rs index bf5235d..56023c8 100644 --- a/src/usp_encoder.rs +++ b/src/usp_encoder.rs @@ -3,51 +3,56 @@ use crate::usp_record::Record; use anyhow::{Context, Result}; -/// Decodes a slice of bytes containing a Protobuf encoded USP Record into a Record structure for -/// further processing +/// Encode the Record into a Protobuf byte stream returned as `Vec<[u8]>` /// /// # Arguments /// -/// * `bytes` - A slice of bytes containing the Protobuf encoded USP Record +/// * `self` - A USP `Record` structure /// /// # Example /// /// ``` /// use rusp::usp_decoder::try_decode_record; -/// let record = -/// try_decode_record(&[ -/// 0x0a, 0x03, 0x31, 0x2e, 0x30, 0x1a, 0x23, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3a, 0x3a, -/// 0x61, 0x78, 0x2d, 0x75, 0x73, 0x70, 0x2d, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x2d, 0x6e, -/// 0x6f, 0x73, 0x73, 0x6c, 0x2d, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, -/// 0x3a, 0x4d, 0x12, 0x4b, 0x0a, 0x08, 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x03, -/// 0x12, 0x3f, 0x0a, 0x3d, 0x42, 0x3b, 0x0a, 0x0f, 0x73, 0x75, 0x62, 0x73, 0x63, 0x72, -/// 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x69, 0x64, 0x42, 0x28, 0x0a, 0x03, 0x6f, -/// 0x75, 0x69, 0x12, 0x0d, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x5f, 0x63, 0x6c, -/// 0x61, 0x73, 0x73, 0x1a, 0x0d, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x5f, 0x6e, 0x75, -/// 0x6d, 0x62, 0x65, 0x72, 0x22, 0x03, 0x31, 0x2e, 0x30, -/// ]); +/// use rusp::usp_encoder::try_encode_record; +/// let bytes = &[ +/// 0x0a, 0x03, 0x31, 0x2e, 0x33, 0x12, 0x07, 0x64, +/// 0x6f, 0x63, 0x3a, 0x3a, 0x74, 0x6f, 0x1a, 0x09, +/// 0x64, 0x6f, 0x63, 0x3a, 0x3a, 0x66, 0x72, 0x6f, +/// 0x6d, 0x52, 0x09, 0x08, 0x01, 0x12, 0x05, 0x74, +/// 0x6f, 0x70, 0x69, 0x63, +/// ]; +/// let record = try_decode_record(bytes).unwrap(); +/// assert_eq!(try_encode_record(&record).unwrap(), bytes); /// ``` -pub fn try_encode_record(record: Record) -> Result> { - use quick_protobuf::{message::MessageWrite, Writer}; - - let mut buf = Vec::new(); - let mut writer = Writer::new(&mut buf); - record - .write_message(&mut writer) - .context("Failed encoding USP Record")?; - - Ok(buf) +pub fn try_encode_record(record: &Record) -> Result> { + record.to_vec() } -pub fn try_encode_msg(msg: Msg) -> Result> { - use quick_protobuf::{message::MessageWrite, Writer}; - - let mut buf = Vec::new(); - let mut writer = Writer::new(&mut buf); - msg.write_message(&mut writer) - .context("Failed encoding USP Msg")?; - - Ok(buf) +/// Encode the Msg into a Protobuf byte stream returned as `Vec<[u8]>` +/// +/// # Arguments +/// +/// * `self` - A decoded USP Msg structure +/// +/// # Example +/// +/// ``` +/// use rusp::usp_decoder::try_decode_msg; +/// use rusp::usp_encoder::try_encode_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!(try_encode_msg(&msg).unwrap(), bytes); +/// ``` +pub fn try_encode_msg(msg: &Msg) -> Result> { + msg.to_vec() } /// Implementation of some extension methods for `Record`s @@ -73,6 +78,30 @@ impl<'a> Record<'a> { /// assert_eq!(record.to_json().unwrap(), "{\"version\":\"1.3\",\"to_id\":\"doc::to\",\"from_id\":\"doc::from\",\"payload_security\":\"PLAINTEXT\",\"mac_signature\":[],\"sender_cert\":[],\"mqtt_connect\":{\"version\":\"V5\",\"subscribed_topic\":\"topic\"}}"); /// ``` pub fn to_json(&'a self) -> Result { + serde_json::to_string(self).context("Failed serializing USP Record to JSON") + } + + /// Render the `Record` into pretty printed JSON + /// + /// # Arguments + /// + /// * `self` - A USP `Record` structure + /// + /// # Example + /// + /// ``` + /// use rusp::usp_decoder::try_decode_record; + /// let record = + /// try_decode_record(&[ + /// 0x0a, 0x03, 0x31, 0x2e, 0x33, 0x12, 0x07, 0x64, + /// 0x6f, 0x63, 0x3a, 0x3a, 0x74, 0x6f, 0x1a, 0x09, + /// 0x64, 0x6f, 0x63, 0x3a, 0x3a, 0x66, 0x72, 0x6f, + /// 0x6d, 0x52, 0x09, 0x08, 0x01, 0x12, 0x05, 0x74, + /// 0x6f, 0x70, 0x69, 0x63, + /// ]).unwrap(); + /// assert_eq!(record.to_json_pretty().unwrap(), "{\n \"version\": \"1.3\",\n \"to_id\": \"doc::to\",\n \"from_id\": \"doc::from\",\n \"payload_security\": \"PLAINTEXT\",\n \"mac_signature\": [],\n \"sender_cert\": [],\n \"mqtt_connect\": {\n \"version\": \"V5\",\n \"subscribed_topic\": \"topic\"\n }\n}"); + /// ``` + pub fn to_json_pretty(&'a self) -> Result { serde_json::to_string_pretty(self).context("Failed serializing USP Record to JSON") } @@ -223,7 +252,7 @@ impl<'a> Record<'a> { /// Implementation of some extension methods for `Msg`s impl<'a> Msg<'a> { - /// Encode the Msg as "native" JSON format + /// Encode the Msg as JSON format /// /// # Arguments /// @@ -246,6 +275,32 @@ impl<'a> Msg<'a> { /// 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\"}}}}}"); /// ``` pub fn to_json(&'a self) -> Result { + serde_json::to_string(self).context("Failed serializing USP Msg to JSON") + } + + /// Encode the Msg in pretty printed JSON format + /// + /// # Arguments + /// + /// * `self` - A decoded USP Msg structure + /// + /// # Example + /// + /// ``` + /// use rusp::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}"); + /// ``` + pub fn to_json_pretty(&'a self) -> Result { serde_json::to_string_pretty(self).context("Failed serializing USP Msg to JSON") }