Skip to content

Commit

Permalink
Add serde support for Value
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Nov 20, 2021
1 parent 99d09c5 commit 2ad717e
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pbjson-types/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ fn main() -> Result<()> {
let descriptor_set = std::fs::read(descriptor_path)?;
pbjson_build::Builder::new()
.register_descriptors(&descriptor_set)?
.exclude([".google.protobuf.Duration", ".google.protobuf.Timestamp"])
.exclude([
".google.protobuf.Duration",
".google.protobuf.Timestamp",
".google.protobuf.Value",
])
.build(&[".google"])?;

Ok(())
Expand Down
4 changes: 4 additions & 0 deletions pbjson-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
clippy::redundant_closure,
clippy::redundant_field_names,
clippy::clone_on_ref_ptr,
clippy::enum_variant_names,
clippy::use_self
)]
mod pb {
Expand All @@ -36,6 +37,9 @@ mod pb {
}

mod duration;
mod list_value;
mod r#struct;
mod timestamp;
mod value;

pub use pb::google::protobuf::*;
27 changes: 27 additions & 0 deletions pbjson-types/src/list_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
impl From<Vec<crate::Value>> for crate::ListValue {
fn from(values: Vec<crate::Value>) -> Self {
Self { values }
}
}

impl FromIterator<crate::value::Kind> for crate::ListValue {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = crate::value::Kind>,
{
Self {
values: iter.into_iter().map(Into::into).collect(),
}
}
}

impl FromIterator<crate::Value> for crate::ListValue {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = crate::Value>,
{
Self {
values: iter.into_iter().collect(),
}
}
}
48 changes: 48 additions & 0 deletions pbjson-types/src/struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
impl From<std::collections::HashMap<String, crate::Value>> for crate::Struct {
fn from(fields: std::collections::HashMap<String, crate::Value>) -> Self {
Self { fields }
}
}

impl FromIterator<(String, crate::Value)> for crate::Struct {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (String, crate::Value)>,
{
Self {
fields: iter.into_iter().collect(),
}
}
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let map = std::collections::HashMap::from([
(String::from("bool"), true.into()),
(String::from("unit"), crate::value::Kind::NullValue(0)),
(String::from("number"), 5.0.into()),
(String::from("string"), "string".into()),
(String::from("list"), vec![1.0.into(), 2.0.into()].into()),
(
String::from("map"),
std::collections::HashMap::from([(String::from("key"), "value".into())]).into(),
),
]);

assert_eq!(
serde_json::to_value(map).unwrap(),
serde_json::json!({
"bool": true,
"unit": null,
"number": 5.0,
"string": "string",
"list": [1.0, 2.0],
"map": {
"key": "value",
}
})
);
}
}
Loading

0 comments on commit 2ad717e

Please sign in to comment.