Skip to content

Commit

Permalink
feat: Add a named variant for rmp_serde_1_3
Browse files Browse the repository at this point in the history
And advise to use it if advanced serde features are in use.
  • Loading branch information
lu-zero authored and vincent-herlemont committed Nov 14, 2024
1 parent 0229d0b commit 87d0c7a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ There are known issues with some `serde` advanced features such as:

Or types implementing similar strategies such as [`serde_json::Value`][serde_json_value].

The `rmp-serde` serialization format can optionally support them serializing structs as maps, the `RmpSerdeNamed` struct is provided to support this use-case.

[serde_json_value]: https://docs.rs/serde_json/latest/serde_json/enum.Value.html

## Data model
Expand Down
50 changes: 48 additions & 2 deletions src/codec/rmp_serde_1_3.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0) ·
//! Enable the `rmp_serde_1_3` feature and
//! [`annotate your type`](crate::native_model) with
//! `native_model::rmp_serde_1_3::RmpSerde` to have `native_db` use this crate.
//! `native_model::rmp_serde_1_3::RmpSerde` or `native_model::rmp_serde_1_3::RmpSerdeNamed`
//! to have `native_db` use this crate.

/// Used to specify the
/// [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0)
/// crate for serialization & deserialization.
/// crate for serialization & deserialization, using arrays to serialize structs.
///
/// Do not use this if you plan to use serde features that skip serializing fields,
/// use [RmpSerdeNamed] instead.
///
/// # Basic usage
///
Expand Down Expand Up @@ -43,3 +47,45 @@ impl<T: for<'de> serde::Deserialize<'de>> crate::Decode<T> for RmpSerde {
rmp_serde_1_3::decode::from_slice(&data)
}
}

/// Used to specify the
/// [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0)
/// crate for serialization & deserialization, using maps to serialize structs.
///
/// # Basic usage
///
/// After enabling the `rmp_serde_1_3` feature in your `Cargo.toml`, use the
/// [`with`](crate::native_model) attribute on your type to instruct
/// `native_model` to use `RmpSerdeNamed` for serialization & deserialization.
///
/// Example usage:
///
/// ```rust
/// # use native_model::*;
/// #[derive(Clone, Default, serde::Deserialize, serde::Serialize)]
/// #[native_model(id = 1, version = 1, with = native_model::rmp_serde_1_3::RmpSerdeNamed)]
/// struct MyStruct {
/// #[serde(skip_serializing_if = "String::is_empty")]
/// my_string: String
/// }
/// ```

pub struct RmpSerdeNamed;

#[cfg(all(feature = "serde", feature = "rmp_serde_1_3"))]
impl<T: serde::Serialize> crate::Encode<T> for RmpSerdeNamed {
type Error = rmp_serde_1_3::encode::Error;
/// Serializes a type into bytes using the `rmp-serde` `1.3` crate.
fn encode(obj: &T) -> Result<Vec<u8>, Self::Error> {
rmp_serde_1_3::encode::to_vec_named(obj)
}
}

#[cfg(all(feature = "serde", feature = "rmp_serde_1_3"))]
impl<T: for<'de> serde::Deserialize<'de>> crate::Decode<T> for RmpSerdeNamed {
type Error = rmp_serde_1_3::decode::Error;
/// Deserializes a type from bytes using the `rmp-serde` `1.3` crate.
fn decode(data: Vec<u8>) -> Result<T, Self::Error> {
rmp_serde_1_3::decode::from_slice(&data)
}
}

0 comments on commit 87d0c7a

Please sign in to comment.