Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust: ability to convert to common serde map structure #1346

Merged
merged 10 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions rust/sbp/src/json/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::convert::TryFrom;

use bytes::BytesMut;

use crate::Sbp;

use super::{JsonError, JsonOutput};

/// A JSON map holding all the fields and metadata of the
/// SBP message. This is format used by the output of the
/// `sbp2json` tool, for example:
///
/// {
/// "crc": 49084,
/// "length": 52,
/// "msg_type": 1025,
/// "msg_name": "MSG_LOG",
/// "payload": "BHNicCBuZXcgY2xpZW50IGNvbm5lY3Rpb24gOjpmZmZmOjE5Mi4xNjguMC4xMDo2MTQ1MA==",
/// "preamble": 85,
/// "sender": 4096,
/// "level": 4,
/// "text": "sbp new client connection ::ffff:192.168.0.10:61450"
/// }
pub type JsonMap = serde_json::Map<String, serde_json::Value>;

impl TryFrom<crate::Sbp> for JsonMap {
type Error = JsonError;

fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
let mut frame = BytesMut::with_capacity(crate::BUFLEN);
let mut payload = String::with_capacity(crate::BUFLEN);
let output = JsonOutput {
common: super::ser::get_common_fields(&mut payload, &mut frame, &msg)?,
msg: &msg,
};
let output = serde_json::to_value(output)?;
let output: serde_json::Map<String, serde_json::Value> = serde_json::from_value(output)?;
Ok(output)
}
}
3 changes: 3 additions & 0 deletions rust/sbp/src/json/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Encode/decode SBP messages as JSON.

mod convert;
mod de;
mod ser;

Expand All @@ -17,6 +18,8 @@ pub use de::{iter_json2json_messages, iter_messages, iter_messages_from_fields};

pub use ser::{to_vec, to_writer, Json2JsonEncoder, JsonEncoder};

pub use convert::JsonMap;

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum JsonInput {
Expand Down
9 changes: 5 additions & 4 deletions rust/sbp/src/json/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use dencode::{Encoder, FramedWrite, IterSinkExt};
use serde::Serialize;
use serde_json::{ser::Formatter, Serializer};

use super::{JsonError, JsonOutput};

use crate::{
json::{CommonJson, HaskellishFloatFormatter, Json2JsonInput, Json2JsonOutput},
json::{
CommonJson, HaskellishFloatFormatter, Json2JsonInput, Json2JsonOutput, JsonError,
JsonOutput,
},
messages::Sbp,
SbpMessage, BUFLEN, CRC_LEN, HEADER_LEN, PREAMBLE,
};
Expand Down Expand Up @@ -212,7 +213,7 @@ impl<F: Formatter + Clone> Encoder<Json2JsonInput> for Json2JsonEncoderInner<F>
}
}

fn get_common_fields<'a, M: SbpMessage>(
pub(super) fn get_common_fields<'a, M: SbpMessage>(
payload_buf: &'a mut String,
frame_buf: &'a mut BytesMut,
msg: &M,
Expand Down
8 changes: 4 additions & 4 deletions test_data/benchmark_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

# How much faster Rust should be than other implementations
RATIOS_SBP2JSON = {
"haskell": 2.12,
"python": 17,
"kaitai_python": 3.7,
"kaitai_perl": 18,
"haskell" : 3.01,
"python" : 23.38,
"kaitai_python" : 5.00,
"kaitai_perl" : 18,
}

RATIOS_JSON2SBP = {
Expand Down
Loading