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
|
use figment::Figment;
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use crate::{Error, Result};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Config {
pub ip_address: IpAddr,
pub port: u16,
pub zfs: zone_zfs::Config,
pub nspawn: zone_nspawn::Config,
}
impl Default for Config {
fn default() -> Self {
Self {
ip_address: zone_core::DEFAULT_IP_ADDRESS,
port: zone_core::DEFAULT_PORT,
zfs: Default::default(),
nspawn: Default::default(),
}
}
}
impl TryFrom<Figment> for Config {
type Error = Error;
fn try_from(value: Figment) -> Result<Self> {
value.extract().map_err(Into::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"
[nspawn]
network_configs_path = "/etc/zoned/network.d"
"#,
)?;
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,
zfs: zone_zfs::Config {
quota: 42_000_000u64.into(),
pool_name: String::from("fool"),
mountpoint: PathBuf::from("/mnt"),
},
nspawn: zone_nspawn::Config {
network_configs_path: Some(PathBuf::from("/etc/zoned/network.d")),
},
}
);
Ok(())
});
}
}
|