pzzld-server 0.0.2

A production ready server optimized for WASM applications
Documentation
/*
    Appellation: context <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::config::Scope;
use core::net::SocketAddr;

#[derive(Clone, Debug, Hash, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize)]
pub struct ServerContext {
    /// The address the server is bound to
    pub(crate) addr: SocketAddr,
    /// The scope of the server
    pub(crate) scope: Scope,
}

impl ServerContext {
    pub fn new(addr: SocketAddr, scope: Scope) -> Self {
        Self { addr, scope }
    }

    pub fn localhost(port: u16, scope: Scope) -> Self {
        Self {
            addr: SocketAddr::from(([127, 0, 0, 1], port)),
            scope,
        }
    }
    /// Returns a copy of the address the server is bound to
    pub fn addr(&self) -> SocketAddr {
        self.addr
    }
    /// Returns a reference to the [Scope] of the server
    pub const fn scope(&self) -> &Scope {
        &self.scope
    }
    /// Returns a mutable reference to the [Scope] of the server
    pub fn scope_mut(&mut self) -> &mut Scope {
        &mut self.scope
    }

    setwith! {
        addr: SocketAddr,
        scope: Scope,
    }
    /// initialize a new listener, bound to the configured address
    pub async fn listen(&self) -> tokio::net::TcpListener {
        tokio::net::TcpListener::bind(&self.addr).await.unwrap()
    }
}