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