diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-08-06 07:22:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-06 02:22:19 -0500 |
| commit | 5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch) | |
| tree | b006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-chat/src | |
| parent | 1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff) | |
| download | azalea-drasl-5a9fca0ca9cdb46f4b866781f219756c89e2293a.tar.xz | |
Better errors (#14)
* make reading use thiserror
* finish implementing all the error things
* clippy warnings related to ok_or
* fix some errors in other places
* thiserror in more places
* don't use closures in a couple places
* errors in writing packet
* rip backtraces
* change some BufReadError::Custom to UnexpectedEnumVariant
* Errors say what packet is bad
* error on leftover data and fix
it wasn't reading the properties for gameprofile
Diffstat (limited to 'azalea-chat/src')
| -rwxr-xr-x | azalea-chat/src/component.rs | 9 | ||||
| -rwxr-xr-x | azalea-chat/src/style.rs | 17 | ||||
| -rwxr-xr-x | azalea-chat/src/text_component.rs | 2 |
3 files changed, 14 insertions, 14 deletions
diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index d2f687d4..8a038bf0 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -1,6 +1,6 @@ use std::io::{Read, Write}; -use azalea_buf::{McBufReadable, McBufWritable}; +use azalea_buf::{BufReadError, McBufReadable, McBufWritable}; use serde::{de, Deserialize, Deserializer}; use crate::{ @@ -269,11 +269,10 @@ impl<'de> Deserialize<'de> for Component { } impl McBufReadable for Component { - fn read_from(buf: &mut impl Read) -> Result<Self, String> { + fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { let string = String::read_from(buf)?; - let json: serde_json::Value = serde_json::from_str(string.as_str()) - .map_err(|_| "Component isn't valid JSON".to_string())?; - let component = Component::deserialize(json).map_err(|e| e.to_string())?; + let json: serde_json::Value = serde_json::from_str(string.as_str())?; + let component = Component::deserialize(json)?; Ok(component) } } diff --git a/azalea-chat/src/style.rs b/azalea-chat/src/style.rs index 9fca6563..3a667776 100755 --- a/azalea-chat/src/style.rs +++ b/azalea-chat/src/style.rs @@ -9,17 +9,17 @@ pub struct TextColor { } impl TextColor { - pub fn parse(value: String) -> Result<TextColor, String> { + pub fn parse(value: String) -> Option<TextColor> { if value.starts_with('#') { let n = value.chars().skip(1).collect::<String>(); let n = u32::from_str_radix(&n, 16).unwrap(); - return Ok(TextColor::from_rgb(n)); + return Some(TextColor::from_rgb(n)); } let color_option = NAMED_COLORS.get(&value.to_ascii_uppercase()); if let Some(color) = color_option { - return Ok(color.clone()); + return Some(color.clone()); } - Err(format!("Invalid color {}", value)) + None } fn from_rgb(value: u32) -> TextColor { @@ -157,13 +157,14 @@ impl<'a> ChatFormatting<'a> { } } - pub fn from_code(code: char) -> Result<&'static ChatFormatting<'static>, String> { + pub fn from_code(code: char) -> Option<&'static ChatFormatting<'static>> { for formatter in &ChatFormatting::FORMATTERS { if formatter.code == code { - return Ok(formatter); + return Some(formatter); } } - Err(format!("Invalid formatting code {}", code)) + + None } } @@ -241,7 +242,7 @@ impl Style { let color: Option<TextColor> = json_object .get("color") .and_then(|v| v.as_str()) - .and_then(|v| TextColor::parse(v.to_string()).ok()); + .and_then(|v| TextColor::parse(v.to_string())); Style { color, bold, diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index a704f455..a3eb0e4b 100755 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -22,7 +22,7 @@ pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextCompo while i < legacy_color_code.chars().count() { if legacy_color_code.chars().nth(i).unwrap() == LEGACY_FORMATTING_CODE_SYMBOL { let formatting_code = legacy_color_code.chars().nth(i + 1).unwrap(); - if let Ok(formatter) = ChatFormatting::from_code(formatting_code) { + if let Some(formatter) = ChatFormatting::from_code(formatting_code) { if components.is_empty() || !components.last().unwrap().text.is_empty() { components.push(TextComponent::new("".to_string())); } |
