diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-02-13 21:14:33 -0600 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-02-13 21:14:33 -0600 |
commit | 8985b2ffd7c8d6e9cf726f630f39ce6e8f00df79 (patch) | |
tree | 3e632e691378e36d389393d80049fa81c1b8f9cf /zone_zfs/src/lib.rs | |
parent | 96e571eca2991a19fc9bcdc26e451c4fad5bc791 (diff) |
refactor: add error handling to libraries
refactor libraries from using anyhow to explicitly handling errors
Diffstat (limited to 'zone_zfs/src/lib.rs')
-rw-r--r-- | zone_zfs/src/lib.rs | 62 |
1 files changed, 38 insertions, 24 deletions
diff --git a/zone_zfs/src/lib.rs b/zone_zfs/src/lib.rs index 53947a6..c430cba 100644 --- a/zone_zfs/src/lib.rs +++ b/zone_zfs/src/lib.rs @@ -1,13 +1,32 @@ use self::file_system::FileSystem; -use anyhow::Result; -use figment::{providers::{Serialized, Toml, Env, Format}, Figment, Metadata, Profile, Provider}; +use figment::{ + providers::{Env, Format, Serialized, Toml}, + Figment, Metadata, Profile, Provider, +}; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; +use std::{io, path::PathBuf, result}; pub mod file_system; pub mod snapshot; +type Result<T> = result::Result<T, Error>; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("ZFS error")] + ZFS(String), + + #[error("Snapshot Error: {0:?}")] + Snapshot(String), + + #[error("File System Error: {0:?}")] + FileSystem(String), + + #[error("IO Error: Failed to run command")] + IO(#[from] io::Error), +} + #[derive(Debug, Deserialize, Serialize)] pub struct Config { pub quota: String, @@ -22,11 +41,10 @@ impl Default for Config { } impl Config { - pub fn from<T: Provider>(provider: T) -> Result<Config, figment::Error> { + pub fn from<T: Provider>(provider: T) -> result::Result<Config, figment::Error> { Figment::from(provider).extract() } - // Provide a default provider, a `Figment`. pub fn figment() -> Figment { Figment::from(Config::default()) .merge(Toml::file(Env::var_or("ZFS_CONFIG", "ZFS.toml")).nested()) @@ -39,7 +57,9 @@ impl Provider for Config { Metadata::named("ZFS Config") } - fn data(&self) -> Result<figment::value::Map<Profile, figment::value::Dict>, figment::Error> { + fn data( + &self, + ) -> result::Result<figment::value::Map<Profile, figment::value::Dict>, figment::Error> { Serialized::defaults(Config::default()).data() } } @@ -49,33 +69,27 @@ pub fn create_file_system( name: String, config: &Config, ) -> Result<FileSystem> { - let fs = FileSystem::get_file_systems() - .unwrap() + let fs = FileSystem::get_file_systems()? .into_iter() - .find(|fs| match fs.get_name() { - Ok(name) => name == base_fs_name, - Err(_) => false, + .find_map(|fs| match fs.get_name() { + Ok(n) if n == base_fs_name => Some(fs), + _ => None, }) - .unwrap_or_else(|| todo!("Handle!")); + .ok_or_else(|| Error::FileSystem("No ".to_string()))?; let snapshot = fs - .get_latest_snapshot() - .expect("No snapshot found") - .unwrap(); + .get_latest_snapshot()? + .ok_or_else(|| Error::Snapshot("No snapshot found".to_string()))?; 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)), + )?; - cloned_fs - .set_quota(&config.quota) - .expect("Failed to set quota"); + cloned_fs.set_quota(&config.quota)?; Ok(cloned_fs) } |