From 592d1a55f0c05c1dcba58261f327066b4832a703 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Fri, 28 Aug 2020 18:44:45 -0700 Subject: Use anyhow for error handling This patch changes our error handling approach from the ground up: instead of having a globally used Error enum that contains variants for all possible errors, we now use anyhow's Error type. This approach is more dynamic (and not statically typed), but it allows for more fine grained error messages and overall more user-friendly error reporting. Overall it also is a net simplification. While we have one dynamic cast now, in order to be able to handle erroneous password/PIN entries correctly, that is considered a reasonable compromise. --- src/args.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/args.rs') diff --git a/src/args.rs b/src/args.rs index 56a10b4..0f548e4 100644 --- a/src/args.rs +++ b/src/args.rs @@ -134,16 +134,14 @@ pub enum ConfigOption { } impl ConfigOption { - pub fn try_from(disable: bool, value: Option, name: &'static str) -> Result { + pub fn try_from(disable: bool, value: Option, name: &'static str) -> anyhow::Result { if disable { - if value.is_some() { - Err(format!( - "--{name} and --no-{name} are mutually exclusive", - name = name - )) - } else { - Ok(ConfigOption::Disable) - } + anyhow::ensure!( + value.is_none(), + "--{name} and --no-{name} are mutually exclusive", + name = name + ); + Ok(ConfigOption::Disable) } else { match value { Some(value) => Ok(ConfigOption::Enable(value)), -- cgit v1.2.1