diff options
| author | mat <github@matdoes.dev> | 2022-01-09 14:50:41 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-01-09 14:50:41 -0600 |
| commit | 8331851d972b42b68e1fb64e2ec9faef6c02be32 (patch) | |
| tree | 35f8a0c9175a10f626835f7930660ef7e7415acf /azalea-brigadier/src/exceptions | |
| parent | d959fb2d0cc4d8ad97eae86666876e22b2e50613 (diff) | |
| download | azalea-drasl-8331851d972b42b68e1fb64e2ec9faef6c02be32.tar.xz | |
string reader
Diffstat (limited to 'azalea-brigadier/src/exceptions')
10 files changed, 243 insertions, 0 deletions
diff --git a/azalea-brigadier/src/exceptions/builtin_exceptions.rs b/azalea-brigadier/src/exceptions/builtin_exceptions.rs index e69de29b..fcca49cd 100644 --- a/azalea-brigadier/src/exceptions/builtin_exceptions.rs +++ b/azalea-brigadier/src/exceptions/builtin_exceptions.rs @@ -0,0 +1,158 @@ +use std::fmt; + +use crate::{immutable_string_reader::ImmutableStringReader, message::Message}; + +use super::command_syntax_exception::CommandSyntaxException; + +pub enum BuiltInExceptions { + DoubleTooSmall { found: usize, min: usize }, + DoubleTooBig { found: usize, max: usize }, + + FloatTooSmall { found: usize, min: usize }, + FloatTooBig { found: usize, max: usize }, + + IntegerTooSmall { found: usize, min: usize }, + IntegerTooBig { found: usize, max: usize }, + + LONGTooSmall { found: usize, min: usize }, + LONGTooBig { found: usize, max: usize }, + + 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 }, + + ReaderUnknownCommand, + ReaderUnknownArgument, + DusoatcgerExpectedArgumentSeparator, + 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 {}, found {}", min, found) + } + BuiltInExceptions::DoubleTooBig { found, max } => { + write!(f, "Double must not be more than {}, found {}", max, found) + } + + BuiltInExceptions::FloatTooSmall { found, min } => { + write!(f, "Float must not be less than {}, found {}", min, found) + } + BuiltInExceptions::FloatTooBig { found, max } => { + write!(f, "Float must not be more than {}, found {}", max, found) + } + + BuiltInExceptions::IntegerTooSmall { found, min } => { + write!(f, "Integer must not be less than {}, found {}", min, found) + } + BuiltInExceptions::IntegerTooBig { found, max } => { + write!(f, "Integer must not be more than {}, found {}", max, found) + } + + BuiltInExceptions::LONGTooSmall { found, min } => { + write!(f, "Long must not be less than {}, found {}", min, found) + } + BuiltInExceptions::LONGTooBig { found, max } => { + write!(f, "Long must not be more than {}, found {}", max, 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 '{}' in quoted string", + character + ) + } + 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::ReaderUnknownCommand => { + write!(f, "Unknown command") + } + BuiltInExceptions::ReaderUnknownArgument => { + write!(f, "Incorrect argument for command") + } + BuiltInExceptions::DusoatcgerExpectedArgumentSeparator => { + 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 = Message::from(format!("{:?}", self)); + CommandSyntaxException::create(self, message) + } + + pub fn create_with_context(self, reader: &dyn ImmutableStringReader) -> CommandSyntaxException { + let message = Message::from(format!("{:?}", self)); + CommandSyntaxException::new(self, message, reader.string(), reader.cursor()) + } +} diff --git a/azalea-brigadier/src/exceptions/command_exception_type.rs b/azalea-brigadier/src/exceptions/command_exception_type.rs deleted file mode 100644 index e69de29b..00000000 --- a/azalea-brigadier/src/exceptions/command_exception_type.rs +++ /dev/null diff --git a/azalea-brigadier/src/exceptions/command_syntax_exception.rs b/azalea-brigadier/src/exceptions/command_syntax_exception.rs index e69de29b..b9fbea45 100644 --- a/azalea-brigadier/src/exceptions/command_syntax_exception.rs +++ b/azalea-brigadier/src/exceptions/command_syntax_exception.rs @@ -0,0 +1,82 @@ +use std::{cmp, rc::Rc}; + +use super::builtin_exceptions::BuiltInExceptions; +use crate::message::Message; + +pub struct CommandSyntaxException { + type_: BuiltInExceptions, + message: Message, + input: Option<String>, + cursor: Option<usize>, +} + +const CONTEXT_AMOUNT: usize = 10; +const ENABLE_COMMAND_STACK_TRACES: bool = true; + +impl CommandSyntaxException { + pub fn new(type_: BuiltInExceptions, message: Message, input: &str, cursor: usize) -> Self { + Self { + type_, + message, + input: Some(input.to_string()), + cursor: Some(cursor), + } + } + + pub fn create(type_: BuiltInExceptions, message: Message) -> Self { + Self { + type_, + message, + input: None, + cursor: None, + } + } + + pub fn message(&self) -> String { + let mut message = self.message.string(); + let context = self.context(); + if let Some(context) = context { + message.push_str(&format!( + " at position {}: {}", + self.cursor.unwrap_or(usize::MAX), + context + )); + } + message + } + + pub fn raw_message(&self) -> &Message { + &self.message + } + + pub fn context(&self) -> Option<String> { + if let Some(input) = &self.input { + if let Some(cursor) = self.cursor { + let mut builder = String::new(); + let cursor = cmp::min(input.len(), cursor); + + if cursor > CONTEXT_AMOUNT { + builder.push_str("..."); + } + + builder.push_str(&input[cmp::max(0, cursor - CONTEXT_AMOUNT)..cursor]); + builder.push_str("<--[HERE]"); + + return Some(builder); + } + } + None + } + + pub fn get_type(&self) -> &BuiltInExceptions { + &self.type_ + } + + pub fn input(&self) -> String { + self.input() + } + + pub fn cursor(&self) -> Option<usize> { + self.cursor + } +} diff --git a/azalea-brigadier/src/exceptions/dynamic2_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic2_command_exception_type.rs deleted file mode 100644 index e69de29b..00000000 --- a/azalea-brigadier/src/exceptions/dynamic2_command_exception_type.rs +++ /dev/null diff --git a/azalea-brigadier/src/exceptions/dynamic3_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic3_command_exception_type.rs deleted file mode 100644 index e69de29b..00000000 --- a/azalea-brigadier/src/exceptions/dynamic3_command_exception_type.rs +++ /dev/null diff --git a/azalea-brigadier/src/exceptions/dynamic4_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic4_command_exception_type.rs deleted file mode 100644 index e69de29b..00000000 --- a/azalea-brigadier/src/exceptions/dynamic4_command_exception_type.rs +++ /dev/null diff --git a/azalea-brigadier/src/exceptions/dynamicN_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamicN_command_exception_type.rs deleted file mode 100644 index e69de29b..00000000 --- a/azalea-brigadier/src/exceptions/dynamicN_command_exception_type.rs +++ /dev/null diff --git a/azalea-brigadier/src/exceptions/dynamic_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic_command_exception_type.rs deleted file mode 100644 index e69de29b..00000000 --- a/azalea-brigadier/src/exceptions/dynamic_command_exception_type.rs +++ /dev/null diff --git a/azalea-brigadier/src/exceptions/mod.rs b/azalea-brigadier/src/exceptions/mod.rs index e69de29b..4a82b01e 100644 --- a/azalea-brigadier/src/exceptions/mod.rs +++ b/azalea-brigadier/src/exceptions/mod.rs @@ -0,0 +1,3 @@ +pub mod builtin_exception_provider; +pub mod builtin_exceptions; +pub mod command_syntax_exception; diff --git a/azalea-brigadier/src/exceptions/simple_command_exception_type.rs b/azalea-brigadier/src/exceptions/simple_command_exception_type.rs deleted file mode 100644 index e69de29b..00000000 --- a/azalea-brigadier/src/exceptions/simple_command_exception_type.rs +++ /dev/null |
