support_kit/network/
network_config.rs

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
use std::net::SocketAddr;

use serde::{Deserialize, Serialize};

use crate::NetworkInitError;

use super::{NetworkHost, NetworkPort};

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, bon::Builder)]
pub struct NetworkConfig {
    #[serde(default)]
    #[builder(default, into)]
    pub host: NetworkHost,

    #[serde(default)]
    #[builder(default, into)]
    pub port: NetworkPort,
}

impl NetworkConfig {
    pub fn address(&self) -> crate::Result<SocketAddr> {
        Ok(format!("{}:{}", self.host, self.port)
            .parse()
            .map_err(|error| NetworkInitError::from(error))?)
    }
}

impl From<&str> for NetworkConfig {
    fn from(host: &str) -> Self {
        NetworkConfig::builder().host(host).port(80).build()
    }
}

impl<T, U> From<(T, U)> for NetworkConfig
where
    T: AsRef<str>,
    U: Into<NetworkPort>,
{
    fn from((host, port): (T, U)) -> Self {
        NetworkConfig::builder()
            .host(host.as_ref())
            .port(port)
            .build()
    }
}