1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
};
use twilight_model::{
application::command::{CommandOptionType, Number},
channel::ChannelType,
};
#[derive(Debug, Clone, PartialEq, Eq)]
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, Eq)]
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.0)
}
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, Eq)]
pub enum ParseOptionErrorType {
InvalidType(CommandOptionType),
InvalidChoice(String),
IntegerOutOfRange(i64),
NumberOutOfRange(Number),
InvalidChannelType(ChannelType),
LookupFailed(u64),
RequiredField,
UnknownField,
UnknownSubcommand,
}