Custom iterator for Ruleset

This commit is contained in:
Ana Gelez 2020-11-27 20:53:34 +01:00
parent 0e6d3f17c6
commit 57efd8cd5b

View File

@ -34,7 +34,7 @@
//! There is also the `AnyPushRule` type that is the most generic form of push rule, with all //! There is also the `AnyPushRule` type that is the most generic form of push rule, with all
//! the possible fields. //! the possible fields.
use std::collections::BTreeSet; use std::collections::btree_set::{BTreeSet, IntoIter as BTreeSetIter};
use ruma_common::StringEnum; use ruma_common::StringEnum;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -94,20 +94,41 @@ impl Ruleset {
} }
} }
/// Iterator type for `Ruleset`
#[derive(Debug)]
pub struct RulesetIter {
content: BTreeSetIter<ContentPushRule>,
override_: BTreeSetIter<OverridePushRule>,
room: BTreeSetIter<RoomPushRule>,
sender: BTreeSetIter<SenderPushRule>,
underride: BTreeSetIter<UnderridePushRule>
}
impl Iterator for RulesetIter {
type Item = AnyPushRule;
fn next(&mut self) -> Option<Self::Item> {
self.content.next().map(|x| x.0.into())
.or_else(|| self.override_.next().map(|x| x.0.into()))
.or_else(|| self.room.next().map(|x| x.0.into()))
.or_else(|| self.sender.next().map(|x| x.0.into()))
.or_else(|| self.underride.next().map(|x| x.0.into()))
}
}
impl IntoIterator for Ruleset { impl IntoIterator for Ruleset {
type Item = AnyPushRule; type Item = AnyPushRule;
type IntoIter = std::vec::IntoIter<Self::Item>; type IntoIter = RulesetIter;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.content RulesetIter {
.into_iter() content: self.content.into_iter(),
.map(|x| x.0.into()) override_: self.override_.into_iter(),
.chain(self.override_.into_iter().map(|x| x.0.into())) room: self.room.into_iter(),
.chain(self.room.into_iter().map(|x| x.0.into())) sender: self.sender.into_iter(),
.chain(self.sender.into_iter().map(|x| x.0.into())) underride: self.underride.into_iter(),
.chain(self.underride.into_iter().map(|x| x.0.into())) }
.collect::<Vec<_>>()
.into_iter()
} }
} }