doip_rs/client/
config.rs

1use std::net::SocketAddr;
2use getset::{CopyGetters, Getters};
3use iso13400_2::{LogicAddress, TCP_SERVER_PORT};
4
5#[derive(Clone, Debug, Getters, CopyGetters)]
6pub struct Configuration {
7    #[get = "pub"]
8    pub(crate) server_ip: String,
9    #[get_copy = "pub"]
10    pub(crate) address: LogicAddress,
11}
12
13impl Configuration {
14    pub fn new(
15        server_ip: &str,
16        address: LogicAddress,
17    ) -> Option<Self> {
18        match format!("{}:{}", server_ip, TCP_SERVER_PORT).parse::<SocketAddr>() {
19            Ok(_) => Some(Self {
20                server_ip: server_ip.to_owned(),
21                address
22            }),
23            Err(_) => None,
24        }
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use iso13400_2::LogicAddress;
31    use super::Configuration;
32
33    #[test]
34    fn test_configuration() {
35        let cfg = Configuration::new("127.0.0.1", LogicAddress::from(0x0E00)).unwrap();
36        assert_eq!(cfg.server_ip(), "127.0.0.1");
37        assert_eq!(cfg.address(), LogicAddress::from(0x0E00));
38    }
39}