ibc_client_cw/utils/
codec.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use core::fmt::Display;

use ibc_core::host::types::error::DecodingError;
use ibc_core::primitives::proto::Any;
use prost::Message;

/// AnyCodec is a convenient trait that provides a generic way to encode and
/// decode domain types through the `Any` type.
pub trait AnyCodec {
    fn decode_any_vec<C>(data: Vec<u8>) -> Result<C, DecodingError>
    where
        C: TryFrom<Any>,
        <C as TryFrom<Any>>::Error: Display,
    {
        let raw = Any::decode(&mut data.as_slice())?;

        C::try_from(raw).map_err(DecodingError::invalid_raw_data)
    }

    fn encode_to_any_vec<C>(value: C) -> Vec<u8>
    where
        C: Into<Any>,
    {
        value.into().encode_to_vec()
    }
}

impl<T> AnyCodec for T where T: TryFrom<Any> + Into<Any> {}