azalea_brigadier/exceptions/
builtin_exceptions.rs

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::fmt;

use super::command_syntax_exception::CommandSyntaxException;
use crate::string_reader::StringReader;

#[derive(Clone, PartialEq)]
pub enum BuiltInExceptions {
    DoubleTooSmall { found: f64, min: f64 },
    DoubleTooBig { found: f64, max: f64 },

    FloatTooSmall { found: f32, min: f32 },
    FloatTooBig { found: f32, max: f32 },

    IntegerTooSmall { found: i32, min: i32 },
    IntegerTooBig { found: i32, max: i32 },

    LongTooSmall { found: i64, min: i64 },
    LongTooBig { found: i64, max: i64 },

    LiteralIncorrect { expected: String },

    ReaderExpectedStartOfQuote,
    ReaderExpectedEndOfQuote,
    ReaderInvalidEscape { character: char },
    ReaderInvalidBool { value: String },
    ReaderInvalidInt { value: String },
    ReaderExpectedInt,
    ReaderInvalidLong { value: String },
    ReaderExpectedLong,
    ReaderInvalidDouble { value: String },
    ReaderExpectedDouble,
    ReaderInvalidFloat { value: String },
    ReaderExpectedFloat,
    ReaderExpectedBool,
    ReaderExpectedSymbol { symbol: char },

    DispatcherUnknownCommand,
    DispatcherUnknownArgument,
    DispatcherExpectedArgumentSeparator,
    DispatcherParseException { message: String },
}

impl fmt::Debug for BuiltInExceptions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BuiltInExceptions::DoubleTooSmall { found, min } => {
                write!(f, "Double must not be less than {min}, found {found}")
            }
            BuiltInExceptions::DoubleTooBig { found, max } => {
                write!(f, "Double must not be more than {max}, found {found}")
            }

            BuiltInExceptions::FloatTooSmall { found, min } => {
                write!(f, "Float must not be less than {min}, found {found}")
            }
            BuiltInExceptions::FloatTooBig { found, max } => {
                write!(f, "Float must not be more than {max}, found {found}")
            }

            BuiltInExceptions::IntegerTooSmall { found, min } => {
                write!(f, "Integer must not be less than {min}, found {found}")
            }
            BuiltInExceptions::IntegerTooBig { found, max } => {
                write!(f, "Integer must not be more than {max}, found {found}")
            }

            BuiltInExceptions::LongTooSmall { found, min } => {
                write!(f, "Long must not be less than {min}, found {found}")
            }
            BuiltInExceptions::LongTooBig { found, max } => {
                write!(f, "Long must not be more than {max}, found {found}")
            }

            BuiltInExceptions::LiteralIncorrect { expected } => {
                write!(f, "Expected literal {expected}")
            }

            BuiltInExceptions::ReaderExpectedStartOfQuote => {
                write!(f, "Expected quote to start a string")
            }
            BuiltInExceptions::ReaderExpectedEndOfQuote => {
                write!(f, "Unclosed quoted string")
            }
            BuiltInExceptions::ReaderInvalidEscape { character } => {
                write!(f, "Invalid escape sequence '{character}' in quoted string")
            }
            BuiltInExceptions::ReaderInvalidBool { value } => {
                write!(
                    f,
                    "Invalid bool, expected true or false but found '{value}'"
                )
            }
            BuiltInExceptions::ReaderInvalidInt { value } => {
                write!(f, "Invalid Integer '{value}'")
            }
            BuiltInExceptions::ReaderExpectedInt => {
                write!(f, "Expected Integer")
            }
            BuiltInExceptions::ReaderInvalidLong { value } => {
                write!(f, "Invalid long '{value}'")
            }
            BuiltInExceptions::ReaderExpectedLong => {
                write!(f, "Expected long")
            }
            BuiltInExceptions::ReaderInvalidDouble { value } => {
                write!(f, "Invalid double '{value}'")
            }
            BuiltInExceptions::ReaderExpectedDouble => {
                write!(f, "Expected double")
            }
            BuiltInExceptions::ReaderInvalidFloat { value } => {
                write!(f, "Invalid Float '{value}'")
            }
            BuiltInExceptions::ReaderExpectedFloat => {
                write!(f, "Expected Float")
            }
            BuiltInExceptions::ReaderExpectedBool => {
                write!(f, "Expected bool")
            }
            BuiltInExceptions::ReaderExpectedSymbol { symbol } => {
                write!(f, "Expected '{symbol}'")
            }

            BuiltInExceptions::DispatcherUnknownCommand => {
                write!(f, "Unknown command")
            }
            BuiltInExceptions::DispatcherUnknownArgument => {
                write!(f, "Incorrect argument for command")
            }
            BuiltInExceptions::DispatcherExpectedArgumentSeparator => {
                write!(
                    f,
                    "Expected whitespace to end one argument, but found trailing data"
                )
            }
            BuiltInExceptions::DispatcherParseException { message } => {
                write!(f, "Could not parse command: {message}")
            }
        }
    }
}

impl BuiltInExceptions {
    pub fn create(self) -> CommandSyntaxException {
        let message = format!("{self:?}");
        CommandSyntaxException::create(self, message)
    }

    pub fn create_with_context(self, reader: &StringReader) -> CommandSyntaxException {
        let message = format!("{self:?}");
        CommandSyntaxException::new(self, message, reader.string(), reader.cursor())
    }
}