1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00
azalea/azalea-brigadier/src/exceptions/command_syntax_exception.rs
EightFactorial 9f5e5c092b
Complete ClientboundCommand{Suggestion}sPacket, Serde support for NBT Tags (#49)
* Serializing ClientboundStatusResponsePacket

Enable serialization of ClientboundStatusResponsePacket

* Update clientbound_status_response_packet.rs

Add options previewsChat and enforcesSecureChat

* Serialize Style and TextColor

* Serialize BaseComponent

* Serialize TextComponent

* Fix Style

* Serialize Component

* Fix multiple formats per message, fix reset tag

* Fix Style, again

* Use FlatMapSerializer

* Forgot italics

* Count struct fields, reorganize logic

* Serialize TranslatableComponent

* Rewrite TextComponent Serializer

* Fix using TextColor::Parse

* Code Cleanup

* Add default attribute, just in case

* Clippy

* use serde derive feature + preferred formatting choices

* McBufWritable for BrigadierNodeStub

* Thanks Clippy...

* Implement suggestions in azalea-brigadier

* Serde support for NBT Tags

* Serde options

* Forgot Options

* Oops, that's McBufWritable for BrigadierParser

* Fix McBufWritable for SlotData

* Complete ClientboundUpdateRecipesPacket

* fix some issues

* better impl McBufReadable for Suggestions

Co-authored-by: BuildTools <unconfigured@null.spigotmc.org>
Co-authored-by: mat <github@matdoes.dev>
2022-12-06 20:48:48 -06:00

94 lines
2.2 KiB
Rust
Executable file

use super::builtin_exceptions::BuiltInExceptions;
use std::{
cmp,
fmt::{self, Write},
};
#[derive(Clone, PartialEq)]
pub struct CommandSyntaxException {
pub type_: BuiltInExceptions,
message: String,
input: Option<String>,
cursor: Option<usize>,
}
const CONTEXT_AMOUNT: usize = 10;
impl CommandSyntaxException {
pub fn new(type_: BuiltInExceptions, message: String, input: &str, cursor: usize) -> Self {
Self {
type_,
message,
input: Some(input.to_string()),
cursor: Some(cursor),
}
}
pub fn create(type_: BuiltInExceptions, message: String) -> Self {
Self {
type_,
message,
input: None,
cursor: None,
}
}
pub fn message(&self) -> String {
let mut message = self.message.clone();
let context = self.context();
if let Some(context) = context {
write!(
message,
" at position {}: {}",
self.cursor.unwrap_or(usize::MAX),
context
)
.unwrap();
}
message
}
pub fn raw_message(&self) -> &String {
&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 as isize - CONTEXT_AMOUNT as isize) as usize)..cursor],
);
builder.push_str("<--[HERE]");
return Some(builder);
}
}
None
}
pub fn get_type(&self) -> &BuiltInExceptions {
&self.type_
}
pub fn input(&self) -> &Option<String> {
&self.input
}
pub fn cursor(&self) -> Option<usize> {
self.cursor
}
}
impl fmt::Debug for CommandSyntaxException {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message())
}
}