Skip to content

Commit

Permalink
Introduce additional method that keeps the default behaviour the same
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Mar 28, 2024
1 parent 608b806 commit b0e57b2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions node/libs/protobuf/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ pub fn serialize<T: ProtoFmt, S: serde::Serializer>(x: &T, s: S) -> Result<S::Ok
serialize_proto(&x.build(), s)
}

/// Implementation of serde::Deserialize for arbitrary ReflectMessage.
/// Implementation of serde::Deserialize for arbitrary ReflectMessage denying unknown fields
pub fn deserialize_proto<'de, T: ReflectMessage + Default, D: serde::Deserializer<'de>>(
d: D,
) -> Result<T, D::Error> {
deserialize_proto_with_options(d, true)
}

/// Implementation of serde::Deserialize for arbitrary ReflectMessage with deny_unknown_fields option
pub fn deserialize_proto_with_options<'de, T: ReflectMessage + Default, D: serde::Deserializer<'de>>(
d: D,
deny_unknown_fields: bool
) -> Result<T, D::Error> {
let mut p = T::default();
let options = prost_reflect::DeserializeOptions::new().deny_unknown_fields(false);
let options = prost_reflect::DeserializeOptions::new().deny_unknown_fields(deny_unknown_fields);
let msg = prost_reflect::DynamicMessage::deserialize_with_options(p.descriptor(), d, &options)?;
p.merge(msg.encode_to_vec().as_slice()).unwrap();
Ok(p)
}


/// Implementation of serde::Deserialize for arbitrary ProtoFmt.
pub fn deserialize<'de, T: ProtoFmt, D: serde::Deserializer<'de>>(d: D) -> Result<T, D::Error> {
T::read(&deserialize_proto(d)?).map_err(serde::de::Error::custom)
Expand Down

0 comments on commit b0e57b2

Please sign in to comment.