aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/src/text_component.rs
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-chat/src/text_component.rs')
-rw-r--r--azalea-chat/src/text_component.rs18
1 files changed, 13 insertions, 5 deletions
diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs
index bd598e16..eab7ee79 100644
--- a/azalea-chat/src/text_component.rs
+++ b/azalea-chat/src/text_component.rs
@@ -5,21 +5,24 @@ use serde::{__private::ser::FlatMapSerializer, Serialize, Serializer, ser::Seria
use crate::{
FormattedText,
base_component::BaseComponent,
- style::{ChatFormatting, TextColor},
+ style::{ChatFormatting, Style, TextColor},
};
/// A component that contains text that's the same in all locales.
-#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
+#[derive(Clone, Debug, Default, PartialEq)]
pub struct TextComponent {
pub base: BaseComponent,
pub text: String,
}
-
impl Serialize for TextComponent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
+ if self.base == BaseComponent::default() {
+ return serializer.serialize_str(&self.text);
+ }
+
let mut state = serializer.serialize_map(None)?;
state.serialize_entry("text", &self.text)?;
Serialize::serialize(&self.base, FlatMapSerializer(&mut state))?;
@@ -85,7 +88,7 @@ pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextCompo
components.push(TextComponent::new("".to_string()));
}
let style = &mut components.last_mut().unwrap().base.style;
- style.color = TextColor::parse(color);
+ style.color = TextColor::parse(&color);
i += 6;
} else if let Some(formatter) = ChatFormatting::from_code(formatting_code) {
@@ -124,7 +127,8 @@ pub fn legacy_color_code_to_text_component(legacy_color_code: &str) -> TextCompo
}
impl TextComponent {
- pub fn new(text: String) -> Self {
+ pub fn new(text: impl Into<String>) -> Self {
+ let text = text.into();
// if it contains a LEGACY_FORMATTING_CODE_SYMBOL, format it
if text.contains(LEGACY_FORMATTING_CODE_SYMBOL) {
legacy_color_code_to_text_component(&text)
@@ -139,6 +143,10 @@ impl TextComponent {
fn get(self) -> FormattedText {
FormattedText::Text(self)
}
+ pub fn with_style(mut self, style: Style) -> Self {
+ self.base.style = Box::new(style);
+ self
+ }
}
impl Display for TextComponent {