cosm_utils/chain/
msg.rs

1use core::fmt::Debug;
2use cosmrs::{proto::traits::TypeUrl, tx::MessageExt, Any};
3use std::fmt::Display;
4
5use super::error::ChainError;
6
7pub trait Msg:
8    Clone + Sized + TryFrom<Self::Proto, Error = Self::Err> + TryInto<Self::Proto, Error = Self::Err>
9{
10    /// Protobuf type
11    type Proto: Default + MessageExt + Sized + TypeUrl;
12
13    /// Protobuf conversion error type
14    type Err: From<ChainError> + Debug + Display;
15
16    /// Parse this message proto from [`Any`].
17    fn from_any(any: &Any) -> Result<Self, Self::Err> {
18        Self::Proto::from_any(any)
19            .map_err(ChainError::prost_proto_decoding)?
20            .try_into()
21    }
22
23    /// Serialize this message proto as [`Any`].
24    fn to_any(&self) -> Result<Any, Self::Err> {
25        self.clone().into_any()
26    }
27
28    /// Convert this message proto into [`Any`].
29    fn into_any(self) -> Result<Any, Self::Err> {
30        Ok(self
31            .try_into()?
32            .to_any()
33            .map_err(ChainError::prost_proto_encoding)?)
34    }
35}