aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/src
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-chat/src')
-rwxr-xr-xazalea-chat/src/base_component.rs4
-rwxr-xr-xazalea-chat/src/component.rs104
-rwxr-xr-xazalea-chat/src/lib.rs2
-rwxr-xr-xazalea-chat/src/text_component.rs23
-rwxr-xr-xazalea-chat/src/translatable_component.rs29
5 files changed, 81 insertions, 81 deletions
diff --git a/azalea-chat/src/base_component.rs b/azalea-chat/src/base_component.rs
index ab4f5e5d..43b35aef 100755
--- a/azalea-chat/src/base_component.rs
+++ b/azalea-chat/src/base_component.rs
@@ -1,11 +1,11 @@
-use crate::{style::Style, Component};
+use crate::{style::Style, FormattedText};
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct BaseComponent {
// implements mutablecomponent
#[serde(skip_serializing_if = "Vec::is_empty")]
- pub siblings: Vec<Component>,
+ pub siblings: Vec<FormattedText>,
#[serde(flatten)]
pub style: Style,
}
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())
}
}
diff --git a/azalea-chat/src/lib.rs b/azalea-chat/src/lib.rs
index 97a2580d..d6ff7285 100755
--- a/azalea-chat/src/lib.rs
+++ b/azalea-chat/src/lib.rs
@@ -6,4 +6,4 @@ pub mod style;
pub mod text_component;
pub mod translatable_component;
-pub use component::Component;
+pub use component::FormattedText;
diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs
index 44bcbcf1..42932d0e 100755
--- a/azalea-chat/src/text_component.rs
+++ b/azalea-chat/src/text_component.rs
@@ -1,4 +1,4 @@
-use crate::{base_component::BaseComponent, style::ChatFormatting, Component};
+use crate::{base_component::BaseComponent, style::ChatFormatting, FormattedText};
use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSerializer};
use std::fmt::Display;
@@ -26,7 +26,7 @@ impl Serialize for TextComponent {
const LEGACY_FORMATTING_CODE_SYMBOL: char = 'ยง';
-/// Convert a legacy color code string into a Component
+/// Convert a legacy color code string into a FormattedText
/// Technically in Minecraft this is done when displaying the text, but AFAIK
/// it's the same as just doing it in TextComponent
pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextComponent {
@@ -41,12 +41,9 @@ 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);
- let formatting_code = match formatting_code {
- Some(formatting_code) => formatting_code,
- None => {
- i += 1;
- continue;
- }
+ let Some(formatting_code) = formatting_code else {
+ i += 1;
+ continue;
};
if let Some(formatter) = ChatFormatting::from_code(formatting_code) {
if components.is_empty() || !components.last().unwrap().text.is_empty() {
@@ -98,18 +95,18 @@ impl TextComponent {
}
}
- fn get(self) -> Component {
- Component::Text(self)
+ fn get(self) -> FormattedText {
+ FormattedText::Text(self)
}
}
impl Display for TextComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// this contains the final string will all the ansi escape codes
- for component in Component::Text(self.clone()).into_iter() {
+ for component in FormattedText::Text(self.clone()).into_iter() {
let component_text = match &component {
- Component::Text(c) => c.text.to_string(),
- Component::Translatable(c) => c.read()?.to_string(),
+ FormattedText::Text(c) => c.text.to_string(),
+ FormattedText::Translatable(c) => c.read()?.to_string(),
};
f.write_str(&component_text)?;
diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs
index 7819d5ff..edbb5a6d 100755
--- a/azalea-chat/src/translatable_component.rs
+++ b/azalea-chat/src/translatable_component.rs
@@ -1,7 +1,7 @@
use std::fmt::{self, Display, Formatter};
use crate::{
- base_component::BaseComponent, style::Style, text_component::TextComponent, Component,
+ base_component::BaseComponent, style::Style, text_component::TextComponent, FormattedText,
};
use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSerializer};
@@ -9,7 +9,7 @@ use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSer
#[serde(untagged)]
pub enum StringOrComponent {
String(String),
- Component(Component),
+ FormattedText(FormattedText),
}
/// A message whose content depends on the client's language.
@@ -42,7 +42,7 @@ impl TranslatableComponent {
}
}
- /// Convert the key and args to a Component.
+ /// Convert the key and args to a FormattedText.
pub fn read(&self) -> Result<TextComponent, fmt::Error> {
let template = azalea_language::get(&self.key).unwrap_or(&self.key);
// decode the % things
@@ -57,12 +57,9 @@ impl TranslatableComponent {
while i < template.len() {
if template.chars().nth(i).unwrap() == '%' {
- let char_after = match template.chars().nth(i + 1) {
- Some(c) => c,
- None => {
- built_text.push(template.chars().nth(i).unwrap());
- break;
- }
+ let Some(char_after) = template.chars().nth(i + 1) else {
+ built_text.push(template.chars().nth(i).unwrap());
+ break;
};
i += 1;
match char_after {
@@ -111,7 +108,7 @@ impl TranslatableComponent {
built_text.push(template.chars().nth(i).unwrap());
}
- i += 1
+ i += 1;
}
if components.is_empty() {
@@ -122,7 +119,7 @@ impl TranslatableComponent {
Ok(TextComponent {
base: BaseComponent {
- siblings: components.into_iter().map(Component::Text).collect(),
+ siblings: components.into_iter().map(FormattedText::Text).collect(),
style: Style::default(),
},
text: "".to_string(),
@@ -133,10 +130,10 @@ impl TranslatableComponent {
impl Display for TranslatableComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// this contains the final string will all the ansi escape codes
- for component in Component::Translatable(self.clone()).into_iter() {
+ for component in FormattedText::Translatable(self.clone()).into_iter() {
let component_text = match &component {
- Component::Text(c) => c.text.to_string(),
- Component::Translatable(c) => c.read()?.to_string(),
+ FormattedText::Text(c) => c.text.to_string(),
+ FormattedText::Translatable(c) => c.read()?.to_string(),
};
f.write_str(&component_text)?;
@@ -150,7 +147,7 @@ impl Display for StringOrComponent {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
match self {
StringOrComponent::String(s) => write!(f, "{s}"),
- StringOrComponent::Component(c) => write!(f, "{c}"),
+ StringOrComponent::FormattedText(c) => write!(f, "{c}"),
}
}
}
@@ -159,7 +156,7 @@ impl From<StringOrComponent> for TextComponent {
fn from(soc: StringOrComponent) -> Self {
match soc {
StringOrComponent::String(s) => TextComponent::new(s),
- StringOrComponent::Component(c) => TextComponent::new(c.to_string()),
+ StringOrComponent::FormattedText(c) => TextComponent::new(c.to_string()),
}
}
}