diff options
| author | mat <github@matdoes.dev> | 2021-12-15 14:06:14 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-15 14:06:14 -0600 |
| commit | 1c9e089b7268fe4ffeeee5e594bb2d708470d2a1 (patch) | |
| tree | 7743fa0a6347d3adf727d7b942e6f91c1956e242 /minecraft-chat/src | |
| parent | 732de94d7b9f1bf2bc9239c8138a37c53242b470 (diff) | |
| download | azalea-drasl-1c9e089b7268fe4ffeeee5e594bb2d708470d2a1.tar.xz | |
clippy
Diffstat (limited to 'minecraft-chat/src')
| -rw-r--r-- | minecraft-chat/src/base_component.rs | 6 | ||||
| -rw-r--r-- | minecraft-chat/src/component.rs | 13 | ||||
| -rw-r--r-- | minecraft-chat/src/style.rs | 19 | ||||
| -rw-r--r-- | minecraft-chat/src/text_component.rs | 6 |
4 files changed, 21 insertions, 23 deletions
diff --git a/minecraft-chat/src/base_component.rs b/minecraft-chat/src/base_component.rs index b07e08e7..fa39a11c 100644 --- a/minecraft-chat/src/base_component.rs +++ b/minecraft-chat/src/base_component.rs @@ -15,3 +15,9 @@ impl BaseComponent { } } } + +impl Default for BaseComponent { + fn default() -> Self { + Self::new() + } +} diff --git a/minecraft-chat/src/component.rs b/minecraft-chat/src/component.rs index 22e803bd..1ed1f836 100644 --- a/minecraft-chat/src/component.rs +++ b/minecraft-chat/src/component.rs @@ -1,7 +1,4 @@ -use serde::{ - de::{self, Error}, - Deserialize, Deserializer, -}; +use serde::{de, Deserialize, Deserializer}; use crate::{ base_component::BaseComponent, @@ -59,7 +56,7 @@ impl Component { /// Convert this component into an ansi string pub fn to_ansi(&self, default_style: Option<&Style>) -> String { // default the default_style to white if it's not set - let default_style: &Style = default_style.unwrap_or_else(|| &DEFAULT_STYLE); + let default_style: &Style = default_style.unwrap_or(&DEFAULT_STYLE); // this contains the final string will all the ansi escape codes let mut built_string = String::new(); @@ -132,10 +129,10 @@ impl<'de> Deserialize<'de> for Component { if json.get("with").is_some() { let with = json.get("with").unwrap().as_array().unwrap(); let mut with_array = Vec::with_capacity(with.len()); - for i in 0..with.len() { + for item in with { // if it's a string component with no styling and no siblings, just add a string to with_array // otherwise add the component to the array - let c = Component::deserialize(&with[i]).map_err(de::Error::custom)?; + let c = Component::deserialize(item).map_err(de::Error::custom)?; if let Component::Text(text_component) = c { if text_component.base.siblings.is_empty() && text_component.base.style.is_empty() @@ -145,7 +142,7 @@ impl<'de> Deserialize<'de> for Component { } } with_array.push(StringOrComponent::Component( - Component::deserialize(&with[i]).map_err(de::Error::custom)?, + Component::deserialize(item).map_err(de::Error::custom)?, )); } component = diff --git a/minecraft-chat/src/style.rs b/minecraft-chat/src/style.rs index 7b333e5f..4e3b24de 100644 --- a/minecraft-chat/src/style.rs +++ b/minecraft-chat/src/style.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, fmt}; use serde_json::Value; @@ -15,9 +15,9 @@ impl TextColor { let n = u32::from_str_radix(&n, 16).unwrap(); return Ok(TextColor::from_rgb(n)); } - let color = NAMED_COLORS.get(&value.to_ascii_uppercase()); - if color.is_some() { - return Ok(color.unwrap().clone()); + let color_option = NAMED_COLORS.get(&value.to_ascii_uppercase()); + if let Some(color) = color_option { + return Ok(color.clone()); } Err(format!("Invalid color {}", value)) } @@ -52,9 +52,6 @@ lazy_static! { }; } -/// The weird S character Minecraft used to use for chat formatting -const PREFIX_CODE: char = '\u{00a7}'; - #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub struct ChatFormatting<'a> { pub name: &'a str, @@ -178,12 +175,14 @@ impl TextColor { pub fn format(&self) -> String { format!("#{:06X}", self.value) } +} - pub fn to_string(&self) -> String { +impl fmt::Display for TextColor { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if let Some(name) = &self.name { - name.clone() + write!(f, "{}", name.clone()) } else { - self.format() + write!(f, "{}", self.format()) } } } diff --git a/minecraft-chat/src/text_component.rs b/minecraft-chat/src/text_component.rs index 25341086..6c43f8b7 100644 --- a/minecraft-chat/src/text_component.rs +++ b/minecraft-chat/src/text_component.rs @@ -1,10 +1,6 @@ use std::fmt; -use crate::{ - base_component::BaseComponent, - component::Component, - style::{ChatFormatting, Style, TextColor}, -}; +use crate::{base_component::BaseComponent, component::Component, style::ChatFormatting}; #[derive(Clone, Debug)] pub struct TextComponent { |
