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;
#[derive(Debug)]
pub enum OofError{
pub enum OofError<'t> {
FlagValueConflict {
flag: Flag,
previous_value: OsString,
@ -13,16 +13,17 @@ pub enum OofError{
InvalidUnicode(OsString),
/// User supplied an unrecognized short flag
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)> {
None
}
}
impl fmt::Display for OofError {
impl<'t> fmt::Display for OofError<'t> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO: implement proper debug messages
match self {
@ -38,6 +39,7 @@ impl fmt::Display for OofError {
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),
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
let flag_argument = iter.next().unwrap_or_else(|| {
panic!(
"USer errror: argument flag `argument_flag` came at last, but it \
requires an argument"
)
});
let flag_argument = iter.next().ok_or_else(|| {
OofError::MissingValueToFlag(flag_info)
})?;
// Otherwise, insert it.
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 {
todo!("We need to implement this properly");
}