matrix-sdk: Add `room::State` enum

This enum contains the room in the Joined, Left and Invited state.
master
Julian Sparber 2021-03-16 18:22:09 +01:00
parent 5d66ff475f
commit 19cacb1f26
6 changed files with 78 additions and 7 deletions

View File

@ -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<room::Common> {
pub fn rooms(&self) -> Vec<room::Room> {
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<room::Common> {
pub fn get_room(&self, room_id: &RoomId) -> Option<room::Room> {
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.

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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<Common> 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<Joined> 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<Left> 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<Invited> 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 }),
}
}
}

View File

@ -0,0 +1 @@