use std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
};
use twilight_model::{application::command::CommandOptionType, channel::ChannelType};
#[derive(Debug, Clone, PartialEq)]
pub enum ParseError {
EmptyOptions,
Option(ParseOptionError),
}
impl Error for ParseError {}
impl Display for ParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
ParseError::EmptyOptions => write!(f, "received an empty option list"),
ParseError::Option(error) => error.fmt(f),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParseOptionError {
pub field: String,
pub kind: ParseOptionErrorType,
}
impl Error for ParseOptionError {}
impl Display for ParseOptionError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "failed to parse option `{}`: ", self.field)?;
match &self.kind {
ParseOptionErrorType::InvalidType(ty) => write!(f, "invalid type, found {}", ty.kind()),
ParseOptionErrorType::InvalidChoice(choice) => {
write!(f, "invalid choice value, found `{choice}`")
}
ParseOptionErrorType::IntegerOutOfRange(val) => {
write!(f, "out of range integer, received `{val}`")
}
ParseOptionErrorType::NumberOutOfRange(val) => {
write!(f, "out of range number, received `{val}`")
}
ParseOptionErrorType::StringLengthOutOfRange(val) => {
write!(f, "out of range string length, received `{val}`")
}
ParseOptionErrorType::InvalidChannelType(kind) => {
write!(f, "invalid channel type, received `{}`", kind.name())
}
ParseOptionErrorType::LookupFailed(id) => write!(f, "failed to resolve `{id}`"),
ParseOptionErrorType::UnknownField => write!(f, "unknown field"),
ParseOptionErrorType::UnknownSubcommand => write!(f, "unknown subcommand"),
ParseOptionErrorType::RequiredField => write!(f, "missing required field"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ParseOptionErrorType {
InvalidType(CommandOptionType),
InvalidChoice(String),
IntegerOutOfRange(i64),
NumberOutOfRange(f64),
StringLengthOutOfRange(String),
InvalidChannelType(ChannelType),
LookupFailed(u64),
RequiredField,
UnknownField,
UnknownSubcommand,
}