Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cosmos-sdk-proto: add serde derive macros #471

Merged
merged 24 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 22 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
32 changes: 22 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cosmos-sdk-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ tendermint-proto = "0.39.1"

# Optional dependencies
tonic = { version = "0.12", optional = true, default-features = false, features = ["codegen", "prost"] }
serde = { version = "1.0.203", optional = true, default-features = false, features = ["alloc"] }
pbjson = { package = "informalsystems-pbjson", optional = true, version = "0.7" }

[features]
default = ["grpc-transport"]
std = ["prost/std", "tendermint-proto/std"]
grpc = ["std", "tonic"]
grpc-transport = ["grpc", "tonic/transport"]
cosmwasm = []
serialization = ["serde", "tendermint-proto/std", "pbjson"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of serialization going on in this crate, both protobuf and now JSON.

How about:

Suggested change
serialization = ["serde", "tendermint-proto/std", "pbjson"]
serde = ["dep:serde", "tendermint-proto/std", "pbjson"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and adjusted the code gen


[package.metadata.docs.rs]
all-features = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Module {}
include!("cosmos.app.module.v1alpha1.serde.rs");
// @@protoc_insertion_point(module)
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @generated
#[cfg(feature = "serialization")]
impl serde::Serialize for Module {
#[allow(deprecated)]
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
let len = 0;
let struct_ser = serializer.serialize_struct("cosmos.app.module.v1alpha1.Module", len)?;
struct_ser.end()
}
}
#[cfg(feature = "serialization")]
impl<'de> serde::Deserialize<'de> for Module {
#[allow(deprecated)]
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
const FIELDS: &[&str] = &[];

#[allow(clippy::enum_variant_names)]
enum GeneratedField {}
#[cfg(feature = "serialization")]
impl<'de> serde::Deserialize<'de> for GeneratedField {
fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>
where
D: serde::Deserializer<'de>,
{
struct GeneratedVisitor;

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

fn expecting(
&self,
formatter: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
write!(formatter, "expected one of: {:?}", &FIELDS)
}

#[allow(unused_variables)]
fn visit_str<E>(self, value: &str) -> std::result::Result<GeneratedField, E>
where
E: serde::de::Error,
{
Err(serde::de::Error::unknown_field(value, FIELDS))
}
}
deserializer.deserialize_identifier(GeneratedVisitor)
}
}
struct GeneratedVisitor;
impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {
type Value = Module;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("struct cosmos.app.module.v1alpha1.Module")
}

fn visit_map<V>(self, mut map_: V) -> std::result::Result<Module, V::Error>
where
V: serde::de::MapAccess<'de>,
{
while map_.next_key::<GeneratedField>()?.is_some() {
let _ = map_.next_value::<serde::de::IgnoredAny>()?;
}
Ok(Module {})
}
}
deserializer.deserialize_struct(
"cosmos.app.module.v1alpha1.Module",
FIELDS,
GeneratedVisitor,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ pub struct QueryConfigResponse {
#[prost(message, optional, tag = "1")]
pub config: ::core::option::Option<Config>,
}
include!("cosmos.app.v1alpha1.serde.rs");
include!("cosmos.app.v1alpha1.tonic.rs");
// @@protoc_insertion_point(module)
Loading