From fe5cd8a58af58a8998ecbc877b2dc290193b8d86 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Wed, 2 Sep 2020 17:50:42 -0400 Subject: [PATCH] Prelude: args: use anyhow as Error/Result --- youmubot-prelude/src/args.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/youmubot-prelude/src/args.rs b/youmubot-prelude/src/args.rs index cdb5979..80175d7 100644 --- a/youmubot-prelude/src/args.rs +++ b/youmubot-prelude/src/args.rs @@ -2,14 +2,17 @@ pub use duration::Duration; pub use username_arg::UsernameArg; mod duration { + use crate::{Error, Result}; use std::fmt; use std::time::Duration as StdDuration; - use String as Error; - // Parse a single duration unit - fn parse_duration_string(s: &str) -> Result { + + const INVALID_DURATION: &str = "Not a valid duration"; + + /// Parse a single duration unit + fn parse_duration_string(s: &str) -> Result { // We reject the empty case if s == "" { - return Err(Error::from("empty strings are not valid durations")); + return Err(Error::msg("empty strings are not valid durations")); } struct ParseStep { current_value: Option, @@ -26,7 +29,7 @@ mod duration { current_value: Some(v.unwrap_or(0) * 10 + ((item as u64) - ('0' as u64))), ..s }), - (_, None) => Err(Error::from("Not a valid duration")), + (_, None) => Err(Error::msg(INVALID_DURATION)), (item, Some(v)) => Ok(ParseStep { current_value: None, current_duration: s.current_duration @@ -36,7 +39,7 @@ mod duration { 'h' => StdDuration::from_secs(60 * 60), 'd' => StdDuration::from_secs(60 * 60 * 24), 'w' => StdDuration::from_secs(60 * 60 * 24 * 7), - _ => return Err(Error::from("Not a valid duration")), + _ => return Err(Error::msg(INVALID_DURATION)), } * (v as u32), }), }, @@ -44,7 +47,7 @@ mod duration { .and_then(|v| match v.current_value { // All values should be consumed None => Ok(v), - _ => Err(Error::from("Not a valid duration")), + _ => Err(Error::msg(INVALID_DURATION)), }) .map(|v| v.current_duration) }