diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 0639a635..0537fa0b 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -544,11 +544,11 @@ impl Client { /// Get all the rooms the client knows about. /// /// This will return the list of joined, invited, and left rooms. - pub fn rooms(&self) -> Vec { + pub fn rooms(&self) -> Vec { self.store() .get_rooms() .into_iter() - .map(|room| room::Common::new(self.clone(), room)) + .map(|room| room::Common::new(self.clone(), room).into()) .collect() } @@ -584,10 +584,10 @@ impl Client { /// # Arguments /// /// `room_id` - The unique id of the room that should be fetched. - pub fn get_room(&self, room_id: &RoomId) -> Option { + pub fn get_room(&self, room_id: &RoomId) -> Option { self.store() .get_room(room_id) - .map(|room| room::Common::new(self.clone(), room)) + .map(|room| room::Common::new(self.clone(), room).into()) } /// Get a joined room with the given room id. diff --git a/matrix_sdk/src/room/invited.rs b/matrix_sdk/src/room/invited.rs index 67cd10b7..879ce58a 100644 --- a/matrix_sdk/src/room/invited.rs +++ b/matrix_sdk/src/room/invited.rs @@ -7,7 +7,7 @@ use std::ops::Deref; /// Operations may fail once the underlaying `Room` changes `RoomType`. #[derive(Debug, Clone)] pub struct Invited { - inner: Common, + pub(crate) inner: Common, } impl Invited { diff --git a/matrix_sdk/src/room/joined.rs b/matrix_sdk/src/room/joined.rs index b8ce2b41..797b9d70 100644 --- a/matrix_sdk/src/room/joined.rs +++ b/matrix_sdk/src/room/joined.rs @@ -46,7 +46,7 @@ use tracing::instrument; /// Operations may fail once the underlaying `Room` changes `RoomType`. #[derive(Debug, Clone)] pub struct Joined { - inner: Common, + pub(crate) inner: Common, } impl Deref for Joined { diff --git a/matrix_sdk/src/room/left.rs b/matrix_sdk/src/room/left.rs index 52638f9c..b49940d5 100644 --- a/matrix_sdk/src/room/left.rs +++ b/matrix_sdk/src/room/left.rs @@ -9,7 +9,7 @@ use matrix_sdk_common::api::r0::membership::forget_room; /// Operations may fail once the underlaying `Room` changes `RoomType`. #[derive(Debug, Clone)] pub struct Left { - inner: Common, + pub(crate) inner: Common, } impl Left { diff --git a/matrix_sdk/src/room/mod.rs b/matrix_sdk/src/room/mod.rs index 906500b1..ee603cd9 100644 --- a/matrix_sdk/src/room/mod.rs +++ b/matrix_sdk/src/room/mod.rs @@ -1,3 +1,7 @@ +use std::ops::Deref; + +use crate::RoomType; + mod common; mod invited; mod joined; @@ -7,3 +11,69 @@ pub use self::common::Common; pub use self::invited::Invited; pub use self::joined::Joined; pub use self::left::Left; + +/// An enum that abstracts over the different states a room can be in. +#[derive(Debug, Clone)] +pub enum Room { + /// The room in the `join` state. + Joined(Joined), + /// The room in the `left` state. + Left(Left), + /// The room in the `invited` state. + Invited(Invited), +} + +impl Deref for Room { + type Target = Common; + + fn deref(&self) -> &Self::Target { + match self { + Self::Joined(room) => &*room, + Self::Left(room) => &*room, + Self::Invited(room) => &*room, + } + } +} + +impl From for Room { + fn from(room: Common) -> Self { + match room.room_type() { + RoomType::Joined => Self::Joined(Joined { inner: room }), + RoomType::Left => Self::Left(Left { inner: room }), + RoomType::Invited => Self::Invited(Invited { inner: room }), + } + } +} + +impl From for Room { + fn from(room: Joined) -> Self { + let room = (*room).clone(); + match room.room_type() { + RoomType::Joined => Self::Joined(Joined { inner: room }), + RoomType::Left => Self::Left(Left { inner: room }), + RoomType::Invited => Self::Invited(Invited { inner: room }), + } + } +} + +impl From for Room { + fn from(room: Left) -> Self { + let room = (*room).clone(); + match room.room_type() { + RoomType::Joined => Self::Joined(Joined { inner: room }), + RoomType::Left => Self::Left(Left { inner: room }), + RoomType::Invited => Self::Invited(Invited { inner: room }), + } + } +} + +impl From for Room { + fn from(room: Invited) -> Self { + let room = (*room).clone(); + match room.room_type() { + RoomType::Joined => Self::Joined(Joined { inner: room }), + RoomType::Left => Self::Left(Left { inner: room }), + RoomType::Invited => Self::Invited(Invited { inner: room }), + } + } +} diff --git a/matrix_sdk/src/room/room.rs b/matrix_sdk/src/room/room.rs new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/matrix_sdk/src/room/room.rs @@ -0,0 +1 @@ +