Skip to content

Commit

Permalink
sdk/rust: Add serde_wormhole::RawMessage
Browse files Browse the repository at this point in the history
Add a RawMessage type that can be used to defer parsing parts of a
payload, similar to the `json.RawMessage` from Go.  The implementation
is inspired by `serde_json::RawValue`, which does a similar thing.

When serializing, RawMessage will serialize to a base64-encoded string
if it detects that the data format is human readable (like JSON).
Otherwise it will simply forward the raw bytes to the serializer.

RawMessage has both borrowed and boxed versions.  The borrowed version
is the most efficient as it enables zero-copy handling of the input data
but also requires that the input data already contains raw bytes and is
not suitable when dealing with human-readable formats like JSON.

The boxed version is more flexible as it supports byte slices, base64-
encoded strings, and byte sequences but is slightly less efficient as it
requires copying or decoding the input data.
  • Loading branch information
jynnantonix committed Jan 18, 2023
1 parent 8f08d68 commit 3c6702b
Show file tree
Hide file tree
Showing 7 changed files with 688 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cosmwasm/Cargo.lock

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

8 changes: 8 additions & 0 deletions sdk/rust/Cargo.lock

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

4 changes: 3 additions & 1 deletion sdk/rust/serde_wormhole/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ edition = "2021"
description = "Serde data format for VAA payloads"

[dependencies]
base64 = "0.13"
itoa = "1.0.1"
serde = { version = "1.0.103", default-features = false }
serde_bytes = "0.11.5"
thiserror = "1.0"

[dev-dependencies]
serde = { version = "1.0.103", default-features = false, features = ["alloc", "derive"] }
serde_bytes = "0.11.5"
serde_json = "1"
serde_repr = "0.1.7"
18 changes: 13 additions & 5 deletions sdk/rust/serde_wormhole/src/de.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::{convert::TryFrom, mem::size_of};
use std::{
convert::TryFrom,
mem::{self, size_of},
};

use serde::de::{
self, DeserializeSeed, EnumAccess, Error as DeError, IntoDeserializer, MapAccess, SeqAccess,
VariantAccess, Visitor,
self, value::BorrowedBytesDeserializer, DeserializeSeed, EnumAccess, Error as DeError,
IntoDeserializer, MapAccess, SeqAccess, VariantAccess, Visitor,
};

use crate::error::Error;
Expand Down Expand Up @@ -250,13 +253,18 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
#[inline]
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_newtype_struct(self)
if name == crate::raw::TOKEN {
let rem = mem::take(&mut self.input);
visitor.visit_newtype_struct(BorrowedBytesDeserializer::new(rem))
} else {
visitor.visit_newtype_struct(self)
}
}

fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
Expand Down
2 changes: 2 additions & 0 deletions sdk/rust/serde_wormhole/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};

mod de;
mod error;
mod raw;
mod ser;

pub use error::Error;
pub use raw::{to_raw_message, RawMessage};

/// Deserialize an instance of type `T` from the provided reader.
pub fn from_reader<R: Read, T: DeserializeOwned>(mut r: R) -> Result<T, Error> {
Expand Down
Loading

0 comments on commit 3c6702b

Please sign in to comment.