Skip to content

Commit 19cacb1

Browse files
committed
matrix-sdk: Add room::State enum
This enum contains the room in the Joined, Left and Invited state.
1 parent 5d66ff4 commit 19cacb1

File tree

6 files changed

+78
-7
lines changed

6 files changed

+78
-7
lines changed

matrix_sdk/src/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,11 @@ impl Client {
544544
/// Get all the rooms the client knows about.
545545
///
546546
/// This will return the list of joined, invited, and left rooms.
547-
pub fn rooms(&self) -> Vec<room::Common> {
547+
pub fn rooms(&self) -> Vec<room::Room> {
548548
self.store()
549549
.get_rooms()
550550
.into_iter()
551-
.map(|room| room::Common::new(self.clone(), room))
551+
.map(|room| room::Common::new(self.clone(), room).into())
552552
.collect()
553553
}
554554

@@ -584,10 +584,10 @@ impl Client {
584584
/// # Arguments
585585
///
586586
/// `room_id` - The unique id of the room that should be fetched.
587-
pub fn get_room(&self, room_id: &RoomId) -> Option<room::Common> {
587+
pub fn get_room(&self, room_id: &RoomId) -> Option<room::Room> {
588588
self.store()
589589
.get_room(room_id)
590-
.map(|room| room::Common::new(self.clone(), room))
590+
.map(|room| room::Common::new(self.clone(), room).into())
591591
}
592592

593593
/// Get a joined room with the given room id.

matrix_sdk/src/room/invited.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ops::Deref;
77
/// Operations may fail once the underlaying `Room` changes `RoomType`.
88
#[derive(Debug, Clone)]
99
pub struct Invited {
10-
inner: Common,
10+
pub(crate) inner: Common,
1111
}
1212

1313
impl Invited {

matrix_sdk/src/room/joined.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use tracing::instrument;
4646
/// Operations may fail once the underlaying `Room` changes `RoomType`.
4747
#[derive(Debug, Clone)]
4848
pub struct Joined {
49-
inner: Common,
49+
pub(crate) inner: Common,
5050
}
5151

5252
impl Deref for Joined {

matrix_sdk/src/room/left.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use matrix_sdk_common::api::r0::membership::forget_room;
99
/// Operations may fail once the underlaying `Room` changes `RoomType`.
1010
#[derive(Debug, Clone)]
1111
pub struct Left {
12-
inner: Common,
12+
pub(crate) inner: Common,
1313
}
1414

1515
impl Left {

matrix_sdk/src/room/mod.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::ops::Deref;
2+
3+
use crate::RoomType;
4+
15
mod common;
26
mod invited;
37
mod joined;
@@ -7,3 +11,69 @@ pub use self::common::Common;
711
pub use self::invited::Invited;
812
pub use self::joined::Joined;
913
pub use self::left::Left;
14+
15+
/// An enum that abstracts over the different states a room can be in.
16+
#[derive(Debug, Clone)]
17+
pub enum Room {
18+
/// The room in the `join` state.
19+
Joined(Joined),
20+
/// The room in the `left` state.
21+
Left(Left),
22+
/// The room in the `invited` state.
23+
Invited(Invited),
24+
}
25+
26+
impl Deref for Room {
27+
type Target = Common;
28+
29+
fn deref(&self) -> &Self::Target {
30+
match self {
31+
Self::Joined(room) => &*room,
32+
Self::Left(room) => &*room,
33+
Self::Invited(room) => &*room,
34+
}
35+
}
36+
}
37+
38+
impl From<Common> for Room {
39+
fn from(room: Common) -> Self {
40+
match room.room_type() {
41+
RoomType::Joined => Self::Joined(Joined { inner: room }),
42+
RoomType::Left => Self::Left(Left { inner: room }),
43+
RoomType::Invited => Self::Invited(Invited { inner: room }),
44+
}
45+
}
46+
}
47+
48+
impl From<Joined> for Room {
49+
fn from(room: Joined) -> Self {
50+
let room = (*room).clone();
51+
match room.room_type() {
52+
RoomType::Joined => Self::Joined(Joined { inner: room }),
53+
RoomType::Left => Self::Left(Left { inner: room }),
54+
RoomType::Invited => Self::Invited(Invited { inner: room }),
55+
}
56+
}
57+
}
58+
59+
impl From<Left> for Room {
60+
fn from(room: Left) -> Self {
61+
let room = (*room).clone();
62+
match room.room_type() {
63+
RoomType::Joined => Self::Joined(Joined { inner: room }),
64+
RoomType::Left => Self::Left(Left { inner: room }),
65+
RoomType::Invited => Self::Invited(Invited { inner: room }),
66+
}
67+
}
68+
}
69+
70+
impl From<Invited> for Room {
71+
fn from(room: Invited) -> Self {
72+
let room = (*room).clone();
73+
match room.room_type() {
74+
RoomType::Joined => Self::Joined(Joined { inner: room }),
75+
RoomType::Left => Self::Left(Left { inner: room }),
76+
RoomType::Invited => Self::Invited(Invited { inner: room }),
77+
}
78+
}
79+
}

matrix_sdk/src/room/room.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)