|
|
@ -1,6 +1,5 @@ |
|
|
use std::mem;
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
|
use anyhow::anyhow;
|
|
|
|
|
|
use solstice_proto::ServerRequest;
|
|
|
use solstice_proto::ServerRequest;
|
|
|
use thiserror::Error;
|
|
|
use thiserror::Error;
|
|
|
use tokio::sync::mpsc::Sender;
|
|
|
use tokio::sync::mpsc::Sender;
|
|
|
@ -22,6 +21,9 @@ pub struct ServerLoggedInContext { |
|
|
|
|
|
|
|
|
// Immutable, see accessor methods.
|
|
|
// Immutable, see accessor methods.
|
|
|
user_name: String,
|
|
|
user_name: String,
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Remove annotation.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
motd: String,
|
|
|
motd: String,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -49,17 +51,21 @@ impl ServerLoggedInContext { |
|
|
request_tx,
|
|
|
request_tx,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// The user name with which we logged in.
|
|
|
/// The user name with which we logged in.
|
|
|
|
|
|
#[cfg(test)]
|
|
|
pub fn user_name(&self) -> &str {
|
|
|
pub fn user_name(&self) -> &str {
|
|
|
&self.user_name
|
|
|
&self.user_name
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// The message of the day sent by the server when we logged in.
|
|
|
/// The message of the day sent by the server when we logged in.
|
|
|
|
|
|
#[cfg(test)]
|
|
|
pub fn motd(&self) -> &str {
|
|
|
pub fn motd(&self) -> &str {
|
|
|
&self.motd
|
|
|
&self.motd
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Records that we have logged out.
|
|
|
/// Records that we have logged out.
|
|
|
|
|
|
#[cfg(test)]
|
|
|
pub fn logout(self) -> ServerLoggedOutContext {
|
|
|
pub fn logout(self) -> ServerLoggedOutContext {
|
|
|
ServerLoggedOutContext {
|
|
|
ServerLoggedOutContext {
|
|
|
user_name: self.user_name,
|
|
|
user_name: self.user_name,
|
|
|
@ -79,6 +85,7 @@ pub struct ServerLoggedOutContext { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Information provided upon a successful login.
|
|
|
/// Information provided upon a successful login.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub struct ServerLoginInfo {
|
|
|
pub struct ServerLoginInfo {
|
|
|
/// The message of the day sent by the server when we logged in.
|
|
|
/// The message of the day sent by the server when we logged in.
|
|
|
pub motd: String,
|
|
|
pub motd: String,
|
|
|
@ -89,22 +96,26 @@ pub struct ServerLoginInfo { |
|
|
|
|
|
|
|
|
impl ServerLoggedOutContext {
|
|
|
impl ServerLoggedOutContext {
|
|
|
/// The user name with which we will attempt or have attempted to log in.
|
|
|
/// The user name with which we will attempt or have attempted to log in.
|
|
|
|
|
|
#[cfg(test)]
|
|
|
pub fn user_name(&self) -> &str {
|
|
|
pub fn user_name(&self) -> &str {
|
|
|
&self.user_name
|
|
|
&self.user_name
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// The error received during the last login attempt, if any.
|
|
|
/// The error received during the last login attempt, if any.
|
|
|
/// `None` if we have never attempted to log in.
|
|
|
/// `None` if we have never attempted to log in.
|
|
|
|
|
|
#[cfg(test)]
|
|
|
pub fn error(&self) -> Option<&str> {
|
|
|
pub fn error(&self) -> Option<&str> {
|
|
|
self.error.as_deref()
|
|
|
self.error.as_deref()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Records that we failed to log in, and the server sent us the given error.
|
|
|
/// Records that we failed to log in, and the server sent us the given error.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub fn set_error(&mut self, error: String) {
|
|
|
pub fn set_error(&mut self, error: String) {
|
|
|
self.error = Some(error)
|
|
|
self.error = Some(error)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Records that we logged in successfully.
|
|
|
/// Records that we logged in successfully.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub fn login(self, info: ServerLoginInfo) -> ServerLoggedInContext {
|
|
|
pub fn login(self, info: ServerLoginInfo) -> ServerLoggedInContext {
|
|
|
ServerLoggedInContext {
|
|
|
ServerLoggedInContext {
|
|
|
user_name: self.user_name,
|
|
|
user_name: self.user_name,
|
|
|
@ -120,6 +131,7 @@ impl ServerLoggedOutContext { |
|
|
#[derive(Debug)]
|
|
|
#[derive(Debug)]
|
|
|
pub enum ServerContext {
|
|
|
pub enum ServerContext {
|
|
|
/// We are logged out of the server.
|
|
|
/// We are logged out of the server.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
LoggedOut(ServerLoggedOutContext),
|
|
|
LoggedOut(ServerLoggedOutContext),
|
|
|
|
|
|
|
|
|
/// We are logged in to the server.
|
|
|
/// We are logged in to the server.
|
|
|
@ -147,6 +159,7 @@ pub struct ServerLoggedInError { |
|
|
|
|
|
|
|
|
impl ServerContext {
|
|
|
impl ServerContext {
|
|
|
/// Constructs a new logged out context with the given user name.
|
|
|
/// Constructs a new logged out context with the given user name.
|
|
|
|
|
|
#[cfg(test)]
|
|
|
pub fn new(user_name: String) -> Self {
|
|
|
pub fn new(user_name: String) -> Self {
|
|
|
Self::LoggedOut(ServerLoggedOutContext {
|
|
|
Self::LoggedOut(ServerLoggedOutContext {
|
|
|
user_name,
|
|
|
user_name,
|
|
|
@ -155,6 +168,7 @@ impl ServerContext { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Attempts to record a successful login. Fails if we are already logged in.
|
|
|
/// Attempts to record a successful login. Fails if we are already logged in.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub fn login(
|
|
|
pub fn login(
|
|
|
&mut self,
|
|
|
&mut self,
|
|
|
info: ServerLoginInfo,
|
|
|
info: ServerLoginInfo,
|
|
|
@ -182,6 +196,7 @@ impl ServerContext { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Attempts to record that we logged out. Fails if we are not logged in.
|
|
|
/// Attempts to record that we logged out. Fails if we are not logged in.
|
|
|
|
|
|
#[cfg(test)]
|
|
|
pub fn logout(&mut self) -> Result<(), ServerLoggedOutError> {
|
|
|
pub fn logout(&mut self) -> Result<(), ServerLoggedOutError> {
|
|
|
if let Self::LoggedOut(context) = self {
|
|
|
if let Self::LoggedOut(context) = self {
|
|
|
return Err(ServerLoggedOutError {
|
|
|
return Err(ServerLoggedOutError {
|
|
|
@ -192,7 +207,7 @@ impl ServerContext { |
|
|
// See `mem::replace()` call in `login()`.
|
|
|
// See `mem::replace()` call in `login()`.
|
|
|
let old_self = mem::replace(self, Self::new(String::new()));
|
|
|
let old_self = mem::replace(self, Self::new(String::new()));
|
|
|
let context = match old_self {
|
|
|
let context = match old_self {
|
|
|
Self::LoggedOut(context) => unreachable!(),
|
|
|
|
|
|
|
|
|
Self::LoggedOut(_context) => unreachable!(),
|
|
|
Self::LoggedIn(context) => context,
|
|
|
Self::LoggedIn(context) => context,
|
|
|
};
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -201,6 +216,7 @@ impl ServerContext { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Returns a reference to the logged in context.
|
|
|
/// Returns a reference to the logged in context.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub fn logged_in(
|
|
|
pub fn logged_in(
|
|
|
&mut self,
|
|
|
&mut self,
|
|
|
) -> Result<&mut ServerLoggedInContext, ServerLoggedOutError> {
|
|
|
) -> Result<&mut ServerLoggedInContext, ServerLoggedOutError> {
|
|
|
@ -213,6 +229,7 @@ impl ServerContext { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/// Returns a reference to the logged out context.
|
|
|
/// Returns a reference to the logged out context.
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
pub fn logged_out(
|
|
|
pub fn logged_out(
|
|
|
&mut self,
|
|
|
&mut self,
|
|
|
) -> Result<&mut ServerLoggedOutContext, ServerLoggedInError> {
|
|
|
) -> Result<&mut ServerLoggedOutContext, ServerLoggedInError> {
|
|
|
|