You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.7 KiB

//! Argument related errors
use super::*;
use std::{
error,
fmt,
};
#[derive(Debug)]
#[non_exhaustive]
pub enum ParseErrorKind
{
ExpectedArg(String),
UnexpectedArg(Option<String>),
UnknownOption(char),
UnknownKeyFormat(&'static str, String),
UnknownOptionFor(char, char),
CannotCombine(char, char),
BadTail(char, String),
NotUnique(char),
NotUniqueLong(Option<String>),
ModeSet,
}
impl error::Error for ParseErrorKind{}
impl fmt::Display for ParseErrorKind
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self {
Self::ExpectedArg(arg) => write!(f, "expected argument {:?}", arg),
Self::UnexpectedArg(None) => write!(f, "unknown argument"),
Self::UnexpectedArg(Some(arg)) => write!(f, "argument {:?} not expected here", arg),
Self::UnknownOption(ch) => write!(f, "unknown option `{}`", ch),
Self::UnknownKeyFormat(key, fmt) => write!(f, "unknown key format {:?} for {}", fmt, key),
Self::UnknownOptionFor(fo, o) => write!(f, "unknown option `{}` for `{}`", o, fo),
Self::CannotCombine(o1,o2) => write!(f, "cannot combine option `{}` with `{}`", o2, o1),
Self::BadTail(ch, ret) => write!(f, "unexpected tail {:?} for option `{}`", ret, ch),
Self::NotUnique(o) => write!(f, "option `{}` already specified", o),
Self::NotUniqueLong(None) => write!(f, "argument already specified"),
Self::NotUniqueLong(Some(opt)) => write!(f, "option {:?} already specified", opt),
Self::ModeSet => write!(f, "mode already set"),
}
}
}
impl From<(String, ParseErrorKind)> for Error
{
#[inline] fn from((froms, fromk): (String, ParseErrorKind)) -> Self
{
Self::Parse(froms, fromk)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ConstructError
{
Dropped,
NoGenerateKind,
ExpectedNotPresent(&'static str),
AlreadyExists(String),
Resolve(resolve::Error),
}
impl error::Error for ConstructError
{
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match self {
Self::Resolve(res) => res,
_=> return None,
})
}
}
impl fmt::Display for ConstructError
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self {
Self::NoGenerateKind => write!(f, "no key kind specified"),
Self::ExpectedNotPresent(name) => write!(f, "expected a {}, one was not provided", name),
Self::AlreadyExists(opt) => write!(f, "duplicate atom {}", opt),
Self::Resolve(_) => write!(f, "error resolving autodetect"),
Self::Dropped => panic!("mpsc fatal: dropped rx shoudl've been handled not reported"),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Parse(String, ParseErrorKind),
ExpectedKind(Option<String>),
Unknown,
AtomInternal,
}
impl error::Error for Error
{
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(match &self {
Error::Parse(_, parse) => parse,
_=> return None,
})
}
}
impl fmt::Display for Error
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self {
Error::Parse(arg, _) => write!(f, "error while parsing arg {:?}", arg),
Error::ExpectedKind(Some(got)) => write!(f, r#"expected a key kind ("AES" or "RSA"), got {:?}"#, got),
Error::ExpectedKind(None) => write!(f, "expected a key kind"),
Error::AtomInternal => unreachable!("This should've been handled."),
_ => write!(f, "unknown error"),
}
}
}
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error
{
#[inline] fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self
{
Self::AtomInternal
}
}
impl From<resolve::Error> for ConstructError
{
#[inline] fn from(from: resolve::Error) -> Self
{
Self::Resolve(from)
}
}