diff options
Diffstat (limited to 'azalea-chat/src/component.rs')
| -rwxr-xr-x | azalea-chat/src/component.rs | 104 |
1 files changed, 55 insertions, 49 deletions
diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 14ec0d0c..59e11bbf 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -17,7 +17,7 @@ use std::{ /// A chat component, basically anything you can see in chat. #[derive(Clone, Debug, PartialEq, Serialize)] #[serde(untagged)] -pub enum Component { +pub enum FormattedText { Text(TextComponent), Translatable(TranslatableComponent), } @@ -28,7 +28,7 @@ pub static DEFAULT_STYLE: Lazy<Style> = Lazy::new(|| Style { }); /// A chat component -impl Component { +impl FormattedText { pub fn get_base_mut(&mut self) -> &mut BaseComponent { match self { Self::Text(c) => &mut c.base, @@ -44,14 +44,16 @@ impl Component { } /// Add a component as a sibling of this one - fn append(&mut self, sibling: Component) { + fn append(&mut self, sibling: FormattedText) { self.get_base_mut().siblings.push(sibling); } /// Get the "separator" component from the json - fn parse_separator(json: &serde_json::Value) -> Result<Option<Component>, serde_json::Error> { + fn parse_separator( + json: &serde_json::Value, + ) -> Result<Option<FormattedText>, serde_json::Error> { if json.get("separator").is_some() { - return Ok(Some(Component::deserialize( + return Ok(Some(FormattedText::deserialize( json.get("separator").unwrap(), )?)); } @@ -62,16 +64,17 @@ impl Component { /// [ANSI string](https://en.wikipedia.org/wiki/ANSI_escape_code), so you /// can print it to your terminal and get styling. /// - /// This is technically a shortcut for [`Component::to_ansi_custom_style`] - /// with a default [`Style`] colored white. + /// This is technically a shortcut for + /// [`FormattedText::to_ansi_custom_style`] with a default [`Style`] + /// colored white. /// /// # Examples /// /// ```rust - /// use azalea_chat::Component; + /// use azalea_chat::FormattedText; /// use serde::de::Deserialize; /// - /// let component = Component::deserialize(&serde_json::json!({ + /// let component = FormattedText::deserialize(&serde_json::json!({ /// "text": "Hello, world!", /// "color": "red", /// })).unwrap(); @@ -86,7 +89,7 @@ impl Component { /// Convert this component into an /// [ANSI string](https://en.wikipedia.org/wiki/ANSI_escape_code). /// - /// This is the same as [`Component::to_ansi`], but you can specify a + /// This is the same as [`FormattedText::to_ansi`], but you can specify a /// default [`Style`] to use. pub fn to_ansi_custom_style(&self, default_style: &Style) -> String { // this contains the final string will all the ansi escape codes @@ -117,12 +120,12 @@ impl Component { } } -impl IntoIterator for Component { +impl IntoIterator for FormattedText { /// Recursively call the function for every component in this component fn into_iter(self) -> Self::IntoIter { let base = self.get_base(); let siblings = base.siblings.clone(); - let mut v: Vec<Component> = Vec::with_capacity(siblings.len() + 1); + let mut v: Vec<FormattedText> = Vec::with_capacity(siblings.len() + 1); v.push(self); for sibling in siblings { v.extend(sibling.into_iter()); @@ -131,11 +134,11 @@ impl IntoIterator for Component { v.into_iter() } - type Item = Component; + type Item = FormattedText; type IntoIter = std::vec::IntoIter<Self::Item>; } -impl<'de> Deserialize<'de> for Component { +impl<'de> Deserialize<'de> for FormattedText { fn deserialize<D>(de: D) -> Result<Self, D::Error> where D: Deserializer<'de>, @@ -143,11 +146,11 @@ impl<'de> Deserialize<'de> for Component { let json: serde_json::Value = serde::Deserialize::deserialize(de)?; // we create a component that we might add siblings to - let mut component: Component; + let mut component: FormattedText; // if it's primitive, make it a text component if !json.is_array() && !json.is_object() { - return Ok(Component::Text(TextComponent::new( + return Ok(FormattedText::Text(TextComponent::new( json.as_str().unwrap_or("").to_string(), ))); } @@ -155,7 +158,7 @@ impl<'de> Deserialize<'de> for Component { else if json.is_object() { if let Some(text) = json.get("text") { let text = text.as_str().unwrap_or("").to_string(); - component = Component::Text(TextComponent::new(text)); + component = FormattedText::Text(TextComponent::new(text)); } else if let Some(translate) = json.get("translate") { let translate = translate .as_str() @@ -170,8 +173,8 @@ impl<'de> Deserialize<'de> for Component { // 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(item).map_err(de::Error::custom)?; - if let Component::Text(text_component) = c { + let c = FormattedText::deserialize(item).map_err(de::Error::custom)?; + if let FormattedText::Text(text_component) = c { if text_component.base.siblings.is_empty() && text_component.base.style.is_empty() { @@ -179,16 +182,19 @@ impl<'de> Deserialize<'de> for Component { continue; } } - with_array.push(StringOrComponent::Component( - Component::deserialize(item).map_err(de::Error::custom)?, + with_array.push(StringOrComponent::FormattedText( + FormattedText::deserialize(item).map_err(de::Error::custom)?, )); } - component = - Component::Translatable(TranslatableComponent::new(translate, with_array)); + component = FormattedText::Translatable(TranslatableComponent::new( + translate, with_array, + )); } else { // if it doesn't have a "with", just have the with_array be empty - component = - Component::Translatable(TranslatableComponent::new(translate, Vec::new())); + component = FormattedText::Translatable(TranslatableComponent::new( + translate, + Vec::new(), + )); } } else if let Some(score) = json.get("score") { // object = GsonHelper.getAsJsonObject(jsonObject, "score"); @@ -210,14 +216,13 @@ impl<'de> Deserialize<'de> for Component { "keybind text components aren't yet supported", )); } else { - let _nbt = if let Some(nbt) = json.get("nbt") { - nbt - } else { + let Some(_nbt) = json.get("nbt") else { return Err(de::Error::custom( - format!("Don't know how to turn {json} into a Component").as_str(), + format!("Don't know how to turn {json} into a FormattedText").as_str(), )); }; - let _separator = Component::parse_separator(&json).map_err(de::Error::custom)?; + let _separator = + FormattedText::parse_separator(&json).map_err(de::Error::custom)?; let _interpret = match json.get("interpret") { Some(v) => v.as_bool().ok_or(Some(false)).unwrap(), @@ -229,16 +234,15 @@ impl<'de> Deserialize<'de> for Component { )); } if let Some(extra) = json.get("extra") { - let extra = match extra.as_array() { - Some(r) => r, - None => return Err(de::Error::custom("Extra isn't an array")), + let Some(extra) = extra.as_array() else { + return Err(de::Error::custom("Extra isn't an array")); }; if extra.is_empty() { return Err(de::Error::custom("Unexpected empty array of components")); } for extra_component in extra { let sibling = - Component::deserialize(extra_component).map_err(de::Error::custom)?; + FormattedText::deserialize(extra_component).map_err(de::Error::custom)?; component.append(sibling); } } @@ -251,16 +255,18 @@ impl<'de> Deserialize<'de> for Component { // ok so it's not an object, if it's an array deserialize every item else if !json.is_array() { return Err(de::Error::custom( - format!("Don't know how to turn {json} into a Component").as_str(), + format!("Don't know how to turn {json} into a FormattedText").as_str(), )); } let json_array = json.as_array().unwrap(); // the first item in the array is the one that we're gonna return, the others // are siblings - let mut component = Component::deserialize(&json_array[0]).map_err(de::Error::custom)?; + let mut component = + FormattedText::deserialize(&json_array[0]).map_err(de::Error::custom)?; for i in 1..json_array.len() { component.append( - Component::deserialize(json_array.get(i).unwrap()).map_err(de::Error::custom)?, + FormattedText::deserialize(json_array.get(i).unwrap()) + .map_err(de::Error::custom)?, ); } Ok(component) @@ -268,18 +274,18 @@ impl<'de> Deserialize<'de> for Component { } #[cfg(feature = "azalea-buf")] -impl McBufReadable for Component { +impl McBufReadable for FormattedText { fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { let string = String::read_from(buf)?; - debug!("Component string: {}", string); + debug!("FormattedText string: {}", string); let json: serde_json::Value = serde_json::from_str(string.as_str())?; - let component = Component::deserialize(json)?; + let component = FormattedText::deserialize(json)?; Ok(component) } } #[cfg(feature = "azalea-buf")] -impl McBufWritable for Component { +impl McBufWritable for FormattedText { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { let json = serde_json::to_string(self).unwrap(); json.write_into(buf)?; @@ -287,31 +293,31 @@ impl McBufWritable for Component { } } -impl From<String> for Component { +impl From<String> for FormattedText { fn from(s: String) -> Self { - Component::Text(TextComponent { + FormattedText::Text(TextComponent { text: s, base: BaseComponent::default(), }) } } -impl From<&str> for Component { +impl From<&str> for FormattedText { fn from(s: &str) -> Self { Self::from(s.to_string()) } } -impl Display for Component { +impl Display for FormattedText { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Component::Text(c) => c.fmt(f), - Component::Translatable(c) => c.fmt(f), + FormattedText::Text(c) => c.fmt(f), + FormattedText::Translatable(c) => c.fmt(f), } } } -impl Default for Component { +impl Default for FormattedText { fn default() -> Self { - Component::Text(TextComponent::default()) + FormattedText::Text(TextComponent::default()) } } |
