arachnid_cli/config/kinds/
network.rs1use 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 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 pub const fn address(&self) -> &NetAddr {
41 &self.address
42 }
43 pub const fn address_mut(&mut self) -> &mut NetAddr {
45 &mut self.address
46 }
47 pub fn basepath(&self) -> &str {
49 &self.basepath
50 }
51 pub const fn max_connections(&self) -> u16 {
53 self.max_connections
54 }
55 pub const fn max_connections_mut(&mut self) -> &mut u16 {
57 &mut self.max_connections
58 }
59 pub fn should_open(self) -> Self {
61 Self { open: true, ..self }
62 }
63 pub fn should_not_open(self) -> Self {
65 Self {
66 open: false,
67 ..self
68 }
69 }
70 pub fn with_address(self, address: NetAddr) -> Self {
72 Self { address, ..self }
73 }
74 pub fn with_basepath(self, basepath: impl ToString) -> Self {
76 Self {
77 basepath: basepath.to_string(),
78 ..self
79 }
80 }
81 pub fn set_address(&mut self, address: NetAddr) {
83 self.address = address
84 }
85 pub fn set_basepath<T>(&mut self, basepath: T)
87 where
88 T: ToString,
89 {
90 self.basepath = basepath.to_string()
91 }
92 pub const fn set_max_connections(&mut self, max_connections: u16) {
94 self.max_connections = max_connections
95 }
96 pub fn as_socket_addr(&self) -> core::net::SocketAddr {
98 self.address.as_socket_addr()
99 }
100 pub async fn bind(&self) -> std::io::Result<tokio::net::TcpListener> {
102 self.address().bind().await
103 }
104 pub fn host(&self) -> &str {
106 self.address().host()
107 }
108 pub fn ip(&self) -> core::net::IpAddr {
110 self.address().ip()
111 }
112 pub fn open(&self) -> bool {
114 self.open
115 }
116 pub fn port(&self) -> u16 {
118 self.address.port
119 }
120 pub fn set_port(&mut self, port: u16) {
122 self.address.port = port;
123 }
124 pub fn with_port(self, port: u16) -> Self {
126 Self {
127 address: NetAddr {
128 port,
129 ..self.address
130 },
131 ..self
132 }
133 }
134 pub fn set_host(&mut self, host: impl ToString) {
136 self.address.host = host.to_string();
137 }
138 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}