From c08eb9163251a4a6e20812d392c7ddd93cfd90c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Miguel?= Date: Wed, 7 Apr 2021 21:59:23 -0300 Subject: [PATCH] oof: add some error variants to replace panics --- oof/src/error.rs | 10 +++++++++- oof/src/lib.rs | 24 +++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/oof/src/error.rs b/oof/src/error.rs index abc8671..3403771 100644 --- a/oof/src/error.rs +++ b/oof/src/error.rs @@ -3,12 +3,17 @@ use std::{error, ffi::OsString, fmt}; use crate::Flag; #[derive(Debug)] -pub enum OofError { +pub enum OofError{ FlagValueConflict { flag: Flag, previous_value: OsString, new_value: OsString, }, + /// User supplied a flag containing invalid Unicode + InvalidUnicode(OsString), + /// User supplied an unrecognized short flag + UnknownShortFlag(char), + MisplacedShortArgFlagError(char) } impl error::Error for OofError { @@ -30,6 +35,9 @@ impl fmt::Display for OofError { "CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.", flag.long, previous_value, new_value ), + OofError::InvalidUnicode(flag) => write!(f, "{:?} is not valid Unicode.", flag), + OofError::UnknownShortFlag(ch) => write!(f, "Unknown argument '-{}'", ch), + OofError::MisplacedShortArgFlagError(ch) => write!(f, "Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.", ch), } } } diff --git a/oof/src/lib.rs b/oof/src/lib.rs index ccd4364..ef509ac 100644 --- a/oof/src/lib.rs +++ b/oof/src/lib.rs @@ -7,10 +7,7 @@ mod error; mod flags; pub mod util; -use std::{ - collections::BTreeMap, - ffi::{OsStr, OsString}, -}; +use std::{collections::BTreeMap, ffi::{OsStr, OsString}}; pub use error::OofError; pub use flags::{ArgFlag, Flag, FlagType, Flags}; @@ -105,10 +102,11 @@ pub fn filter_flags( continue; } - // If it is a flag, now we try to interpret as valid utf-8 - let flag: &str = arg - .to_str() - .unwrap_or_else(|| panic!("User error: The flag needs to be valid utf8")); + // If it is a flag, now we try to interpret it as valid utf-8 + let flag= match arg.to_str() { + Some(arg) => arg, + None => return Err(OofError::InvalidUnicode(arg)) + }; // Only one hyphen in the flag // A short flag can be of form "-", "-abcd", "-h", "-v", etc @@ -129,14 +127,14 @@ pub fn filter_flags( // Safety: this loop only runs when len >= 1, so this subtraction is safe let is_last_letter = i == letters.len() - 1; - let flag_info = short_flags_info.get(&letter).unwrap_or_else(|| { - panic!("User error: Unexpected/UNKNOWN flag `letter`, error") - }); + let flag_info = short_flags_info.get(&letter).ok_or_else(|| { + OofError::UnknownShortFlag(letter) + })?; if !is_last_letter && flag_info.takes_value { - panic!("User error: Only the last letter can refer to flag that takes values"); + return Err(OofError::MisplacedShortArgFlagError(letter)) // Because "-AB argument" only works if B takes values, not A. - // That is, the short flag that takes values need to come at the end + // That is, the short flag that takes values needs to come at the end // of this piece of text }