Browse Source

Move user name from State to Context.

wip
Titouan Rigoudy 4 years ago
parent
commit
cbc9777be0
3 changed files with 12 additions and 15 deletions
  1. +9
    -12
      client/src/context.rs
  2. +2
    -2
      client/src/handlers/login_status_request_handler.rs
  3. +1
    -1
      client/src/main.rs

+ 9
- 12
client/src/context.rs View File

@ -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<ServerRequest>,
@ -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,


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

@ -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()


+ 1
- 1
client/src/main.rs View File

@ -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);


Loading…
Cancel
Save