oof: add error variant MissingValueToFlag

This commit is contained in:
Vinícius Miguel 2021-04-07 23:28:37 -03:00
parent 5cca3c42e4
commit faae7c088f
3 changed files with 10 additions and 11 deletions

View File

@ -3,7 +3,7 @@ use std::{error, ffi::OsString, fmt};
use crate::Flag; use crate::Flag;
#[derive(Debug)] #[derive(Debug)]
pub enum OofError{ pub enum OofError<'t> {
FlagValueConflict { FlagValueConflict {
flag: Flag, flag: Flag,
previous_value: OsString, previous_value: OsString,
@ -13,16 +13,17 @@ pub enum OofError{
InvalidUnicode(OsString), InvalidUnicode(OsString),
/// User supplied an unrecognized short flag /// User supplied an unrecognized short flag
UnknownShortFlag(char), UnknownShortFlag(char),
MisplacedShortArgFlagError(char) MisplacedShortArgFlagError(char),
MissingValueToFlag(&'t Flag)
} }
impl error::Error for OofError { impl<'t> error::Error for OofError<'t> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> { fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None None
} }
} }
impl fmt::Display for OofError { impl<'t> fmt::Display for OofError<'t> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO: implement proper debug messages // TODO: implement proper debug messages
match self { match self {
@ -38,6 +39,7 @@ impl fmt::Display for OofError {
OofError::InvalidUnicode(flag) => write!(f, "{:?} is not valid Unicode.", flag), OofError::InvalidUnicode(flag) => write!(f, "{:?} is not valid Unicode.", flag),
OofError::UnknownShortFlag(ch) => write!(f, "Unknown argument '-{}'", ch), 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), OofError::MisplacedShortArgFlagError(ch) => write!(f, "Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.", ch),
OofError::MissingValueToFlag(flag) => write!(f, "Flag {} takes value but none was supplied.", flag)
} }
} }
} }

View File

@ -147,12 +147,9 @@ pub fn filter_flags(
} }
// pop the next one // pop the next one
let flag_argument = iter.next().unwrap_or_else(|| { let flag_argument = iter.next().ok_or_else(|| {
panic!( OofError::MissingValueToFlag(flag_info)
"USer errror: argument flag `argument_flag` came at last, but it \ })?;
requires an argument"
)
});
// Otherwise, insert it. // Otherwise, insert it.
result_flags.argument_flags.insert(flag_name, flag_argument); result_flags.argument_flags.insert(flag_name, flag_argument);

View File

@ -116,7 +116,7 @@ impl From<walkdir::Error> for Error {
} }
} }
impl From<oof::OofError> for Error { impl<'t> From<oof::OofError<'t>> for Error {
fn from(_err: oof::OofError) -> Self { fn from(_err: oof::OofError) -> Self {
todo!("We need to implement this properly"); todo!("We need to implement this properly");
} }