scsys_config/services/
network.rs1use 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 pub fn open_on_startup(self) -> Self {
31 Self { open: true, ..self }
32 }
33 pub const fn address(&self) -> &NetworkAddr {
35 &self.address
36 }
37 pub const fn address_mut(&mut self) -> &mut NetworkAddr {
39 &mut self.address
40 }
41 pub fn basepath(&self) -> Option<&String> {
43 self.basepath.as_ref()
44 }
45 pub fn basepath_mut(&mut self) -> Option<&mut String> {
47 self.basepath.as_mut()
48 }
49 pub fn max_connections(&self) -> Option<u16> {
51 self.max_connections
52 }
53 pub fn open(&self) -> bool {
55 self.open
56 }
57 pub fn as_socket_addr(&self) -> core::net::SocketAddr {
59 self.address().as_socket_addr()
61 }
62 pub fn host(&self) -> &str {
64 self.address().host()
65 }
66 pub fn ip(&self) -> core::net::IpAddr {
68 self.address().ip()
69 }
70 pub fn port(&self) -> u16 {
72 self.address().port()
73 }
74 pub fn set_address(&mut self, address: NetworkAddr) -> &mut Self {
76 self.address = address;
77 self
78 }
79 pub fn with_address(self, address: NetworkAddr) -> Self {
81 Self { address, ..self }
82 }
83 pub fn set_basepath<T: ToString>(&mut self, basepath: T) -> &mut Self {
85 self.basepath = Some(basepath.to_string());
86 self
87 }
88 pub fn with_basepath<T: ToString>(self, basepath: T) -> Self {
90 Self {
91 basepath: Some(basepath.to_string()),
92 ..self
93 }
94 }
95 pub fn set_max_connections(&mut self, max_connections: u16) -> &mut Self {
97 self.max_connections = Some(max_connections);
98 self
99 }
100 pub fn with_max_connections(self, max_connections: u16) -> Self {
102 Self {
103 max_connections: Some(max_connections),
104 ..self
105 }
106 }
107 pub fn set_open(&mut self, open: bool) -> &mut Self {
109 self.open = open;
110 self
111 }
112 pub fn with_open(self, open: bool) -> Self {
114 Self { open, ..self }
115 }
116 pub fn set_host<T: ToString>(&mut self, host: T) -> &mut Self {
118 self.address_mut().set_host(host);
119 self
120 }
121 pub fn with_host<T: ToString>(self, host: T) -> Self {
123 Self {
124 address: self.address.with_host(host),
125 ..self
126 }
127 }
128 pub fn set_port(&mut self, port: u16) -> &mut Self {
130 self.address_mut().set_port(port);
131 self
132 }
133 pub fn with_port(self, port: u16) -> Self {
135 Self {
136 address: NetworkAddr {
137 port,
138 ..self.address
139 },
140 ..self
141 }
142 }
143}