aboutsummaryrefslogtreecommitdiffstats
path: root/zoned/src/config.rs
blob: 53c0bfce9efe63cb7755efeef3fee29e881b1553 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use figment::Figment;
use serde::{Deserialize, Serialize};
use std::{
    net::{IpAddr, SocketAddr},
    path::PathBuf,
    sync::Arc,
};

use crate::{Error, Result};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct Config {
    pub ip_address: IpAddr,
    pub port: u16,
    pub init_script: Option<PathBuf>,
    pub zfs: zone_zfs::Config,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            ip_address: zone_core::DEFAULT_IP_ADDRESS,
            port: zone_core::DEFAULT_PORT,
            init_script: None,
            zfs: Default::default(),
        }
    }
}

impl TryFrom<Figment> for Config {
    type Error = Error;

    fn try_from(value: Figment) -> Result<Self> {
        value.extract().map_err(Into::into)
    }
}

impl From<Config> for SocketAddr {
    fn from(val: Config) -> Self {
        SocketAddr::from((val.ip_address, val.port))
    }
}

impl Config {
    pub fn into_arc(self) -> Arc<Self> {
        self.into()
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use figment::providers::{Format, Serialized, Toml};

    use super::*;

    #[test]
    fn defaults() {
        figment::Jail::expect_with(|jail| {
            jail.create_file(
                "Config.toml",
                r#"
                ip_address = "127.0.0.1"
                port = 8000

                [zfs]
                quota = "16G"
                pool_name = "pool"
                mountpoint = "/srv"
                "#,
            )?;

            let config: Config = Figment::from(Serialized::defaults(Config::default()))
                .merge(Toml::file("Config.toml"))
                .extract()?;

            assert_eq!(config, Config::default());

            Ok(())
        });
    }

    #[test]
    fn custom() {
        figment::Jail::expect_with(|jail| {
            jail.create_file(
                "Config.toml",
                r#"
                ip_address = "192.168.1.1"
                port = 6555

                [zfs]
                quota = 42000000
                pool_name = "fool"
                mountpoint = "/mnt"
                "#,
            )?;

            let config: Config = Figment::from(Serialized::defaults(Config::default()))
                .merge(Toml::file("Config.toml"))
                .extract()?;

            assert_eq!(
                config,
                Config {
                    ip_address: [192, 168, 1, 1].into(),
                    port: 6555,
                    init_script: None,
                    zfs: zone_zfs::Config {
                        quota: 42_000_000u64.into(),
                        pool_name: PathBuf::from("fool"),
                        mountpoint: PathBuf::from("/mnt"),
                    },
                }
            );

            Ok(())
        });
    }
}