Browse Source

Rename Room to RoomState.

main
Titouan Rigoudy 2 years ago
parent
commit
138f305da9
8 changed files with 48 additions and 38 deletions
  1. +8
    -6
      client/src/control/response.rs
  2. +3
    -3
      client/src/handlers/room_join_request_handler.rs
  3. +2
    -2
      client/src/handlers/room_join_response_handler.rs
  4. +7
    -4
      client/src/handlers/room_list_request_handler.rs
  5. +7
    -4
      client/src/handlers/room_list_response_handler.rs
  6. +3
    -3
      client/src/handlers/room_message_response_handler.rs
  7. +15
    -13
      client/src/room/map.rs
  8. +3
    -3
      client/src/room/state.rs

+ 8
- 6
client/src/control/response.rs View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use solstice_proto::User;
use crate::room::{Message, Room};
use crate::room::{Message, RoomState};
/// This enumeration is the list of possible control responses from the client
/// to the controller.
@ -21,7 +21,7 @@ pub enum Response {
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomJoinResponse {
pub room_name: String,
pub room: Room,
pub room: RoomState,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
@ -62,7 +62,7 @@ pub enum LoginStatusResponse {
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomListResponse {
/// The list of (room name, room data) pairs.
pub rooms: Vec<(String, Room)>,
pub rooms: Vec<(String, RoomState)>,
}
/// This structure contains a message said in a chat room the user is a member
@ -109,7 +109,9 @@ mod tests {
use solstice_proto::{User, UserStatus};
use crate::room::{Membership, Message, MessageHistory, Room, Visibility};
use crate::room::{
Membership, Message, MessageHistory, RoomState, Visibility,
};
use super::{
LoginStatusResponse, RoomJoinResponse, RoomLeaveResponse, RoomListResponse,
@ -192,7 +194,7 @@ mod tests {
.unwrap(),
RoomJoinResponse {
room_name: "bleep".to_string(),
room: Room {
room: RoomState {
membership: Membership::Joining,
visibility: Visibility::PrivateOwned,
operated: false,
@ -267,7 +269,7 @@ mod tests {
RoomListResponse {
rooms: vec![(
"bleep".to_string(),
Room {
RoomState {
membership: Membership::Joining,
visibility: Visibility::PrivateOwned,
operated: false,


+ 3
- 3
client/src/handlers/room_join_request_handler.rs View File

@ -75,7 +75,7 @@ mod tests {
use crate::context::{ContextBundle, ContextOptions};
use crate::control;
use crate::message_handler::MessageHandler;
use crate::room::{Membership, Room, Visibility};
use crate::room::{Membership, RoomState, Visibility};
use super::RoomJoinRequestHandler;
@ -99,7 +99,7 @@ mod tests {
#[test]
fn run_already_joined_responds_immediately() -> anyhow::Result<()> {
let mut room = Room::new(Visibility::Public, 3);
let mut room = RoomState::new(Visibility::Public, 3);
room.membership = Membership::Member;
let mut options = ContextOptions::default();
@ -138,7 +138,7 @@ mod tests {
options
.initial_state
.rooms
.insert("bleep".to_string(), Room::new(Visibility::Public, 3));
.insert("bleep".to_string(), RoomState::new(Visibility::Public, 3));
let mut bundle = ContextBundle::new(options);
RoomJoinRequestHandler::default()


+ 2
- 2
client/src/handlers/room_join_response_handler.rs View File

@ -55,13 +55,13 @@ mod tests {
use crate::context::{ContextBundle, ContextOptions};
use crate::control;
use crate::message_handler::MessageHandler;
use crate::room::{Membership, Room, Visibility};
use crate::room::{Membership, RoomState, Visibility};
use super::RoomJoinResponseHandler;
#[test]
fn run_updates_room_state_and_forwards_response() {
let mut room = Room::new(Visibility::Public, 42);
let mut room = RoomState::new(Visibility::Public, 42);
let mut options = ContextOptions::default();
options


+ 7
- 4
client/src/handlers/room_list_request_handler.rs View File

@ -45,12 +45,12 @@ mod tests {
use crate::context::{ContextBundle, ContextOptions};
use crate::control;
use crate::message_handler::MessageHandler;
use crate::room::{Room, Visibility};
use crate::room::{RoomState, Visibility};
use super::RoomListRequestHandler;
// Cannot get the compiler to be satisfied when borrowing the name...
fn room_name(pair: &(String, Room)) -> String {
fn room_name(pair: &(String, RoomState)) -> String {
pair.0.clone()
}
@ -97,8 +97,11 @@ mod tests {
assert_eq!(
rooms,
vec![
("apple".to_string(), Room::new(Visibility::Public, 42)),
("potato".to_string(), Room::new(Visibility::Public, 123)),
("apple".to_string(), RoomState::new(Visibility::Public, 42)),
(
"potato".to_string(),
RoomState::new(Visibility::Public, 123)
),
]
);
}


+ 7
- 4
client/src/handlers/room_list_response_handler.rs View File

@ -41,12 +41,12 @@ mod tests {
use crate::context::ContextBundle;
use crate::message_handler::MessageHandler;
use crate::room::{Room, Visibility};
use crate::room::{RoomState, Visibility};
use super::RoomListResponseHandler;
// Cannot get the compiler to be satisfied when borrowing the name...
fn room_name(pair: &(String, Room)) -> String {
fn room_name(pair: &(String, RoomState)) -> String {
pair.0.clone()
}
@ -71,8 +71,11 @@ mod tests {
assert_eq!(
rooms,
vec![
("apple".to_string(), Room::new(Visibility::Public, 42)),
("potato".to_string(), Room::new(Visibility::Public, 123)),
("apple".to_string(), RoomState::new(Visibility::Public, 42)),
(
"potato".to_string(),
RoomState::new(Visibility::Public, 123)
),
]
);
}


+ 3
- 3
client/src/handlers/room_message_response_handler.rs View File

@ -58,7 +58,7 @@ mod tests {
use crate::context::{ContextBundle, ContextOptions};
use crate::control;
use crate::message_handler::MessageHandler;
use crate::room::{Message, Room, Visibility};
use crate::room::{Message, RoomState, Visibility};
use super::RoomMessageResponseHandler;
@ -72,7 +72,7 @@ mod tests {
options
.initial_state
.rooms
.insert("apple".to_string(), Room::new(Visibility::Public, 1));
.insert("apple".to_string(), RoomState::new(Visibility::Public, 1));
options.simulated_clock =
Some(SimulatedSystemClock::new(system_time_from_secs(42)));
@ -109,7 +109,7 @@ mod tests {
#[test]
fn run_stores_message() {
let mut room = Room::new(Visibility::Public, 42);
let mut room = RoomState::new(Visibility::Public, 42);
room.messages.insert(Message {
received_at: system_time_from_secs(42),
user_name: "karandeep".to_string(),


+ 15
- 13
client/src/room/map.rs View File

@ -5,7 +5,7 @@ use log::{error, info, warn};
use solstice_proto::{server, User};
use thiserror::Error;
use crate::room::{Membership, Room, Visibility};
use crate::room::{Membership, RoomState, Visibility};
/// The error returned by RoomMap functions.
#[derive(Debug, Error)]
@ -26,7 +26,7 @@ pub struct RoomNotFoundError(String);
#[derive(Debug, Default)]
pub struct RoomMap {
/// The actual map from room names to room data.
map: HashMap<String, Room>,
map: HashMap<String, RoomState>,
}
impl RoomMap {
@ -37,7 +37,7 @@ impl RoomMap {
/// Inserts the given room in the map under the given name.
/// Same semantics as `std::collections::HashMap::insert()`.
pub fn insert(&mut self, name: String, room: Room) -> Option<Room> {
pub fn insert(&mut self, name: String, room: RoomState) -> Option<RoomState> {
self.map.insert(name, room)
}
@ -46,7 +46,7 @@ impl RoomMap {
pub fn get_strict(
&self,
room_name: &str,
) -> Result<&Room, RoomNotFoundError> {
) -> Result<&RoomState, RoomNotFoundError> {
match self.map.get(room_name) {
Some(room) => Ok(room),
None => Err(RoomNotFoundError(room_name.to_string())),
@ -58,7 +58,7 @@ impl RoomMap {
pub fn get_mut_strict(
&mut self,
room_name: &str,
) -> Result<&mut Room, RoomNotFoundError> {
) -> Result<&mut RoomState, RoomNotFoundError> {
match self.map.get_mut(room_name) {
Some(room) => Ok(room),
None => Err(RoomNotFoundError(room_name.to_string())),
@ -72,10 +72,10 @@ impl RoomMap {
name: String,
visibility: Visibility,
user_count: u32,
old_map: &mut HashMap<String, Room>,
old_map: &mut HashMap<String, RoomState>,
) {
let room = match old_map.remove(&name) {
None => Room::new(Visibility::Public, user_count as usize),
None => RoomState::new(Visibility::Public, user_count as usize),
Some(mut room) => {
room.visibility = visibility;
room.user_count = user_count as usize;
@ -118,7 +118,7 @@ impl RoomMap {
}
/// Returns the list of (room name, room data) representing all known rooms.
pub fn get_room_list(&self) -> Vec<(String, Room)> {
pub fn get_room_list(&self) -> Vec<(String, RoomState)> {
let mut rooms = Vec::new();
for (room_name, room) in self.map.iter() {
rooms.push((room_name.clone(), room.clone()));
@ -153,7 +153,7 @@ impl RoomMap {
owner: Option<String>,
mut operators: Vec<String>,
members: &[User],
) -> Result<&Room, RoomError> {
) -> Result<&RoomState, RoomError> {
// First look up the room struct.
let room = self.get_mut_strict(room_name)?;
@ -267,7 +267,9 @@ mod tests {
use solstice_proto::server::RoomListResponse;
use crate::room::{Membership, Message, MessageHistory, Room, Visibility};
use crate::room::{
Membership, Message, MessageHistory, RoomState, Visibility,
};
use super::RoomMap;
@ -311,7 +313,7 @@ mod tests {
#[test]
fn deserialize_room() {
assert_eq!(
serde_json::from_str::<Room>(
serde_json::from_str::<RoomState>(
r#"{
"membership": "Joining",
"visibility": "PrivateOwned",
@ -336,7 +338,7 @@ mod tests {
}"#
)
.unwrap(),
Room {
RoomState {
membership: Membership::Joining,
visibility: Visibility::PrivateOwned,
operated: false,
@ -387,7 +389,7 @@ mod tests {
assert_eq!(
rooms.get_strict("room a").unwrap(),
&Room::new(Visibility::Public, 42)
&RoomState::new(Visibility::Public, 42)
);
}
}

+ 3
- 3
client/src/room/state.rs View File

@ -106,7 +106,7 @@ impl MessageHistory {
/// It does not store the name, as that is stored implicitly as the key in the
/// room hash table.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Room {
pub struct RoomState {
/// The membership state of the user for the room.
pub membership: Membership,
/// The visibility of the room.
@ -128,10 +128,10 @@ pub struct Room {
pub tickers: Vec<(String, String)>,
}
impl Room {
impl RoomState {
/// Creates a new room with the given visibility and user count.
pub fn new(visibility: Visibility, user_count: usize) -> Self {
Room {
Self {
membership: Membership::NonMember,
visibility: visibility,
operated: false,


Loading…
Cancel
Save