diff --git a/client/src/control/response.rs b/client/src/control/response.rs index c303d41..f5fc918 100644 --- a/client/src/control/response.rs +++ b/client/src/control/response.rs @@ -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, diff --git a/client/src/handlers/room_join_request_handler.rs b/client/src/handlers/room_join_request_handler.rs index a8888dc..a90cb17 100644 --- a/client/src/handlers/room_join_request_handler.rs +++ b/client/src/handlers/room_join_request_handler.rs @@ -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() diff --git a/client/src/handlers/room_join_response_handler.rs b/client/src/handlers/room_join_response_handler.rs index e366171..d09d276 100644 --- a/client/src/handlers/room_join_response_handler.rs +++ b/client/src/handlers/room_join_response_handler.rs @@ -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 diff --git a/client/src/handlers/room_list_request_handler.rs b/client/src/handlers/room_list_request_handler.rs index e6f666c..e6ad47c 100644 --- a/client/src/handlers/room_list_request_handler.rs +++ b/client/src/handlers/room_list_request_handler.rs @@ -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) + ), ] ); } diff --git a/client/src/handlers/room_list_response_handler.rs b/client/src/handlers/room_list_response_handler.rs index 2f9b860..6beefe5 100644 --- a/client/src/handlers/room_list_response_handler.rs +++ b/client/src/handlers/room_list_response_handler.rs @@ -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) + ), ] ); } diff --git a/client/src/handlers/room_message_response_handler.rs b/client/src/handlers/room_message_response_handler.rs index 585896a..f883567 100644 --- a/client/src/handlers/room_message_response_handler.rs +++ b/client/src/handlers/room_message_response_handler.rs @@ -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(), diff --git a/client/src/room/map.rs b/client/src/room/map.rs index bcc7b04..d313519 100644 --- a/client/src/room/map.rs +++ b/client/src/room/map.rs @@ -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, + map: HashMap, } 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 { + pub fn insert(&mut self, name: String, room: RoomState) -> Option { 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, + old_map: &mut HashMap, ) { 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, mut operators: Vec, 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::( + serde_json::from_str::( 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) ); } } diff --git a/client/src/room/state.rs b/client/src/room/state.rs index 9623d37..29276d0 100644 --- a/client/src/room/state.rs +++ b/client/src/room/state.rs @@ -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,