diff --git a/client/src/context.rs b/client/src/context.rs index 64ffafe..508e926 100644 --- a/client/src/context.rs +++ b/client/src/context.rs @@ -13,29 +13,21 @@ use crate::user::UserMap; use crate::Config; /// Contains all the different bits of client state. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct State { - pub user_name: String, pub rooms: RoomMap, pub users: UserMap, } -impl Default for State { - fn default() -> Self { - return State { - user_name: Config::default().credentials.user_name().to_string(), - rooms: RoomMap::default(), - users: UserMap::default(), - }; - } -} - /// Holds process-wide context for message handlers to execute against. #[derive(Debug)] pub struct Context { /// Mutable state. pub state: State, + /// The user name with which we logged in to the server. + pub user_name: String, + /// Sender half of a channel used to send requests to the server. pub server_request_tx: Sender, @@ -65,6 +57,9 @@ pub struct ContextOptions { /// The state to start out with. pub initial_state: State, + /// The user name with which we logged in to the server. + pub user_name: String, + /// The buffer size of the server request channel. pub server_request_buffer: usize, @@ -80,6 +75,7 @@ impl Default for ContextOptions { fn default() -> Self { Self { initial_state: State::default(), + user_name: Config::default().credentials.user_name().to_string(), server_request_buffer: 100, control_response_buffer: 100, #[cfg(test)] @@ -104,6 +100,7 @@ impl ContextBundle { Self { context: Context { state: options.initial_state, + user_name: options.user_name, server_request_tx, control_response_tx, system_clock, diff --git a/client/src/handlers/login_status_request_handler.rs b/client/src/handlers/login_status_request_handler.rs index 6ef57f9..212a68e 100644 --- a/client/src/handlers/login_status_request_handler.rs +++ b/client/src/handlers/login_status_request_handler.rs @@ -15,7 +15,7 @@ impl MessageHandler for LoginStatusRequestHandler { context: &mut Context, &(): &Self::Message, ) -> anyhow::Result<()> { - let user_name = context.state.user_name.clone(); + let user_name = context.user_name.clone(); context .control_response_tx .blocking_send(control::Response::LoginStatusResponse( @@ -46,7 +46,7 @@ mod tests { #[test] fn run_sends_response() -> anyhow::Result<()> { let mut options = ContextOptions::default(); - options.initial_state.user_name = "shruti".to_string(); + options.user_name = "shruti".to_string(); let mut bundle = ContextBundle::new(options); LoginStatusRequestHandler::default() diff --git a/client/src/main.rs b/client/src/main.rs index b8e3567..2b9d7dd 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -83,7 +83,7 @@ async fn run_client( async fn async_main(config: Config) -> anyhow::Result<()> { let mut options = ContextOptions::default(); - options.initial_state.user_name = config.credentials.user_name().to_string(); + options.user_name = config.credentials.user_name().to_string(); let bundle = ContextBundle::new(options);