Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Pbjson

Pbjson is a set of crates to automatically generate [serde](https://serde.rs/) [Serialize](https://docs.rs/serde/1.0.130/serde/trait.Serialize.html) and [Deserialize](https://docs.rs/serde/1.0.130/serde/trait.Deserialize.html) implementations for auto-generated prost types.
Pbjson is a set of crates to automatically generate [serde](https://serde.rs/) [Serialize](https://docs.rs/serde_core/1.0.228/serde_core/trait.Serialize.html) and [Deserialize](https://docs.rs/serde_core/1.0.228/serde_core/trait.Deserialize.html) implementations for auto-generated prost types.

See [pbjson-build](https://docs.rs/pbjson-build) for usage instructions
8 changes: 4 additions & 4 deletions pbjson-build/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ fn write_fields_array<'a, W: Write, I: Iterator<Item = &'a str>>(
fn write_serialize_start<W: Write>(indent: usize, rust_type: &str, writer: &mut W) -> Result<()> {
writeln!(
writer,
r#"{indent}impl serde::Serialize for {rust_type} {{
r#"{indent}impl serde_core::Serialize for {rust_type} {{
{indent} #[allow(deprecated)]
{indent} fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
{indent} where
{indent} S: serde::Serializer,
{indent} S: serde_core::Serializer,
{indent} {{"#,
indent = Indent(indent),
rust_type = rust_type
Expand All @@ -60,11 +60,11 @@ fn write_serialize_end<W: Write>(indent: usize, writer: &mut W) -> Result<()> {
fn write_deserialize_start<W: Write>(indent: usize, rust_type: &str, writer: &mut W) -> Result<()> {
writeln!(
writer,
r#"{indent}impl<'de> serde::Deserialize<'de> for {rust_type} {{
r#"{indent}impl<'de> serde_core::Deserialize<'de> for {rust_type} {{
{indent} #[allow(deprecated)]
{indent} fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
{indent} where
{indent} D: serde::Deserializer<'de>,
{indent} D: serde_core::Deserializer<'de>,
{indent} {{"#,
indent = Indent(indent),
rust_type = rust_type
Expand Down
14 changes: 7 additions & 7 deletions pbjson-build/src/generator/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn write_visitor<W: Write>(
writer,
r#"{indent}struct GeneratedVisitor;

{indent}impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {{
{indent}impl<'de> serde_core::de::Visitor<'de> for GeneratedVisitor {{
{indent} type Value = {rust_type};

{indent} fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
Expand All @@ -107,31 +107,31 @@ fn write_visitor<W: Write>(

{indent} fn visit_i64<E>(self, v: i64) -> std::result::Result<Self::Value, E>
{indent} where
{indent} E: serde::de::Error,
{indent} E: serde_core::de::Error,
{indent} {{
{indent} i32::try_from(v)
{indent} .ok()
{indent} .and_then(|x| x.try_into().ok())
{indent} .ok_or_else(|| {{
{indent} serde::de::Error::invalid_value(serde::de::Unexpected::Signed(v), &self)
{indent} serde_core::de::Error::invalid_value(serde_core::de::Unexpected::Signed(v), &self)
{indent} }})
{indent} }}

{indent} fn visit_u64<E>(self, v: u64) -> std::result::Result<Self::Value, E>
{indent} where
{indent} E: serde::de::Error,
{indent} E: serde_core::de::Error,
{indent} {{
{indent} i32::try_from(v)
{indent} .ok()
{indent} .and_then(|x| x.try_into().ok())
{indent} .ok_or_else(|| {{
{indent} serde::de::Error::invalid_value(serde::de::Unexpected::Unsigned(v), &self)
{indent} serde_core::de::Error::invalid_value(serde_core::de::Unexpected::Unsigned(v), &self)
{indent} }})
{indent} }}

{indent} fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>
{indent} where
{indent} E: serde::de::Error,
{indent} E: serde_core::de::Error,
{indent} {{"#,
indent = Indent(indent),
rust_type = rust_type,
Expand All @@ -151,7 +151,7 @@ fn write_visitor<W: Write>(

writeln!(
writer,
"{indent}_ => Err(serde::de::Error::unknown_variant(value, FIELDS)),",
"{indent}_ => Err(serde_core::de::Error::unknown_variant(value, FIELDS)),",
indent = Indent(indent + 3)
)?;
writeln!(writer, "{}}}", Indent(indent + 2))?;
Expand Down
32 changes: 18 additions & 14 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ fn write_struct_serialize_start<W: Write>(
writer: &mut W,
emit_fields: bool,
) -> Result<()> {
writeln!(writer, "{}use serde::ser::SerializeStruct;", Indent(indent))?;
writeln!(
writer,
"{}use serde_core::ser::SerializeStruct;",
Indent(indent)
)?;

let required_len = message
.fields
Expand Down Expand Up @@ -211,7 +215,7 @@ fn write_decode_variant<W: Write>(
writeln!(writer, "{}::try_from({})", resolver.rust_type(path), value)?;
write!(
writer,
"{}.map_err(|_| serde::ser::Error::custom(format!(\"Invalid variant {{}}\", {})))",
"{}.map_err(|_| serde_core::ser::Error::custom(format!(\"Invalid variant {{}}\", {})))",
Indent(indent),
value
)
Expand Down Expand Up @@ -521,7 +525,7 @@ fn write_deserialize_message<W: Write>(

writeln!(
writer,
r#"{indent}impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {{
r#"{indent}impl<'de> serde_core::de::Visitor<'de> for GeneratedVisitor {{
{indent} type Value = {rust_type};

{indent} fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
Expand All @@ -530,7 +534,7 @@ fn write_deserialize_message<W: Write>(

{indent} fn visit_map<V>(self, mut map_: V) -> std::result::Result<{rust_type}, V::Error>
{indent} where
{indent} V: serde::de::MapAccess<'de>,
{indent} V: serde_core::de::MapAccess<'de>,
{indent} {{"#,
indent = Indent(indent),
name = message.path,
Expand Down Expand Up @@ -593,7 +597,7 @@ fn write_deserialize_message<W: Write>(
)?;
writeln!(
writer,
"{}let _ = map_.next_value::<serde::de::IgnoredAny>()?;",
"{}let _ = map_.next_value::<serde_core::de::IgnoredAny>()?;",
Indent(indent + 5),
)?;
writeln!(writer, "{}}}", Indent(indent + 4))?;
Expand All @@ -609,7 +613,7 @@ fn write_deserialize_message<W: Write>(
)?;
writeln!(
writer,
"{}let _ = map_.next_value::<serde::de::IgnoredAny>()?;",
"{}let _ = map_.next_value::<serde_core::de::IgnoredAny>()?;",
Indent(indent + 3)
)?;
writeln!(writer, "{}}}", Indent(indent + 2))?;
Expand All @@ -621,7 +625,7 @@ fn write_deserialize_message<W: Write>(
FieldModifier::Required => {
writeln!(
writer,
"{indent}{field}: {field}__.ok_or_else(|| serde::de::Error::missing_field(\"{json_name}\"))?,",
"{indent}{field}: {field}__.ok_or_else(|| serde_core::de::Error::missing_field(\"{json_name}\"))?,",
indent = Indent(indent + 3),
field = field.rust_field_name(),
json_name = field.json_name()
Expand Down Expand Up @@ -699,14 +703,14 @@ fn write_deserialize_field_name<W: Write>(

writeln!(
writer,
r#"{indent}impl<'de> serde::Deserialize<'de> for GeneratedField {{
r#"{indent}impl<'de> serde_core::Deserialize<'de> for GeneratedField {{
{indent} fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>
{indent} where
{indent} D: serde::Deserializer<'de>,
{indent} D: serde_core::Deserializer<'de>,
{indent} {{
{indent} struct GeneratedVisitor;

{indent} impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {{
{indent} impl<'de> serde_core::de::Visitor<'de> for GeneratedVisitor {{
{indent} type Value = GeneratedField;

{indent} fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {{
Expand All @@ -716,7 +720,7 @@ fn write_deserialize_field_name<W: Write>(
{indent} #[allow(unused_variables)]
{indent} fn visit_str<E>(self, value: &str) -> std::result::Result<GeneratedField, E>
{indent} where
{indent} E: serde::de::Error,
{indent} E: serde_core::de::Error,
{indent} {{"#,
indent = Indent(indent)
)?;
Expand Down Expand Up @@ -752,7 +756,7 @@ fn write_deserialize_field_name<W: Write>(
} else {
writeln!(
writer,
"{}_ => Err(serde::de::Error::unknown_field(value, FIELDS)),",
"{}_ => Err(serde_core::de::Error::unknown_field(value, FIELDS)),",
Indent(indent + 5)
)?;
}
Expand All @@ -766,7 +770,7 @@ fn write_deserialize_field_name<W: Write>(
} else {
writeln!(
writer,
"{}Err(serde::de::Error::unknown_field(value, FIELDS))",
"{}Err(serde_core::de::Error::unknown_field(value, FIELDS))",
Indent(indent + 5)
)?;
}
Expand Down Expand Up @@ -840,7 +844,7 @@ fn write_deserialize_field<W: Write>(
// Note: this will report duplicate field if multiple value are specified for a one of
writeln!(
writer,
"{}return Err(serde::de::Error::duplicate_field(\"{}\"));",
"{}return Err(serde_core::de::Error::duplicate_field(\"{}\"));",
Indent(indent + 2),
json_name
)?;
Expand Down
8 changes: 4 additions & 4 deletions pbjson-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! `pbjson-build` consumes the descriptor output of [`prost-build`][1] and generates
//! [`serde::Serialize`][2] and [`serde::Deserialize`][3] implementations
//! [`serde_core::Serialize`][2] and [`serde_core::Deserialize`][3] implementations
//! that are compliant with the [protobuf JSON mapping][4]
//!
//! # Usage
Expand Down Expand Up @@ -63,11 +63,11 @@
//! ```
//!
//! The module will now contain the generated prost structs for your protobuf definition
//! along with compliant implementations of [serde::Serialize][2] and [serde::Deserialize][3]
//! along with compliant implementations of [serde_core::Serialize][2] and [serde_core::Deserialize][3]
//!
//! [1]: https://docs.rs/prost-build
//! [2]: https://docs.rs/serde/1.0.130/serde/trait.Serialize.html
//! [3]: https://docs.rs/serde/1.0.130/serde/trait.Deserialize.html
//! [2]: https://docs.rs/serde_core/1.0.228/serde_core/trait.Serialize.html
//! [3]: https://docs.rs/serde_core/1.0.228/serde_core/trait.Deserialize.html
//! [4]: https://developers.google.com/protocol-buffers/docs/proto3#json

#![deny(rustdoc::broken_intra_doc_links, rustdoc::bare_urls, rust_2018_idioms)]
Expand Down
1 change: 1 addition & 0 deletions pbjson-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ prost = "0.14"
pbjson = { path = "../pbjson" }
pbjson-types = { path = "../pbjson-types" }
serde = { version = "1.0", features = ["derive"] }
serde_core = { version = "1.0.200" }

[features]
ignore-unknown-fields = []
Expand Down
2 changes: 1 addition & 1 deletion pbjson-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bytes = "1.0"
chrono = { version = "0.4", default-features = false, features = ["alloc"] }
pbjson = { path = "../pbjson", version = "0.8" }
prost = "0.14"
serde = { version = "1.0", features = ["derive"] }
serde_core = { version = "1.0.200" }

[dev-dependencies]
serde_json = "1.0"
Expand Down
Binary file modified pbjson-types/descriptors.bin
Binary file not shown.
26 changes: 14 additions & 12 deletions pbjson-types/src/duration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Duration;
use serde::de::Visitor;
use serde::Serialize;
use serde_core::de::Visitor;
use serde_core::Serialize;

impl TryFrom<Duration> for std::time::Duration {
type Error = std::num::TryFromIntError;
Expand All @@ -25,10 +25,12 @@ impl From<std::time::Duration> for Duration {
impl Serialize for Duration {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
S: serde_core::Serializer,
{
if self.seconds != 0 && self.nanos != 0 && (self.nanos < 0) != (self.seconds < 0) {
return Err(serde::ser::Error::custom("Duration has inconsistent signs"));
return Err(serde_core::ser::Error::custom(
"Duration has inconsistent signs",
));
}

let mut s = if self.seconds == 0 {
Expand Down Expand Up @@ -67,11 +69,11 @@ impl<'de> Visitor<'de> for DurationVisitor {

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
E: serde_core::de::Error,
{
let s = s
.strip_suffix('s')
.ok_or_else(|| serde::de::Error::custom("missing 's' suffix"))?;
.ok_or_else(|| serde_core::de::Error::custom("missing 's' suffix"))?;

let (negative, s) = match s.strip_prefix('-') {
Some(s) => (true, s),
Expand All @@ -82,19 +84,19 @@ impl<'de> Visitor<'de> for DurationVisitor {
Some((seconds_str, decimal_str)) => {
let exp = 9_u32
.checked_sub(decimal_str.len() as u32)
.ok_or_else(|| serde::de::Error::custom("too many decimal places"))?;
.ok_or_else(|| serde_core::de::Error::custom("too many decimal places"))?;

let pow = 10_u32.pow(exp);
let seconds = seconds_str.parse().map_err(serde::de::Error::custom)?;
let decimal: u32 = decimal_str.parse().map_err(serde::de::Error::custom)?;
let seconds = seconds_str.parse().map_err(serde_core::de::Error::custom)?;
let decimal: u32 = decimal_str.parse().map_err(serde_core::de::Error::custom)?;

Duration {
seconds,
nanos: (decimal * pow) as i32,
}
}
None => Duration {
seconds: s.parse().map_err(serde::de::Error::custom)?,
seconds: s.parse().map_err(serde_core::de::Error::custom)?,
nanos: 0,
},
};
Expand All @@ -109,10 +111,10 @@ impl<'de> Visitor<'de> for DurationVisitor {
}
}

impl<'de> serde::Deserialize<'de> for Duration {
impl<'de> serde_core::Deserialize<'de> for Duration {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
D: serde_core::Deserializer<'de>,
{
deserializer.deserialize_str(DurationVisitor)
}
Expand Down
6 changes: 3 additions & 3 deletions pbjson-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! `pbjson-types` provides the `google.protobuf` types, commonly known as well-known-types,
//! with [`serde::Serialize`][1] and [`serde::Deserialize`][2] implementations
//! with [`serde_core::Serialize`][1] and [`serde_core::Deserialize`][2] implementations
//! that are compliant with the [protobuf JSON mapping][3]
//!
//! __Note: Coverage of all types is currently incomplete,
//! some may have non-compliant implementations__
//!
//! [1]: https://docs.rs/serde/1.0.130/serde/trait.Serialize.html
//! [2]: https://docs.rs/serde/1.0.130/serde/trait.Deserialize.html
//! [1]: https://docs.rs/serde_core/1.0.228/serde_core/trait.Serialize.html
//! [2]: https://docs.rs/serde_core/1.0.228/serde_core/trait.Deserialize.html
//! [3]: https://developers.google.com/protocol-buffers/docs/proto3#json

#![deny(rustdoc::broken_intra_doc_links, rustdoc::bare_urls, rust_2018_idioms)]
Expand Down
12 changes: 6 additions & 6 deletions pbjson-types/src/list_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ impl FromIterator<crate::Value> for ListValue {
}
}

impl serde::Serialize for ListValue {
impl serde_core::Serialize for ListValue {
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
S: serde_core::Serializer,
{
self.values.serialize(ser)
}
}

impl<'de> serde::Deserialize<'de> for ListValue {
impl<'de> serde_core::Deserialize<'de> for ListValue {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
D: serde_core::Deserializer<'de>,
{
deserializer.deserialize_seq(ListValueVisitor)
}
}

struct ListValueVisitor;

impl<'de> serde::de::Visitor<'de> for ListValueVisitor {
impl<'de> serde_core::de::Visitor<'de> for ListValueVisitor {
type Value = ListValue;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -65,7 +65,7 @@ impl<'de> serde::de::Visitor<'de> for ListValueVisitor {

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
A: serde_core::de::SeqAccess<'de>,
{
let mut list = Vec::new();

Expand Down
Loading