From 8985b2ffd7c8d6e9cf726f630f39ce6e8f00df79 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 13 Feb 2022 21:14:33 -0600 Subject: refactor: add error handling to libraries refactor libraries from using anyhow to explicitly handling errors --- zone_zfs/src/file_system.rs | 73 ++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 44 deletions(-) (limited to 'zone_zfs/src/file_system.rs') diff --git a/zone_zfs/src/file_system.rs b/zone_zfs/src/file_system.rs index 50e3c56..83164c9 100644 --- a/zone_zfs/src/file_system.rs +++ b/zone_zfs/src/file_system.rs @@ -1,12 +1,11 @@ -use anyhow::{anyhow, Context, Result}; use std::{ ffi::{OsStr, OsString}, fmt::Display, path::PathBuf, - process::{Command, Output}, + process::Command, }; -use super::snapshot::Snapshot; +use crate::{snapshot::Snapshot, Error, Result}; #[derive(Debug)] pub struct FileSystem { @@ -22,9 +21,9 @@ impl Display for FileSystem { // pool/000.0/nkollack-1 impl TryFrom for FileSystem { - type Error = anyhow::Error; + type Error = Error; - fn try_from(value: OsString) -> Result { + fn try_from(value: OsString) -> Result { Ok(FileSystem { value, mountpoint: None, @@ -33,9 +32,9 @@ impl TryFrom for FileSystem { } impl TryFrom<&str> for FileSystem { - type Error = anyhow::Error; + type Error = Error; - fn try_from(value: &str) -> Result { + fn try_from(value: &str) -> Result { value.try_into() } } @@ -58,34 +57,24 @@ impl From for String { } } -impl TryFrom for FileSystem { - type Error = anyhow::Error; - - fn try_from(value: Output) -> Result { - std::str::from_utf8(&value.stdout)?.try_into() - } -} - impl FileSystem { pub(super) fn get_name(&self) -> Result { Ok(PathBuf::from(self.value.clone()) .file_name() - .context("Invalid path for filesystem")? + .ok_or_else(|| Error::FileSystem(format!("Invalid path for filesystem: {:?}", self)))? .to_string_lossy() .into_owned()) } pub(super) fn set_quota(&self, quota: &str) -> Result<()> { - match Command::new("zfs") + Command::new("zfs") .arg("set") .arg(format!("quota={}", quota)) .arg(&self.value) .status()? .success() - { - true => Ok(()), - false => Err(anyhow!("Failed to set a quota: {:?}", self)), - } + .then(|| ()) + .ok_or_else(|| Error::FileSystem(format!("Failed to set quota: {:?}", self))) } pub(super) fn get_snapshots(&self) -> Result> { @@ -100,7 +89,8 @@ impl FileSystem { .output()? .stdout; - String::from_utf8(stdout)? + String::from_utf8(stdout) + .map_err(|err| Error::FileSystem(format!("Failed to parse command output: {:?}", err)))? .split_whitespace() .map(|s| s.try_into()) .collect() @@ -115,7 +105,7 @@ impl FileSystem { } pub(super) fn get_file_systems() -> Result> { - let output = Command::new("zfs") + let stdout = Command::new("zfs") .arg("list") .arg("-H") .arg("-o") @@ -123,34 +113,31 @@ impl FileSystem { .output()? .stdout; - std::str::from_utf8(&output)? + String::from_utf8(stdout) + .map_err(|err| Error::FileSystem(format!("Failed to parse command output: {:?}", err)))? .split_whitespace() - .map(|fs| fs.try_into()) + .map(|s| s.try_into()) .collect() } pub fn mount(&self) -> Result<()> { - match Command::new("zfs") + Command::new("zfs") .arg("mount") .arg(&self.value) .status()? .success() - { - true => Ok(()), - false => Err(anyhow!("Failed to mount the filesystem: {:?}", self)), - } + .then(|| ()) + .ok_or_else(|| Error::FileSystem(format!("Failed to mount: {:?}", self))) } pub fn unmount(&self) -> Result<()> { - match Command::new("zfs") + Command::new("zfs") .arg("unmount") .arg(&self.value) .status()? .success() - { - true => Ok(()), - false => Err(anyhow!("Failed to unmount the filesystem: {:?}", self)), - } + .then(|| ()) + .ok_or_else(|| Error::FileSystem(format!("Failed to unmount: {:?}", self))) } pub fn destroy(&self, force: bool) -> Result<()> { @@ -161,14 +148,12 @@ impl FileSystem { } args.push(&self.value); - match Command::new("zfs") - .arg("destroy") - .args(args) - .status()? - .success() - { - true => Ok(()), - false => Err(anyhow!("Failed to destroy the filesystem: {:?}", self)), - } + Command::new("zfs") + .arg("destroy") + .args(args) + .status()? + .success() + .then(|| ()) + .ok_or_else(|| Error::FileSystem(format!("Failed to destroy: {:?}", self))) } } -- cgit v1.2.3-70-g09d2