pzzld-server 0.0.2

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

#[derive(
    Clone,
    Debug,
    Default,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    serde::Deserialize,
    serde::Serialize,
)]
#[serde(default)]
pub struct NetworkConfig {
    pub(crate) address: NetAddr,
    pub(crate) basepath: Option<String>,
    pub(crate) max_connections: Option<u16>,
    pub(crate) open: bool,
}

impl NetworkConfig {
    pub fn new() -> Self {
        Self {
            address: NetAddr::default(),
            basepath: None,
            max_connections: None,
            open: false,
        }
    }

    getter! {
        address: NetAddr,
        open: bool,
    }

    setwith! {
        address: NetAddr,
        open: bool,
    }
    /// Returns an instance of the address as a [SocketAddr](core::net::SocketAddr)
    pub fn as_socket_addr(&self) -> core::net::SocketAddr {
        self.address.as_socket_addr()
    }

    pub async fn bind(&self) -> std::io::Result<tokio::net::TcpListener> {
        self.address().bind().await
    }
    /// Returns the host of the address
    pub fn host(&self) -> &str {
        self.address().host()
    }
    /// Returns the ip of the address
    pub fn ip(&self) -> core::net::IpAddr {
        self.address().ip()
    }
    /// Returns the port of the address
    pub fn port(&self) -> u16 {
        self.address.port
    }

    pub fn set_port(&mut self, port: u16) {
        self.address.port = port;
    }

    pub fn with_port(self, port: u16) -> Self {
        Self {
            address: NetAddr {
                port,
                ..self.address
            },
            ..self
        }
    }

    pub fn set_host(&mut self, host: impl ToString) {
        self.address.host = host.to_string();
    }

    pub fn with_host(self, host: impl ToString) -> Self {
        Self {
            address: self.address.with_host(host),
            ..self
        }
    }

    pub fn set_basepath(&mut self, basepath: impl ToString) {
        self.basepath = Some(basepath.to_string());
    }

    pub fn with_basepath(self, basepath: impl ToString) -> Self {
        Self {
            basepath: Some(basepath.to_string()),
            ..self
        }
    }
}