diff --git a/packages/std/src/msgpack.rs b/packages/std/src/msgpack.rs index b484697be..4b0200317 100644 --- a/packages/std/src/msgpack.rs +++ b/packages/std/src/msgpack.rs @@ -12,11 +12,51 @@ use crate::{StdError, StdResult}; /// Deserializes the given MessagePack bytes to a data structure. /// /// Errors if the input is not valid MessagePack or cannot be deserialized to the given type. +/// +/// ## Examples +/// +/// Encoding and decoding an enum using MessagePack. +/// +/// ``` +/// use cosmwasm_schema::cw_serde; +/// use cosmwasm_std::{to_msgpack_binary, from_msgpack}; +/// +/// #[cw_serde] +/// enum MyPacket { +/// Cowsay { +/// text: String, +/// }, +/// } +/// +/// let packet = MyPacket::Cowsay { text: "hi".to_string() }; +/// let encoded = to_msgpack_binary(&packet).unwrap(); +/// let decoded: MyPacket = from_msgpack(&encoded).unwrap(); +/// assert_eq!(decoded, packet); pub fn from_msgpack(value: impl AsRef<[u8]>) -> StdResult { rmp_serde::from_read(value.as_ref()).map_err(|e| StdError::parse_err(type_name::(), e)) } /// Serializes the given data structure as a MessagePack byte vector. +/// +/// ## Examples +/// +/// Encoding and decoding an enum using MessagePack. +/// +/// ``` +/// use cosmwasm_schema::cw_serde; +/// use cosmwasm_std::{to_msgpack_vec, from_msgpack}; +/// +/// #[cw_serde] +/// enum MyPacket { +/// Cowsay { +/// text: String, +/// }, +/// } +/// +/// let packet = MyPacket::Cowsay { text: "hi".to_string() }; +/// let encoded = to_msgpack_vec(&packet).unwrap(); +/// let decoded: MyPacket = from_msgpack(&encoded).unwrap(); +/// assert_eq!(decoded, packet); pub fn to_msgpack_vec(data: &T) -> StdResult> where T: Serialize + ?Sized, @@ -25,6 +65,27 @@ where } /// Serializes the given data structure as MessagePack bytes. +/// +/// ## Examples +/// +/// Encoding and decoding an enum using MessagePack. +/// +/// ``` +/// use cosmwasm_schema::cw_serde; +/// use cosmwasm_std::{to_msgpack_binary, from_msgpack}; +/// +/// #[cw_serde] +/// enum MyPacket { +/// Cowsay { +/// text: String, +/// }, +/// } +/// +/// let packet = MyPacket::Cowsay { text: "hi".to_string() }; +/// let encoded = to_msgpack_binary(&packet).unwrap(); +/// let decoded: MyPacket = from_msgpack(&encoded).unwrap(); +/// assert_eq!(decoded, packet); +/// ``` pub fn to_msgpack_binary(data: &T) -> StdResult where T: Serialize + ?Sized,