azalea_brigadier/exceptions/
builtin_exceptions.rs

1use std::fmt;
2
3use super::command_syntax_exception::CommandSyntaxException;
4use crate::string_reader::StringReader;
5
6#[derive(Clone, PartialEq)]
7pub enum BuiltInExceptions {
8    DoubleTooSmall { found: f64, min: f64 },
9    DoubleTooBig { found: f64, max: f64 },
10
11    FloatTooSmall { found: f32, min: f32 },
12    FloatTooBig { found: f32, max: f32 },
13
14    IntegerTooSmall { found: i32, min: i32 },
15    IntegerTooBig { found: i32, max: i32 },
16
17    LongTooSmall { found: i64, min: i64 },
18    LongTooBig { found: i64, max: i64 },
19
20    LiteralIncorrect { expected: String },
21
22    ReaderExpectedStartOfQuote,
23    ReaderExpectedEndOfQuote,
24    ReaderInvalidEscape { character: char },
25    ReaderInvalidBool { value: String },
26    ReaderInvalidInt { value: String },
27    ReaderExpectedInt,
28    ReaderInvalidLong { value: String },
29    ReaderExpectedLong,
30    ReaderInvalidDouble { value: String },
31    ReaderExpectedDouble,
32    ReaderInvalidFloat { value: String },
33    ReaderExpectedFloat,
34    ReaderExpectedBool,
35    ReaderExpectedSymbol { symbol: char },
36
37    DispatcherUnknownCommand,
38    DispatcherUnknownArgument,
39    DispatcherExpectedArgumentSeparator,
40    DispatcherParseException { message: String },
41}
42
43impl fmt::Debug for BuiltInExceptions {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            BuiltInExceptions::DoubleTooSmall { found, min } => {
47                write!(f, "Double must not be less than {min}, found {found}")
48            }
49            BuiltInExceptions::DoubleTooBig { found, max } => {
50                write!(f, "Double must not be more than {max}, found {found}")
51            }
52
53            BuiltInExceptions::FloatTooSmall { found, min } => {
54                write!(f, "Float must not be less than {min}, found {found}")
55            }
56            BuiltInExceptions::FloatTooBig { found, max } => {
57                write!(f, "Float must not be more than {max}, found {found}")
58            }
59
60            BuiltInExceptions::IntegerTooSmall { found, min } => {
61                write!(f, "Integer must not be less than {min}, found {found}")
62            }
63            BuiltInExceptions::IntegerTooBig { found, max } => {
64                write!(f, "Integer must not be more than {max}, found {found}")
65            }
66
67            BuiltInExceptions::LongTooSmall { found, min } => {
68                write!(f, "Long must not be less than {min}, found {found}")
69            }
70            BuiltInExceptions::LongTooBig { found, max } => {
71                write!(f, "Long must not be more than {max}, found {found}")
72            }
73
74            BuiltInExceptions::LiteralIncorrect { expected } => {
75                write!(f, "Expected literal {expected}")
76            }
77
78            BuiltInExceptions::ReaderExpectedStartOfQuote => {
79                write!(f, "Expected quote to start a string")
80            }
81            BuiltInExceptions::ReaderExpectedEndOfQuote => {
82                write!(f, "Unclosed quoted string")
83            }
84            BuiltInExceptions::ReaderInvalidEscape { character } => {
85                write!(f, "Invalid escape sequence '{character}' in quoted string")
86            }
87            BuiltInExceptions::ReaderInvalidBool { value } => {
88                write!(
89                    f,
90                    "Invalid bool, expected true or false but found '{value}'"
91                )
92            }
93            BuiltInExceptions::ReaderInvalidInt { value } => {
94                write!(f, "Invalid Integer '{value}'")
95            }
96            BuiltInExceptions::ReaderExpectedInt => {
97                write!(f, "Expected Integer")
98            }
99            BuiltInExceptions::ReaderInvalidLong { value } => {
100                write!(f, "Invalid long '{value}'")
101            }
102            BuiltInExceptions::ReaderExpectedLong => {
103                write!(f, "Expected long")
104            }
105            BuiltInExceptions::ReaderInvalidDouble { value } => {
106                write!(f, "Invalid double '{value}'")
107            }
108            BuiltInExceptions::ReaderExpectedDouble => {
109                write!(f, "Expected double")
110            }
111            BuiltInExceptions::ReaderInvalidFloat { value } => {
112                write!(f, "Invalid Float '{value}'")
113            }
114            BuiltInExceptions::ReaderExpectedFloat => {
115                write!(f, "Expected Float")
116            }
117            BuiltInExceptions::ReaderExpectedBool => {
118                write!(f, "Expected bool")
119            }
120            BuiltInExceptions::ReaderExpectedSymbol { symbol } => {
121                write!(f, "Expected '{symbol}'")
122            }
123
124            BuiltInExceptions::DispatcherUnknownCommand => {
125                write!(f, "Unknown command")
126            }
127            BuiltInExceptions::DispatcherUnknownArgument => {
128                write!(f, "Incorrect argument for command")
129            }
130            BuiltInExceptions::DispatcherExpectedArgumentSeparator => {
131                write!(
132                    f,
133                    "Expected whitespace to end one argument, but found trailing data"
134                )
135            }
136            BuiltInExceptions::DispatcherParseException { message } => {
137                write!(f, "Could not parse command: {message}")
138            }
139        }
140    }
141}
142
143impl BuiltInExceptions {
144    pub fn create(self) -> CommandSyntaxException {
145        let message = format!("{self:?}");
146        CommandSyntaxException::create(self, message)
147    }
148
149    pub fn create_with_context(self, reader: &StringReader) -> CommandSyntaxException {
150        let message = format!("{self:?}");
151        CommandSyntaxException::new(self, message, reader.string(), reader.cursor())
152    }
153}