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

Enhancement: Replace serde_cbor with minicbor #68

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ maintenance = { status = "actively-developed" }
bitmaps = { version = "3.1", default-features = false }
heapless = { version = "0.8", features = ["serde"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_cbor = { version = "0.11", default-features = false, optional = true }

minicbor = { version = "0.25", optional = true }
minicbor-serde = { version = "0.3.2", optional = true }


serde-json-core = { version = "0.6" }
shadow-derive = { path = "shadow_derive", version = "0.2.1" }
embedded-storage-async = "0.4"
Expand Down Expand Up @@ -69,12 +73,13 @@ hex = { version = "0.4.3", features = ["alloc"] }
[features]
default = ["ota_mqtt_data", "provision_cbor"]

provision_cbor = ["serde_cbor"]
provision_cbor = ["dep:minicbor", "dep:minicbor-serde"]

ota_mqtt_data = ["dep:minicbor", "dep:minicbor-serde"]

ota_mqtt_data = ["serde_cbor"]
ota_http_data = []

std = ["serde/std", "serde_cbor?/std"]
std = ["serde/std", "minicbor-serde?/std"]

defmt = [
"dep:defmt",
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ pub mod jobs;
pub mod ota;
pub mod provisioning;
pub mod shadows;

pub use serde_cbor;
2 changes: 1 addition & 1 deletion src/ota/data_interface/mqtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<'a, M: RawMutex> DataInterface for MqttClient<'a, M> {
/// Decode a cbor encoded fileblock received from streaming service
fn decode_file_block<'c>(&self, payload: &'c mut [u8]) -> Result<FileBlock<'c>, OtaError> {
Ok(
serde_cbor::de::from_mut_slice::<cbor::GetStreamResponse>(payload)
minicbor_serde::from_slice::<cbor::GetStreamResponse>(payload)
.map_err(|_| OtaError::Encoding)?
.into(),
)
Expand Down
7 changes: 4 additions & 3 deletions src/ota/encoding/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ pub fn to_slice<T>(value: &T, slice: &mut [u8]) -> Result<usize, ()>
where
T: serde::ser::Serialize,
{
let mut serializer = serde_cbor::ser::Serializer::new(serde_cbor::ser::SliceWrite::new(slice));
let mut serializer =
minicbor_serde::Serializer::new(minicbor::encode::write::Cursor::new(slice));
value.serialize(&mut serializer).map_err(|_| ())?;
Ok(serializer.into_inner().bytes_written())
Ok(serializer.into_encoder().writer().position())
}

impl<'a> From<GetStreamResponse<'a>> for FileBlock<'a> {
Expand Down Expand Up @@ -170,7 +171,7 @@ mod test {
0, 0, 0, 0, 0, 0, 255,
];

let response: GetStreamResponse = serde_cbor::de::from_mut_slice(payload).unwrap();
let response: GetStreamResponse = minicbor_serde::from_slice(payload).unwrap();

assert_eq!(
response,
Expand Down
4 changes: 2 additions & 2 deletions src/provisioning/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl From<serde_json_core::de::Error> for Error {
}
}

impl From<serde_cbor::Error> for Error {
fn from(_e: serde_cbor::Error) -> Self {
impl From<minicbor_serde::error::DecodeError> for Error {
fn from(_e: minicbor_serde::error::DecodeError) -> Self {
Self::DeserializeCbor
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/provisioning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,11 @@ impl FleetProvisioner {
Ok(match payload_format {
#[cfg(feature = "provision_cbor")]
PayloadFormat::Cbor => {
let mut serializer =
serde_cbor::ser::Serializer::new(serde_cbor::ser::SliceWrite::new(buf));
let mut serializer = minicbor_serde::Serializer::new(buf);
register_request
.serialize(&mut serializer)
.map_err(|_| EncodingError::BufferSize)?;
serializer.into_inner().bytes_written()
serializer.into_encoder().writer().len()
}
PayloadFormat::Json => serde_json_core::to_slice(&register_request, buf)
.map_err(|_| EncodingError::BufferSize)?,
Expand Down Expand Up @@ -318,13 +317,11 @@ impl FleetProvisioner {
Ok(match payload_format {
#[cfg(feature = "provision_cbor")]
PayloadFormat::Cbor => {
let mut serializer = serde_cbor::ser::Serializer::new(
serde_cbor::ser::SliceWrite::new(buf),
);
let mut serializer = minicbor_serde::Serializer::new(buf);
request
.serialize(&mut serializer)
.map_err(|_| EncodingError::BufferSize)?;
serializer.into_inner().bytes_written()
serializer.into_encoder().writer().len()
}
PayloadFormat::Json => serde_json_core::to_slice(&request, buf)
.map_err(|_| EncodingError::BufferSize)?,
Expand Down Expand Up @@ -398,7 +395,7 @@ impl FleetProvisioner {

Ok(match payload_format {
#[cfg(feature = "provision_cbor")]
PayloadFormat::Cbor => serde_cbor::de::from_mut_slice::<R>(message.payload_mut())?,
PayloadFormat::Cbor => minicbor_serde::from_slice::<R>(message.payload_mut())?,
PayloadFormat::Json => serde_json_core::from_slice::<R>(message.payload())?.0,
})
}
Expand All @@ -412,7 +409,7 @@ impl FleetProvisioner {
let response = match format {
#[cfg(feature = "provision_cbor")]
PayloadFormat::Cbor => {
serde_cbor::de::from_mut_slice::<ErrorResponse>(message.payload_mut())?
minicbor_serde::from_slice::<ErrorResponse>(message.payload_mut())?
}
PayloadFormat::Json => {
serde_json_core::from_slice::<ErrorResponse>(message.payload())?.0
Expand Down
15 changes: 6 additions & 9 deletions src/shadows/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ where
}

Ok(
serde_cbor::de::from_mut_slice::<S>(
&mut buf[U32_SIZE..len as usize + U32_SIZE],
)
.map_err(|_| Error::InvalidPayload)?,
minicbor_serde::from_slice::<S>(&mut buf[U32_SIZE..len as usize + U32_SIZE])
.map_err(|_| Error::InvalidPayload)?,
)
}
_ => Err(Error::InvalidPayload),
Expand All @@ -43,14 +41,13 @@ where

let buf = &mut [0u8; S::MAX_PAYLOAD_SIZE + U32_SIZE];

let mut serializer = serde_cbor::ser::Serializer::new(serde_cbor::ser::SliceWrite::new(
&mut buf[U32_SIZE..],
))
.packed_format();
let mut serializer = minicbor_serde::Serializer::new(&mut buf[U32_SIZE..]);

state
.serialize(&mut serializer)
.map_err(|_| Error::InvalidPayload)?;
let len = serializer.into_inner().bytes_written();

let len = serializer.into_encoder().writer().len();

if len > S::MAX_PAYLOAD_SIZE {
return Err(Error::Overflow);
Expand Down
Loading