Skip to content

Commit

Permalink
cosmos-sdk-proto: add serde derive macros (#471)
Browse files Browse the repository at this point in the history
Uses Informal's fork of `pbjson` to generate `serde` impls

Co-authored-by: Tony Arcieri (iqlusion) <[email protected]>
  • Loading branch information
ash-burnt and tony-iqlusion authored Aug 13, 2024
1 parent 6755fa0 commit 7e55c1d
Show file tree
Hide file tree
Showing 102 changed files with 83,445 additions and 10 deletions.
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 = []
serde = ["dep:serde", "tendermint-proto/std", "pbjson"]

[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 = "serde")]
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 = "serde")]
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 = "serde")]
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

0 comments on commit 7e55c1d

Please sign in to comment.