use anyhow::Context as AnyhowContext; use solstice_proto::server::RoomListResponse; use crate::context::Context; use crate::control; use crate::message_handler::MessageHandler; #[derive(Debug, Default)] pub struct RoomListResponseHandler; impl MessageHandler for RoomListResponseHandler { type Message = RoomListResponse; fn run( self, context: &mut Context, message: &RoomListResponse, ) -> anyhow::Result<()> { let response = message.clone(); context.state.rooms.set_room_list(response); let rooms = context.state.rooms.get_room_list(); let control_response = control::Response::RoomListResponse(control::RoomListResponse { rooms }); context .control_response_tx .blocking_send(control_response) .context("sending control response")?; Ok(()) } fn name() -> String { "RoomListResponseHandler".to_string() } } #[cfg(test)] mod tests { use solstice_proto::server::RoomListResponse; use crate::context::ContextBundle; use crate::message_handler::MessageHandler; use crate::room::{RoomState, RoomVisibility}; use super::RoomListResponseHandler; // Cannot get the compiler to be satisfied when borrowing the name... fn room_name(pair: &(String, RoomState)) -> String { pair.0.clone() } #[test] fn run_sets_room_list() { let mut bundle = ContextBundle::default(); let response = RoomListResponse { rooms: vec![("potato".to_string(), 123), ("apple".to_string(), 42)], owned_private_rooms: vec![], other_private_rooms: vec![], operated_private_room_names: vec![], }; RoomListResponseHandler::default() .run(&mut bundle.context, &response) .unwrap(); let mut rooms = bundle.context.state.rooms.get_room_list(); rooms.sort_by_key(room_name); assert_eq!( rooms, vec![ ( "apple".to_string(), RoomState::new(RoomVisibility::Public, 42) ), ( "potato".to_string(), RoomState::new(RoomVisibility::Public, 123) ), ] ); } }