cw_storey/
encoding.rs

1use cosmwasm_std::StdError;
2use storey::encoding::{Cover, DecodableWithImpl, EncodableWithImpl, Encoding};
3
4/// An encoding that delegates to the [*MessagePack*] encoding provided by the [`cosmwasm_std`] crate.
5///
6/// This type implements the [`Encoding`] trait (see [`storey::encoding`]), which means it can
7/// be used with some of [`storey`]'s containers to encode and decode values.
8///
9/// You're unlikely to need to use this type directly for basic library usage. You might
10/// need it if you're trying to use third-party containers this crate does not provide.
11///
12/// [*MessagePack*]: https://msgpack.org/
13/// [`cosmwasm_std`]: https://docs.rs/cosmwasm-std
14pub struct CwEncoding;
15
16impl Encoding for CwEncoding {
17    type DecodeError = StdError;
18    type EncodeError = StdError;
19}
20
21impl<T> EncodableWithImpl<CwEncoding> for Cover<&T>
22where
23    T: serde::Serialize,
24{
25    fn encode_impl(self) -> Result<Vec<u8>, StdError> {
26        cosmwasm_std::to_msgpack_vec(self.0)
27    }
28}
29
30impl<T> DecodableWithImpl<CwEncoding> for Cover<T>
31where
32    T: serde::de::DeserializeOwned,
33{
34    fn decode_impl(data: &[u8]) -> Result<Self, StdError> {
35        cosmwasm_std::from_msgpack(data).map(Cover)
36    }
37}