use crate::session::{
in_session::InSession, latent_state::LatentState, logon_state::LogonState,
logout_state::LogoutState, not_session_time::NotSessionTime, pending_timeout::PendingTimeout,
resend_state::ResendState,
};
use delegate::delegate;
use tokio::sync::mpsc::UnboundedReceiver;
#[derive(Debug, Clone)]
pub enum SessionStateEnum {
InSession(InSession),
LatentState(LatentState),
LogonState(LogonState),
LogoutState(LogoutState),
NotSessionTime(NotSessionTime),
ResendState(ResendState),
PendingTimeout(PendingTimeout),
}
#[derive(Debug, Clone)]
pub enum AfterPendingTimeout {
InSession(InSession),
ResendState(ResendState),
}
impl Default for SessionStateEnum {
fn default() -> Self {
Self::new_latent_state()
}
}
impl std::fmt::Display for SessionStateEnum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InSession(is) => write!(f, "{is}"),
Self::LatentState(ls) => write!(f, "{ls}"),
Self::LogoutState(ls) => write!(f, "{ls}"),
Self::LogonState(ls) => write!(f, "{ls}"),
Self::NotSessionTime(nst) => write!(f, "{nst}"),
Self::ResendState(rs) => write!(f, "{rs}"),
Self::PendingTimeout(ps) => write!(f, "{ps}"),
}
}
}
impl SessionState for SessionStateEnum {
delegate! {
to match self {
Self::InSession(is) => is,
Self::LatentState(ls) => ls,
Self::LogoutState(ls) => ls,
Self::LogonState(ls) => ls,
Self::NotSessionTime(nst) => nst,
Self::ResendState(rs) => rs,
Self::PendingTimeout(ps) => ps,
} {
fn is_logged_on(&self) -> bool;
fn is_connected(&self) -> bool;
fn is_session_time(&self) -> bool;
}
}
}
impl SessionStateEnum {
pub fn new_latent_state() -> Self {
Self::LatentState(LatentState::default())
}
#[allow(clippy::unused_async)]
pub async fn new_in_session() -> Self {
Self::InSession(InSession::default())
}
pub fn new_logout_state() -> Self {
Self::LogoutState(LogoutState::default())
}
pub fn new_logon_state() -> Self {
Self::LogonState(LogonState::default())
}
pub fn new_not_session_time() -> Self {
Self::NotSessionTime(NotSessionTime::default())
}
pub fn new_resend_state() -> Self {
Self::ResendState(ResendState::default())
}
pub fn new_pending_timeout_resend_state() -> Self {
Self::PendingTimeout(PendingTimeout {
session_state: AfterPendingTimeout::ResendState(ResendState::default()),
})
}
pub fn new_pending_timeout_in_session() -> Self {
Self::PendingTimeout(PendingTimeout {
session_state: AfterPendingTimeout::InSession(InSession::default()),
})
}
}
pub struct StateMachine {
pub state: SessionStateEnum,
pub pending_stop: bool,
pub stopped: bool,
pub notify_on_in_session_time: Option<UnboundedReceiver<()>>,
}
impl StateMachine {
pub fn is_logged_on(&self) -> bool {
self.state.is_logged_on()
}
pub fn is_connected(&self) -> bool {
self.state.is_connected()
}
pub fn is_session_time(&self) -> bool {
self.state.is_session_time()
}
}
pub trait SessionState: std::fmt::Display {
fn is_logged_on(&self) -> bool;
fn is_connected(&self) -> bool;
fn is_session_time(&self) -> bool;
}
#[derive(Default, Debug, Clone)]
pub struct InSessionTime;
impl InSessionTime {
pub fn is_session_time(&self) -> bool {
true
}
}
#[derive(Default, Debug, Clone)]
pub struct Connected;
impl Connected {
pub fn is_connected(&self) -> bool {
true
}
pub fn is_session_time(&self) -> bool {
true
}
}
#[derive(Default, Debug, Clone)]
pub struct ConnectedNotLoggedOn {
pub connected: Connected,
}
impl ConnectedNotLoggedOn {
delegate! {
to self.connected {
pub fn is_connected(&self) -> bool;
pub fn is_session_time(&self) -> bool;
}
}
pub fn is_logged_on(&self) -> bool {
false
}
#[allow(clippy::unused_async)]
pub async fn shutdown_now(&self) {}
}
#[derive(Default, Debug, Clone)]
pub struct LoggedOn {
pub connected: Connected,
}
impl LoggedOn {
delegate! {
to self.connected {
pub fn is_connected(&self) -> bool;
pub fn is_session_time(&self) -> bool;
}
}
pub fn is_logged_on(&self) -> bool {
true
}
}