Skip to content

Commit

Permalink
fix: inline msgpack encoding functions
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Jun 12, 2024
1 parent 8e0e4a8 commit 01656c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
5 changes: 1 addition & 4 deletions packages/cw-storey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ license = { workspace = true }

[dependencies]
cosmwasm-std = "2"
rmp-serde = "1.1"
serde = "1"

# TODO: temporary, remove after this is released:
# https://github.com/CosmWasm/cosmwasm/pull/2118
cosmwasm-std-new = { git = "https://github.com/CosmWasm/cosmwasm.git", rev = "553bdd6fc5cc9c74bebcba99a89c0c4334b8824d", package = "cosmwasm-std" }

storey = { workspace = true }
30 changes: 26 additions & 4 deletions packages/cw-storey/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cosmwasm_std::StdError;
use storey::encoding::{Cover, DecodableWithImpl, EncodableWithImpl, Encoding};

/// An encoding that delegates to the [*MessagePack*] encoding provided by the [`cosmwasm_std`] crate.
Expand All @@ -13,15 +14,15 @@ use storey::encoding::{Cover, DecodableWithImpl, EncodableWithImpl, Encoding};
pub struct CwEncoding;

impl Encoding for CwEncoding {
type DecodeError = cosmwasm_std_new::StdError;
type EncodeError = cosmwasm_std_new::StdError;
type DecodeError = StdError;
type EncodeError = StdError;
}

impl<T> EncodableWithImpl<CwEncoding> for Cover<&T>
where
T: serde::Serialize,
{
fn encode_impl(self) -> Result<Vec<u8>, cosmwasm_std_new::StdError> {
fn encode_impl(self) -> Result<Vec<u8>, StdError> {
cosmwasm_std_new::to_msgpack_vec(self.0)
}
}
Expand All @@ -30,7 +31,28 @@ impl<T> DecodableWithImpl<CwEncoding> for Cover<T>
where
T: serde::de::DeserializeOwned,
{
fn decode_impl(data: &[u8]) -> Result<Self, cosmwasm_std_new::StdError> {
fn decode_impl(data: &[u8]) -> Result<Self, StdError> {
cosmwasm_std_new::from_msgpack(data).map(Cover)
}
}

// TODO: remove this module once the following PR is released on crates.io:
// https://github.com/CosmWasm/cosmwasm/pull/2118
mod cosmwasm_std_new {
use core::any::type_name;

use cosmwasm_std::{StdError, StdResult};
use serde::de::DeserializeOwned;
use serde::Serialize;

pub(super) fn from_msgpack<T: DeserializeOwned>(value: impl AsRef<[u8]>) -> StdResult<T> {
rmp_serde::from_read(value.as_ref()).map_err(|e| StdError::parse_err(type_name::<T>(), e))
}

pub(super) fn to_msgpack_vec<T>(data: &T) -> StdResult<Vec<u8>>
where
T: Serialize + ?Sized,
{
rmp_serde::to_vec_named(data).map_err(|e| StdError::serialize_err(type_name::<T>(), e))
}
}

0 comments on commit 01656c2

Please sign in to comment.