oof: add some error variants to replace panics

This commit is contained in:
Vinícius Miguel 2021-04-07 21:59:23 -03:00
parent d9b39706e2
commit c08eb91632
2 changed files with 20 additions and 14 deletions

View File

@ -3,12 +3,17 @@ use std::{error, ffi::OsString, fmt};
use crate::Flag; use crate::Flag;
#[derive(Debug)] #[derive(Debug)]
pub enum OofError { pub enum OofError{
FlagValueConflict { FlagValueConflict {
flag: Flag, flag: Flag,
previous_value: OsString, previous_value: OsString,
new_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 { impl error::Error for OofError {
@ -30,6 +35,9 @@ impl fmt::Display for OofError {
"CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.", "CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.",
flag.long, previous_value, new_value 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),
} }
} }
} }

View File

@ -7,10 +7,7 @@ mod error;
mod flags; mod flags;
pub mod util; pub mod util;
use std::{ use std::{collections::BTreeMap, ffi::{OsStr, OsString}};
collections::BTreeMap,
ffi::{OsStr, OsString},
};
pub use error::OofError; pub use error::OofError;
pub use flags::{ArgFlag, Flag, FlagType, Flags}; pub use flags::{ArgFlag, Flag, FlagType, Flags};
@ -105,10 +102,11 @@ pub fn filter_flags(
continue; continue;
} }
// If it is a flag, now we try to interpret as valid utf-8 // If it is a flag, now we try to interpret it as valid utf-8
let flag: &str = arg let flag= match arg.to_str() {
.to_str() Some(arg) => arg,
.unwrap_or_else(|| panic!("User error: The flag needs to be valid utf8")); None => return Err(OofError::InvalidUnicode(arg))
};
// Only one hyphen in the flag // Only one hyphen in the flag
// A short flag can be of form "-", "-abcd", "-h", "-v", etc // 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 // Safety: this loop only runs when len >= 1, so this subtraction is safe
let is_last_letter = i == letters.len() - 1; let is_last_letter = i == letters.len() - 1;
let flag_info = short_flags_info.get(&letter).unwrap_or_else(|| { let flag_info = short_flags_info.get(&letter).ok_or_else(|| {
panic!("User error: Unexpected/UNKNOWN flag `letter`, error") OofError::UnknownShortFlag(letter)
}); })?;
if !is_last_letter && flag_info.takes_value { 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. // 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 // of this piece of text
} }