1#[cfg(feature = "protogen")]
2mod error;
3
4pub use imbibe_macros::GetSigners;
5
6#[cfg(feature = "protogen")]
7pub use self::{codegen::*, error::ProtosError, signer_extractor::*};
8
9#[allow(clippy::doc_overindented_list_items, clippy::doc_lazy_continuation)]
10mod codegen {
11 #[cfg(feature = "protogen")]
12 include!(concat!(env!("OUT_DIR"), "/code_gen/mod.rs"));
13}
14
15#[cfg(feature = "protogen")]
16mod signer_extractor {
17 #[allow(unused_imports)]
18 use super::{GetSigners, codegen::*, error};
19
20 macro_rules! generate_signer_extractors {
21 (
22 $(($type_url:literal, $rust_struct:path)),* $(,)?
23 ) => {
24 pub fn signers_from_any_msg(
25 msg: &::cosmrs::Any,
26 ) -> Result<Box<dyn Iterator<Item = String>>, error::ProtosError> {
27 match msg.type_url.as_str() {
28 $(
29 $type_url => {
30 ::cosmrs::Any::to_msg::<$rust_struct>(msg)
31 .map(GetSigners::signers)
32 .map(|signers| Box::new(signers) as Box<dyn Iterator<Item = String>>)
33 .map_err(From::from)
34 },
35 )*
36 _ => Err(error::ProtosError::NoSignerInMsg { type_url: msg.type_url.clone() }),
37 }
38 }
39
40 #[allow(unused_variables)]
41 pub fn extend_with_signers_from_any_msg<E>(
42 msg: &::cosmrs::Any,
43 signers: &mut E,
44 ) -> Result<(), error::ProtosError>
45 where
46 E: ::std::iter::Extend<String>,
47 {
48 match msg.type_url.as_str() {
49 $(
50 $type_url => {
51 ::cosmrs::Any::to_msg::<$rust_struct>(msg)
52 .map(GetSigners::signers)
53 .map(|s| ::std::iter::Extend::extend(signers, s))
54 .map_err(From::from)
55 },
56 )*
57 _ => Err(error::ProtosError::NoSignerInMsg { type_url: msg.type_url.clone() }),
58 }
59 }
60
61 pub fn unique_signers_from_any_msg(
62 msg: &::cosmrs::Any,
63 ) -> Result<::std::collections::HashSet<String>, error::ProtosError>
64 {
65 let mut unique_signers = std::collections::HashSet::new();
66 extend_with_signers_from_any_msg(msg, &mut unique_signers)?;
67
68 Ok(unique_signers)
69 }
70
71 pub fn unique_signers_from_any_msgs<'a, I>(
72 msgs: I,
73 ) -> Result<::std::collections::HashSet<String>, error::ProtosError>
74 where
75 I: IntoIterator<Item = &'a cosmrs::Any> + 'a,
76 {
77 let mut unique_signers = std::collections::HashSet::new();
78 msgs.into_iter()
79 .map(|msg| extend_with_signers_from_any_msg(msg, &mut unique_signers))
80 .collect::<Result<(), _>>()?;
81
82 Ok(unique_signers)
83 }
84 };
85 }
86
87 #[cfg(feature = "protogen")]
88 include!(concat!(env!("OUT_DIR"), "/any_signer_extractor.rs"));
89}
90
91pub trait GetSigners {
92 type Signer;
93
94 fn signers(self) -> impl Iterator<Item = Self::Signer>;
95}