azalea_brigadier/errors/
builtin_errors.rs

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