aboutsummaryrefslogtreecommitdiffstats
path: root/zone_zfs/src/lib.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-02-01 01:52:04 -0600
committerToby Vincent <tobyv13@gmail.com>2022-02-01 01:52:04 -0600
commit1a5a7eed4df2ee7aebdb752b78e7da78e5f15d28 (patch)
treed6a4575aad284735c1fbb8ceb98b4fe80798cfdc /zone_zfs/src/lib.rs
parent493d909da8c6540b63ca2f461a9d2462715ebd0e (diff)
fix: integrated figment with rocket
Co-authored-by: Neil Kollack <nkollack@gmail.com>
Diffstat (limited to 'zone_zfs/src/lib.rs')
-rw-r--r--zone_zfs/src/lib.rs58
1 files changed, 51 insertions, 7 deletions
diff --git a/zone_zfs/src/lib.rs b/zone_zfs/src/lib.rs
index d88aba5..7b3342f 100644
--- a/zone_zfs/src/lib.rs
+++ b/zone_zfs/src/lib.rs
@@ -1,12 +1,52 @@
+use self::file_system::FileSystem;
use anyhow::Result;
+use figment::{providers::Serialized, Figment, Metadata, Profile, Provider};
+use serde::{Deserialize, Serialize};
use std::path::PathBuf;
-use self::file_system::FileSystem;
pub mod file_system;
pub mod snapshot;
-pub fn create_file_system(base_fs_name: String, name: String, quota: &str) -> Result<FileSystem> {
+#[derive(Debug, Deserialize, Serialize)]
+pub struct Config {
+ pub quota: String,
+}
+
+impl Default for Config {
+ fn default() -> Self {
+ Config {
+ quota: "16G".to_string(),
+ }
+ }
+}
+
+impl Config {
+ pub fn from<T: Provider>(provider: T) -> Result<Config, figment::Error> {
+ Figment::from(provider).extract()
+ }
+
+ // Provide a default provider, a `Figment`.
+ pub fn figment() -> Figment {
+ Figment::from(Config::default())
+ }
+}
+
+impl Provider for Config {
+ fn metadata(&self) -> Metadata {
+ Metadata::named("ZFS Config")
+ }
+
+ fn data(&self) -> Result<figment::value::Map<Profile, figment::value::Dict>, figment::Error> {
+ Serialized::defaults(Config::default()).data()
+ }
+}
+
+pub fn create_file_system(
+ base_fs_name: String,
+ name: String,
+ config: &Config,
+) -> Result<FileSystem> {
let fs = FileSystem::get_file_systems()
.unwrap()
.into_iter()
@@ -24,12 +64,16 @@ pub fn create_file_system(base_fs_name: String, name: String, quota: &str) -> Re
let mut mountpoint = fs.value;
mountpoint.push(name);
- let cloned_fs = snapshot.clone_into_file_system(
- snapshot.file_system.get_name()?,
- Some(PathBuf::from(mountpoint)),
- ).unwrap();
+ let cloned_fs = snapshot
+ .clone_into_file_system(
+ snapshot.file_system.get_name()?,
+ Some(PathBuf::from(mountpoint)),
+ )
+ .unwrap();
- cloned_fs.set_quota(quota).expect("Failed to set quota");
+ cloned_fs
+ .set_quota(&config.quota)
+ .expect("Failed to set quota");
Ok(cloned_fs)
}