mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-19 14:19:06 -04:00
feat(base): Add RoomUpdates::iter_all_room_ids.
This patch adds the `RoomUpdates::iter_all_room_ids` method, to iterate over all IDs of rooms that have received an update.
This commit is contained in:
@@ -80,6 +80,80 @@ pub struct RoomUpdates {
|
||||
pub knocked: BTreeMap<OwnedRoomId, KnockedRoomUpdate>,
|
||||
}
|
||||
|
||||
impl RoomUpdates {
|
||||
/// Iterate over all room IDs, from [`RoomUpdates::left`],
|
||||
/// [`RoomUpdates::joined`], [`RoomUpdates::invited`] and
|
||||
/// [`RoomUpdates::knocked`].
|
||||
fn iter_all_room_ids(&self) -> impl Iterator<Item = &OwnedRoomId> {
|
||||
self.left
|
||||
.keys()
|
||||
.chain(self.joined.keys())
|
||||
.chain(self.invited.keys())
|
||||
.chain(self.knocked.keys())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use assert_matches::assert_matches;
|
||||
use ruma::room_id;
|
||||
|
||||
use super::{
|
||||
InvitedRoomUpdate, JoinedRoomUpdate, KnockedRoomUpdate, LeftRoomUpdate, RoomUpdates,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_room_updates_iter_all_room_ids() {
|
||||
let room_id_0 = room_id!("!r0");
|
||||
let room_id_1 = room_id!("!r1");
|
||||
let room_id_2 = room_id!("!r2");
|
||||
let room_id_3 = room_id!("!r3");
|
||||
let room_id_4 = room_id!("!r4");
|
||||
let room_id_5 = room_id!("!r5");
|
||||
let room_id_6 = room_id!("!r6");
|
||||
let room_id_7 = room_id!("!r7");
|
||||
let room_updates = RoomUpdates {
|
||||
left: {
|
||||
let mut left = BTreeMap::new();
|
||||
left.insert(room_id_0.to_owned(), LeftRoomUpdate::default());
|
||||
left.insert(room_id_1.to_owned(), LeftRoomUpdate::default());
|
||||
left
|
||||
},
|
||||
joined: {
|
||||
let mut joined = BTreeMap::new();
|
||||
joined.insert(room_id_2.to_owned(), JoinedRoomUpdate::default());
|
||||
joined.insert(room_id_3.to_owned(), JoinedRoomUpdate::default());
|
||||
joined
|
||||
},
|
||||
invited: {
|
||||
let mut invited = BTreeMap::new();
|
||||
invited.insert(room_id_4.to_owned(), InvitedRoomUpdate::default());
|
||||
invited.insert(room_id_5.to_owned(), InvitedRoomUpdate::default());
|
||||
invited
|
||||
},
|
||||
knocked: {
|
||||
let mut knocked = BTreeMap::new();
|
||||
knocked.insert(room_id_6.to_owned(), KnockedRoomUpdate::default());
|
||||
knocked.insert(room_id_7.to_owned(), KnockedRoomUpdate::default());
|
||||
knocked
|
||||
},
|
||||
};
|
||||
|
||||
let mut iter = room_updates.iter_all_room_ids();
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_0));
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_1));
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_2));
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_3));
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_4));
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_5));
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_6));
|
||||
assert_matches!(iter.next(), Some(room_id) => assert_eq!(room_id, room_id_7));
|
||||
assert!(iter.next().is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
impl fmt::Debug for RoomUpdates {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
|
||||
Reference in New Issue
Block a user