scsys_config/services/
network.rs

1/*
2    Appellation: server <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::NetworkAddr;
6
7#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
8#[cfg_attr(
9    feature = "serde",
10    derive(serde::Deserialize, serde::Serialize),
11    serde(default, rename_all = "snake_case")
12)]
13pub struct NetworkConfig {
14    pub(crate) address: NetworkAddr,
15    pub(crate) basepath: Option<String>,
16    pub(crate) max_connections: Option<u16>,
17    pub(crate) open: bool,
18}
19
20impl NetworkConfig {
21    pub fn new() -> Self {
22        Self {
23            address: NetworkAddr::default(),
24            basepath: None,
25            max_connections: None,
26            open: false,
27        }
28    }
29    /// toggle the open on startup flag
30    pub fn open_on_startup(self) -> Self {
31        Self { open: true, ..self }
32    }
33    /// returns an immutable reference to the network address
34    pub const fn address(&self) -> &NetworkAddr {
35        &self.address
36    }
37    /// returns a mutable reference to the network address
38    pub const fn address_mut(&mut self) -> &mut NetworkAddr {
39        &mut self.address
40    }
41    /// returns a reference to the basepath; if any.
42    pub fn basepath(&self) -> Option<&String> {
43        self.basepath.as_ref()
44    }
45    /// returns a mutable reference to the basepath; if any.
46    pub fn basepath_mut(&mut self) -> Option<&mut String> {
47        self.basepath.as_mut()
48    }
49    /// returns the maximum number of connections; if any.
50    pub fn max_connections(&self) -> Option<u16> {
51        self.max_connections
52    }
53    /// returns true if the application should automatically open the browser upon startup
54    pub fn open(&self) -> bool {
55        self.open
56    }
57    /// converts the network address into a [`SocketAddr`](core::net::SocketAddr)
58    pub fn as_socket_addr(&self) -> core::net::SocketAddr {
59        // self.address.into_iter().map(|i| i.as_socket_addr())
60        self.address().as_socket_addr()
61    }
62    /// Returns the host of the address
63    pub fn host(&self) -> &str {
64        self.address().host()
65    }
66    /// Returns the ip of the address
67    pub fn ip(&self) -> core::net::IpAddr {
68        self.address().ip()
69    }
70    /// Returns the port of the address
71    pub fn port(&self) -> u16 {
72        self.address().port()
73    }
74    /// update the address then return a mutable reference to the current instance
75    pub fn set_address(&mut self, address: NetworkAddr) -> &mut Self {
76        self.address = address;
77        self
78    }
79    /// consumes the current instance to create another with the given address
80    pub fn with_address(self, address: NetworkAddr) -> Self {
81        Self { address, ..self }
82    }
83    /// update the basepath then return a mutable reference to the current instance
84    pub fn set_basepath<T: ToString>(&mut self, basepath: T) -> &mut Self {
85        self.basepath = Some(basepath.to_string());
86        self
87    }
88    /// consumes the current instance to create another with the given basepath
89    pub fn with_basepath<T: ToString>(self, basepath: T) -> Self {
90        Self {
91            basepath: Some(basepath.to_string()),
92            ..self
93        }
94    }
95    /// update the max_connections then return a mutable reference to the current instance
96    pub fn set_max_connections(&mut self, max_connections: u16) -> &mut Self {
97        self.max_connections = Some(max_connections);
98        self
99    }
100    /// consumes the current instance to create another with the given max_connections
101    pub fn with_max_connections(self, max_connections: u16) -> Self {
102        Self {
103            max_connections: Some(max_connections),
104            ..self
105        }
106    }
107    /// update the open flag then return a mutable reference to the current instance
108    pub fn set_open(&mut self, open: bool) -> &mut Self {
109        self.open = open;
110        self
111    }
112    /// consumes the current instance to create another with the given open flag
113    pub fn with_open(self, open: bool) -> Self {
114        Self { open, ..self }
115    }
116    /// updates the hostname of the current address
117    pub fn set_host<T: ToString>(&mut self, host: T) -> &mut Self {
118        self.address_mut().set_host(host);
119        self
120    }
121    /// update the host then return a mutable reference to the current instance
122    pub fn with_host<T: ToString>(self, host: T) -> Self {
123        Self {
124            address: self.address.with_host(host),
125            ..self
126        }
127    }
128    /// update the port then return a mutable reference to the current instance
129    pub fn set_port(&mut self, port: u16) -> &mut Self {
130        self.address_mut().set_port(port);
131        self
132    }
133    /// consumes the current instance to create another with the given port
134    pub fn with_port(self, port: u16) -> Self {
135        Self {
136            address: NetworkAddr {
137                port,
138                ..self.address
139            },
140            ..self
141        }
142    }
143}