pub mod constant_decoder;
pub mod constant_type_info;
pub mod custom_value_decoder;
pub mod custom_value_type_info;
pub mod extrinsic_decoder;
pub mod extrinsic_encoder;
pub mod extrinsic_type_info;
pub mod runtime_api_decoder;
pub mod runtime_api_encoder;
pub mod runtime_api_type_info;
pub mod storage_decoder;
pub mod storage_encoder;
pub mod storage_type_info;
pub mod view_function_decoder;
pub mod view_function_encoder;
pub mod view_function_type_info;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Entry<In, Name> {
In(In),
Name(Name),
}
impl<T> Entry<T, T> {
pub fn map<R, F: FnOnce(T) -> R>(self, f: F) -> Entry<R, R> {
match self {
Entry::In(t) => Entry::In(f(t)),
Entry::Name(t) => Entry::Name(f(t)),
}
}
}
impl<In, Name> Entry<In, Name> {
pub fn entries_in(
entries: impl Iterator<Item = Entry<In, Name>>,
container: impl PartialEq<In>,
) -> impl Iterator<Item = Name>
where
In: PartialEq,
Name: PartialEq,
{
entries
.skip_while(move |c| !matches!(c, Entry::In(c) if &container == c))
.skip(1)
.take_while(|c| matches!(c, Entry::Name(_)))
.filter_map(|c| match c {
Entry::In(_) => None,
Entry::Name(name) => Some(name),
})
}
pub fn tuples_of(
entries: impl Iterator<Item = Entry<In, Name>>,
) -> impl Iterator<Item = (In, Name)>
where
In: Clone,
{
let mut entry_in = None;
entries.filter_map(move |entry| match entry {
Entry::In(e_in) => {
entry_in = Some(e_in);
None
}
Entry::Name(e_name) => {
let e_in = entry_in.as_ref().unwrap().clone();
Some((e_in, e_name))
}
})
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_entries_in() {
fn entries() -> impl Iterator<Item = Entry<&'static str, &'static str>> {
[
Entry::In("Baz"),
Entry::In("Foo"),
Entry::Name("foo_a"),
Entry::Name("foo_b"),
Entry::Name("foo_c"),
Entry::In("Bar"),
Entry::Name("bar_a"),
Entry::In("Wibble"),
]
.into_iter()
}
assert!(Entry::entries_in(entries(), "Baz").next().is_none());
assert!(Entry::entries_in(entries(), "Wibble").next().is_none());
let foos: Vec<String> = Entry::entries_in(entries(), "Foo")
.map(|s| s.to_owned())
.collect();
assert_eq!(
foos,
Vec::from_iter(["foo_a".to_owned(), "foo_b".to_owned(), "foo_c".to_owned(),])
);
let bars: Vec<String> = Entry::entries_in(entries(), "Bar")
.map(|s| s.to_owned())
.collect();
assert_eq!(bars, Vec::from_iter(["bar_a".to_owned(),]));
}
}