arachnid_cli/config/kinds/
network.rs

1/*
2    Appellation: server <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::config::NetAddr;
6
7fn _default_basepath() -> String {
8    crate::config::DEFAULT_BASEPATH.to_string()
9}
10
11fn _default_max_connections() -> u16 {
12    15
13}
14
15#[derive(
16    Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
17)]
18#[serde(default, deny_unknown_fields, rename_all = "snake_case")]
19pub struct NetworkConfig {
20    // #[serde(flatten)]
21    pub(crate) address: NetAddr,
22    #[serde(default = "_default_basepath")]
23    pub(crate) basepath: String,
24    #[serde(default = "_default_max_connections")]
25    pub(crate) max_connections: u16,
26    pub(crate) open: bool,
27}
28
29impl NetworkConfig {
30    pub fn new() -> Self {
31        Self {
32            address: NetAddr::default(),
33            basepath: _default_basepath(),
34            max_connections: _default_max_connections(),
35            open: false,
36        }
37    }
38
39    /// returns a reference to the address
40    pub const fn address(&self) -> &NetAddr {
41        &self.address
42    }
43    /// returns a mutable reference to the address
44    pub const fn address_mut(&mut self) -> &mut NetAddr {
45        &mut self.address
46    }
47    /// returns a reference to the basepath as a str
48    pub fn basepath(&self) -> &str {
49        &self.basepath
50    }
51    /// returns the maximum number of connections
52    pub const fn max_connections(&self) -> u16 {
53        self.max_connections
54    }
55    /// returns a mutable reference to the maximum number of connections
56    pub const fn max_connections_mut(&mut self) -> &mut u16 {
57        &mut self.max_connections
58    }
59    /// consumes the current instance to create another that should open by default
60    pub fn should_open(self) -> Self {
61        Self { open: true, ..self }
62    }
63    /// consumes the instance to create another that should not open by default
64    pub fn should_not_open(self) -> Self {
65        Self {
66            open: false,
67            ..self
68        }
69    }
70    /// consumes the current instance to create another with the given address
71    pub fn with_address(self, address: NetAddr) -> Self {
72        Self { address, ..self }
73    }
74    /// consumes the current instance to create another with the given basepath
75    pub fn with_basepath(self, basepath: impl ToString) -> Self {
76        Self {
77            basepath: basepath.to_string(),
78            ..self
79        }
80    }
81    /// update the current address and return a mutable reference to self
82    pub fn set_address(&mut self, address: NetAddr) {
83        self.address = address
84    }
85    /// update the current basepath and return a mutable reference to self
86    pub fn set_basepath<T>(&mut self, basepath: T)
87    where
88        T: ToString,
89    {
90        self.basepath = basepath.to_string()
91    }
92    /// update the maximum number of connections and return a mutable reference to self
93    pub const fn set_max_connections(&mut self, max_connections: u16) {
94        self.max_connections = max_connections
95    }
96    /// Returns an instance of the address as a [SocketAddr](core::net::SocketAddr)
97    pub fn as_socket_addr(&self) -> core::net::SocketAddr {
98        self.address.as_socket_addr()
99    }
100    /// Binds the address to a [TcpListener](tokio::net::TcpListener)
101    pub async fn bind(&self) -> std::io::Result<tokio::net::TcpListener> {
102        self.address().bind().await
103    }
104    /// Returns the host of the address
105    pub fn host(&self) -> &str {
106        self.address().host()
107    }
108    /// Returns the ip of the address
109    pub fn ip(&self) -> core::net::IpAddr {
110        self.address().ip()
111    }
112    /// Determines if the server should open the link in a browser
113    pub fn open(&self) -> bool {
114        self.open
115    }
116    /// Returns the port of the address
117    pub fn port(&self) -> u16 {
118        self.address.port
119    }
120    /// Sets the port of the address
121    pub fn set_port(&mut self, port: u16) {
122        self.address.port = port;
123    }
124    /// consumes the current instance and returns a new instance with the port set
125    pub fn with_port(self, port: u16) -> Self {
126        Self {
127            address: NetAddr {
128                port,
129                ..self.address
130            },
131            ..self
132        }
133    }
134    /// Sets the host of the address
135    pub fn set_host(&mut self, host: impl ToString) {
136        self.address.host = host.to_string();
137    }
138    /// consumes the current instance and returns a new instance with the host set
139    pub fn with_host(self, host: impl ToString) -> Self {
140        Self {
141            address: self.address.with_host(host),
142            ..self
143        }
144    }
145}
146
147impl core::fmt::Debug for NetworkConfig {
148    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
149        f.write_str(serde_json::to_string_pretty(self).unwrap().as_str())
150    }
151}
152
153impl core::fmt::Display for NetworkConfig {
154    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
155        f.write_str(serde_json::to_string(self).unwrap().as_str())
156    }
157}