ibc_client_cw/utils/
codec.rs

1use core::fmt::Display;
2
3use ibc_core::host::types::error::DecodingError;
4use ibc_core::primitives::proto::Any;
5use prost::Message;
6
7/// AnyCodec is a convenient trait that provides a generic way to encode and
8/// decode domain types through the `Any` type.
9pub trait AnyCodec {
10    fn decode_any_vec<C>(data: Vec<u8>) -> Result<C, DecodingError>
11    where
12        C: TryFrom<Any>,
13        <C as TryFrom<Any>>::Error: Display,
14    {
15        let raw = Any::decode(&mut data.as_slice())?;
16
17        C::try_from(raw).map_err(DecodingError::invalid_raw_data)
18    }
19
20    fn encode_to_any_vec<C>(value: C) -> Vec<u8>
21    where
22        C: Into<Any>,
23    {
24        value.into().encode_to_vec()
25    }
26}
27
28impl<T> AnyCodec for T where T: TryFrom<Any> + Into<Any> {}