support_kit/network/
network_config.rs

1use std::net::SocketAddr;
2
3use serde::{Deserialize, Serialize};
4
5use crate::NetworkInitError;
6
7use super::{NetworkHost, NetworkPort};
8
9#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, bon::Builder)]
10pub struct NetworkConfig {
11    #[serde(default)]
12    #[builder(default, into)]
13    pub host: NetworkHost,
14
15    #[serde(default)]
16    #[builder(default, into)]
17    pub port: NetworkPort,
18}
19
20impl NetworkConfig {
21    pub fn address(&self) -> crate::Result<SocketAddr> {
22        Ok(format!("{}:{}", self.host, self.port)
23            .parse()
24            .map_err(|error| NetworkInitError::from(error))?)
25    }
26}
27
28impl From<&str> for NetworkConfig {
29    fn from(host: &str) -> Self {
30        NetworkConfig::builder().host(host).port(80).build()
31    }
32}
33
34impl<T, U> From<(T, U)> for NetworkConfig
35where
36    T: AsRef<str>,
37    U: Into<NetworkPort>,
38{
39    fn from((host, port): (T, U)) -> Self {
40        NetworkConfig::builder()
41            .host(host.as_ref())
42            .port(port)
43            .build()
44    }
45}