-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
133 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
pub mod build; | ||
pub mod proto; | ||
mod proto_fmt; | ||
pub mod serde; | ||
mod std_conv; | ||
pub mod testonly; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//! Implementation of serde traits for structs implementing ProtoFmt. | ||
//! This serde implementation is compatible with protobuf json mapping, | ||
//! therefore it is suitable for version control. | ||
//! WARNING: Currently this serde implementation uses reflection, | ||
//! so it is not very efficient. | ||
use crate::ProtoFmt; | ||
use prost::Message as _; | ||
use prost_reflect::ReflectMessage; | ||
|
||
/// Implementation of serde::Serialize for arbitrary ReflectMessage. | ||
pub fn serialize_proto<T: ReflectMessage, S: serde::Serializer>( | ||
x: &T, | ||
s: S, | ||
) -> Result<S::Ok, S::Error> { | ||
let opts = prost_reflect::SerializeOptions::new(); | ||
x.transcode_to_dynamic().serialize_with_options(s, &opts) | ||
} | ||
|
||
/// Implementation of serde::Serialize for arbitrary ProtoFmt. | ||
pub fn serialize<T: ProtoFmt, S: serde::Serializer>(x: &T, s: S) -> Result<S::Ok, S::Error> { | ||
serialize_proto(&x.build(), s) | ||
} | ||
|
||
/// Implementation of serde::Deserialize for arbitrary ReflectMessage. | ||
pub fn deserialize_proto<'de, T: ReflectMessage + Default, D: serde::Deserializer<'de>>( | ||
d: D, | ||
) -> Result<T, D::Error> { | ||
let mut p = T::default(); | ||
let msg = prost_reflect::DynamicMessage::deserialize(p.descriptor(), d)?; | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters