Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples to from_msgpack, to_msgpack_vec and to_msgpack_binary #2291

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/std/src/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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))
}

/// 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<T>(data: &T) -> StdResult<Vec<u8>>
where
T: Serialize + ?Sized,
Expand All @@ -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<T>(data: &T) -> StdResult<Binary>
where
T: Serialize + ?Sized,
Expand Down
Loading