Skip to content

Commit

Permalink
postcard-dyn: reserialize from arbitrary Deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
max-heller committed Dec 16, 2024
1 parent 6d2cb05 commit 374cc4f
Show file tree
Hide file tree
Showing 18 changed files with 2,071 additions and 455 deletions.
6 changes: 6 additions & 0 deletions source/postcard-dyn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ documentation = "https://docs.rs/postcard-dyn/"


[dependencies]
hashbrown = { version = "0.15.2", default-features = false, features = ["default-hasher"] }
serde = { version = "1.0.202", features = ["derive"] }
serde-content = "0.1.0"
serde_json = "1.0.117"

[dependencies.postcard]
Expand All @@ -31,3 +33,7 @@ path = "../postcard"
version = "0.2"
features = ["use-std", "derive"]
path = "../postcard-schema"

[dev-dependencies.serde_json]
version = "1.0"
features = ["preserve_order"]
2 changes: 1 addition & 1 deletion source/postcard-dyn/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::Error;
pub fn from_slice_dyn(
schema: &OwnedNamedType,
data: &[u8],
) -> Result<Value, Error<serde_json::Error>> {
) -> Result<Value, Error<postcard::Error, serde_json::Error>> {
// Matches current value type (`serde_json::Value`)'s representation
crate::reserialize::lossy::reserialize_with_structs_and_enums_as_maps(
schema,
Expand Down
8 changes: 4 additions & 4 deletions source/postcard-dyn/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use core::fmt::{self, Display};

/// Errors encountered by `postcard-dyn`
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error<SerializeError> {
Deserialize(postcard::Error),
pub enum Error<DeserializeError, SerializeError> {
Deserialize(DeserializeError),
Serialize(SerializeError),
}

impl<SerializeError: Display> Display for Error<SerializeError> {
impl<D: Display, S: Display> Display for Error<D, S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Deserialize(err) => Display::fmt(err, f),
Expand All @@ -16,7 +16,7 @@ impl<SerializeError: Display> Display for Error<SerializeError> {
}
}

impl<SerializeError: core::error::Error> core::error::Error for Error<SerializeError> {
impl<D: core::error::Error, S: core::error::Error> core::error::Error for Error<D, S> {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Deserialize(err) => err.source(),
Expand Down
Loading

0 comments on commit 374cc4f

Please sign in to comment.