nutt_conf_parser/
lib.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 serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NuttConfig {
    service: HashMap<String, ServiceConfig>,
}

impl NuttConfig {
    pub fn new() -> Self {
        Self {
            service: HashMap::new(),
        }
    }

    pub fn get_service_config(&self, name: &str) -> Option<ServiceConfig> {
        if let Some(conf) = self.service.get(name) {
            return Some(conf.clone());
        }
        None
    }

    pub fn push_service_config(&mut self,name: &str, service_config: ServiceConfig) {
        self.service.insert(name.to_string(), service_config);
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ServiceConfig {
    local_host: String,
    local_port: u16,
}

impl ServiceConfig {
    pub fn new(local_host: &str, local_port: u16) -> Self {
        Self {
            local_host: local_host.to_string(),
            local_port,
        }
    }

    pub fn get_addr(&self) -> (&str, u16) {
        (&self.local_host, self.local_port)
    }
}