diff options
Diffstat (limited to 'zone_zfs/src/config.rs')
-rw-r--r-- | zone_zfs/src/config.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/zone_zfs/src/config.rs b/zone_zfs/src/config.rs new file mode 100644 index 0000000..f3dc390 --- /dev/null +++ b/zone_zfs/src/config.rs @@ -0,0 +1,48 @@ +use bytesize::ByteSize; +use figment::{ + error::Result, + providers::{Env, Format, Serialized, Toml}, + value::{Dict, Map}, + Figment, Metadata, Profile, Provider, +}; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Config { + pub quota: ByteSize, + pub pool_name: String, + pub mountpoint: PathBuf, +} + +impl Default for Config { + fn default() -> Self { + Config { + quota: ByteSize::gb(16), + pool_name: String::from("pool"), + mountpoint: PathBuf::from("/svr"), + } + } +} + +impl Config { + pub fn from<T: Provider>(provider: T) -> Result<Config> { + Figment::from(provider).extract() + } + + pub fn figment() -> Figment { + Figment::from(Config::default()) + .merge(Toml::file(Env::var_or("ZFS_CONFIG", "ZFS.toml")).nested()) + .merge(Env::prefixed("ZFS_").global()) + } +} + +impl Provider for Config { + fn metadata(&self) -> Metadata { + Metadata::named("ZFS Config") + } + + fn data(&self) -> Result<Map<Profile, Dict>> { + Serialized::defaults(Config::default()).data() + } +} |