From 187bd36ec66b0e406023ea138130eaffd7661ad2 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Mon, 13 Sep 2021 16:26:51 +0200 Subject: [PATCH] Use thiserror for room errors. --- client/src/room.rs | 84 +++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/client/src/room.rs b/client/src/room.rs index b349e62..5d69589 100644 --- a/client/src/room.rs +++ b/client/src/room.rs @@ -1,10 +1,9 @@ -use std::collections; -use std::error; -use std::fmt; +use std::collections::{HashMap, HashSet}; use std::mem; use serde::{Deserialize, Serialize}; use solstice_proto::{server, User}; +use thiserror::Error; /// This enumeration is the list of possible membership states for a chat room. #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] @@ -57,9 +56,9 @@ pub struct Room { /// The name of the room's owner, if any. pub owner: Option, /// The names of the room's operators. - pub operators: collections::HashSet, + pub operators: HashSet, /// The names of the room's members. - pub members: collections::HashSet, + pub members: HashSet, /// The messages sent to this chat room, in chronological order. pub messages: Vec, /// The tickers displayed in this room. @@ -75,8 +74,8 @@ impl Room { operated: false, user_count: user_count, owner: None, - operators: collections::HashSet::new(), - members: collections::HashSet::new(), + operators: HashSet::new(), + members: HashSet::new(), messages: Vec::new(), tickers: Vec::new(), } @@ -84,37 +83,13 @@ impl Room { } /// The error returned by RoomMap functions. -#[derive(Debug)] -pub enum Error { +#[derive(Debug, Error)] +pub enum RoomError { + #[error("room {0} not found")] RoomNotFound(String), - MembershipChangeInvalid(Membership, Membership), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Error::RoomNotFound(ref room_name) => { - write!(f, "room {:?} not found", room_name) - } - - Error::MembershipChangeInvalid(old_membership, new_membership) => { - write!( - f, - "cannot change membership from {:?} to {:?}", - old_membership, new_membership - ) - } - } - } -} -impl error::Error for Error { - fn description(&self) -> &str { - match *self { - Error::RoomNotFound(_) => "room not found", - Error::MembershipChangeInvalid(_, _) => "cannot change membership", - } - } + #[error("cannot change membership from {0:?} to {1:?}")] + MembershipChangeInvalid(Membership, Membership), } /// Contains the mapping from room names to room data and provides a clean @@ -122,7 +97,7 @@ impl error::Error for Error { #[derive(Debug, Default)] pub struct RoomMap { /// The actual map from room names to room data. - map: collections::HashMap, + map: HashMap, } impl RoomMap { @@ -141,19 +116,22 @@ impl RoomMap { /// Looks up the given room name in the map, returning an immutable /// reference to the associated data if found, or an error if not found. #[cfg(test)] - pub fn get_strict(&self, room_name: &str) -> Result<&Room, Error> { + pub fn get_strict(&self, room_name: &str) -> Result<&Room, RoomError> { match self.map.get(room_name) { Some(room) => Ok(room), - None => Err(Error::RoomNotFound(room_name.to_string())), + None => Err(RoomError::RoomNotFound(room_name.to_string())), } } /// Looks up the given room name in the map, returning a mutable /// reference to the associated data if found, or an error if not found. - fn get_mut_strict(&mut self, room_name: &str) -> Result<&mut Room, Error> { + fn get_mut_strict( + &mut self, + room_name: &str, + ) -> Result<&mut Room, RoomError> { match self.map.get_mut(room_name) { Some(room) => Ok(room), - None => Err(Error::RoomNotFound(room_name.to_string())), + None => Err(RoomError::RoomNotFound(room_name.to_string())), } } @@ -164,7 +142,7 @@ impl RoomMap { name: String, visibility: Visibility, user_count: u32, - old_map: &mut collections::HashMap, + old_map: &mut HashMap, ) { let room = match old_map.remove(&name) { None => Room::new(Visibility::Public, user_count as usize), @@ -183,7 +161,7 @@ impl RoomMap { /// server response. pub fn set_room_list(&mut self, mut response: server::RoomListResponse) { // Replace the old mapping with an empty one. - let mut old_map = mem::replace(&mut self.map, collections::HashMap::new()); + let mut old_map = mem::replace(&mut self.map, HashMap::new()); // Add all public rooms. for (name, user_count) in response.rooms.drain(..) { @@ -221,7 +199,7 @@ impl RoomMap { /// Records that we are now trying to join the given room. /// If the room is not found, or if its membership is not `NonMember`, /// returns an error. - pub fn start_joining(&mut self, room_name: &str) -> Result<(), Error> { + pub fn start_joining(&mut self, room_name: &str) -> Result<(), RoomError> { let room = self.get_mut_strict(room_name)?; match room.membership { @@ -230,7 +208,7 @@ impl RoomMap { Ok(()) } - membership => Err(Error::MembershipChangeInvalid( + membership => Err(RoomError::MembershipChangeInvalid( membership, Membership::Joining, )), @@ -245,7 +223,7 @@ impl RoomMap { owner: Option, mut operators: Vec, members: &[User], - ) -> Result<&Room, Error> { + ) -> Result<&Room, RoomError> { // First look up the room struct. let room = self.get_mut_strict(room_name)?; @@ -280,7 +258,7 @@ impl RoomMap { /// Records that we are now trying to leave the given room. /// If the room is not found, or if its membership status is not `Member`, /// returns an error. - pub fn start_leaving(&mut self, room_name: &str) -> Result<(), Error> { + pub fn start_leaving(&mut self, room_name: &str) -> Result<(), RoomError> { let room = self.get_mut_strict(room_name)?; match room.membership { @@ -289,7 +267,7 @@ impl RoomMap { Ok(()) } - membership => Err(Error::MembershipChangeInvalid( + membership => Err(RoomError::MembershipChangeInvalid( membership, Membership::Leaving, )), @@ -297,7 +275,7 @@ impl RoomMap { } /// Records that we have now left the given room. - pub fn leave(&mut self, room_name: &str) -> Result<(), Error> { + pub fn leave(&mut self, room_name: &str) -> Result<(), RoomError> { let room = self.get_mut_strict(room_name)?; match room.membership { @@ -318,7 +296,7 @@ impl RoomMap { &mut self, room_name: &str, message: Message, - ) -> Result<(), Error> { + ) -> Result<(), RoomError> { let room = self.get_mut_strict(room_name)?; room.messages.push(message); Ok(()) @@ -330,7 +308,7 @@ impl RoomMap { &mut self, room_name: &str, user_name: String, - ) -> Result<(), Error> { + ) -> Result<(), RoomError> { let room = self.get_mut_strict(room_name)?; room.members.insert(user_name); Ok(()) @@ -342,7 +320,7 @@ impl RoomMap { &mut self, room_name: &str, user_name: &str, - ) -> Result<(), Error> { + ) -> Result<(), RoomError> { let room = self.get_mut_strict(room_name)?; room.members.remove(user_name); Ok(()) @@ -356,7 +334,7 @@ impl RoomMap { &mut self, room_name: &str, tickers: Vec<(String, String)>, - ) -> Result<(), Error> { + ) -> Result<(), RoomError> { let room = self.get_mut_strict(room_name)?; room.tickers = tickers; Ok(())