frame_decode/methods/
mod.rs1pub mod constant_decoder;
17pub mod constant_type_info;
18pub mod custom_value_decoder;
19pub mod custom_value_type_info;
20pub mod extrinsic_decoder;
21pub mod extrinsic_encoder;
22pub mod extrinsic_type_info;
23pub mod runtime_api_decoder;
24pub mod runtime_api_encoder;
25pub mod runtime_api_type_info;
26pub mod storage_decoder;
27pub mod storage_encoder;
28pub mod storage_type_info;
29pub mod view_function_decoder;
30pub mod view_function_encoder;
31pub mod view_function_type_info;
32
33#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
43pub enum Entry<In, Name> {
44 In(In),
46 Name(Name),
48}
49
50impl<T> Entry<T, T> {
51 pub fn map<R, F: FnOnce(T) -> R>(self, f: F) -> Entry<R, R> {
53 match self {
54 Entry::In(t) => Entry::In(f(t)),
55 Entry::Name(t) => Entry::Name(f(t)),
56 }
57 }
58}
59
60impl<In, Name> Entry<In, Name> {
61 pub fn entries_in(
64 entries: impl Iterator<Item = Entry<In, Name>>,
65 container: impl PartialEq<In>,
66 ) -> impl Iterator<Item = Name>
67 where
68 In: PartialEq,
69 Name: PartialEq,
70 {
71 entries
72 .skip_while(move |c| !matches!(c, Entry::In(c) if &container == c))
73 .skip(1)
74 .take_while(|c| matches!(c, Entry::Name(_)))
75 .filter_map(|c| match c {
76 Entry::In(_) => None,
77 Entry::Name(name) => Some(name),
78 })
79 }
80
81 pub fn tuples_of(
84 entries: impl Iterator<Item = Entry<In, Name>>,
85 ) -> impl Iterator<Item = (In, Name)>
86 where
87 In: Clone,
88 {
89 let mut entry_in = None;
90 entries.filter_map(move |entry| match entry {
91 Entry::In(e_in) => {
92 entry_in = Some(e_in);
93 None
94 }
95 Entry::Name(e_name) => {
96 let e_in = entry_in.as_ref().unwrap().clone();
97 Some((e_in, e_name))
98 }
99 })
100 }
101}
102
103#[cfg(test)]
104mod test {
105 use super::*;
106
107 #[test]
108 fn test_entries_in() {
109 fn entries() -> impl Iterator<Item = Entry<&'static str, &'static str>> {
110 [
111 Entry::In("Baz"),
112 Entry::In("Foo"),
113 Entry::Name("foo_a"),
114 Entry::Name("foo_b"),
115 Entry::Name("foo_c"),
116 Entry::In("Bar"),
117 Entry::Name("bar_a"),
118 Entry::In("Wibble"),
119 ]
120 .into_iter()
121 }
122
123 assert!(Entry::entries_in(entries(), "Baz").next().is_none());
124 assert!(Entry::entries_in(entries(), "Wibble").next().is_none());
125
126 let foos: Vec<String> = Entry::entries_in(entries(), "Foo")
127 .map(|s| s.to_owned())
128 .collect();
129 assert_eq!(
130 foos,
131 Vec::from_iter(["foo_a".to_owned(), "foo_b".to_owned(), "foo_c".to_owned(),])
132 );
133
134 let bars: Vec<String> = Entry::entries_in(entries(), "Bar")
135 .map(|s| s.to_owned())
136 .collect();
137 assert_eq!(bars, Vec::from_iter(["bar_a".to_owned(),]));
138 }
139}