diff --git a/packages/cw-storey/Cargo.toml b/packages/cw-storey/Cargo.toml index 21cb73b..afeaf88 100644 --- a/packages/cw-storey/Cargo.toml +++ b/packages/cw-storey/Cargo.toml @@ -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 } diff --git a/packages/cw-storey/src/encoding.rs b/packages/cw-storey/src/encoding.rs index 088e745..a15dc42 100644 --- a/packages/cw-storey/src/encoding.rs +++ b/packages/cw-storey/src/encoding.rs @@ -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. @@ -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 EncodableWithImpl for Cover<&T> where T: serde::Serialize, { - fn encode_impl(self) -> Result, cosmwasm_std_new::StdError> { + fn encode_impl(self) -> Result, StdError> { cosmwasm_std_new::to_msgpack_vec(self.0) } } @@ -30,7 +31,28 @@ impl DecodableWithImpl for Cover where T: serde::de::DeserializeOwned, { - fn decode_impl(data: &[u8]) -> Result { + fn decode_impl(data: &[u8]) -> Result { 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(value: impl AsRef<[u8]>) -> StdResult { + rmp_serde::from_read(value.as_ref()).map_err(|e| StdError::parse_err(type_name::(), e)) + } + + pub(super) fn to_msgpack_vec(data: &T) -> StdResult> + where + T: Serialize + ?Sized, + { + rmp_serde::to_vec_named(data).map_err(|e| StdError::serialize_err(type_name::(), e)) + } +}