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,
}
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
}
pub fn host(&self) -> &str {
self.address().host()
}
pub fn ip(&self) -> core::net::IpAddr {
self.address().ip()
}
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
}
}
}