Use Cow's in TextComponent
We now don't always convert &str to Strings allowing us to use stack allocations
This commit is contained in:
1 parent
dfc0b8afe9
commit
a6d9ed216d
26 files changed
+134
-149
No files matched your search
@@ -1,19 +1,21 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Action to take on click of the text.
|
||||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "action", content = "value", rename_all = "snake_case")]
|
||||
pub enum ClickEvent {
|
||||
pub enum ClickEvent<'a> {
|
||||
/// Opens a URL
|
||||
OpenUrl(String),
|
||||
OpenUrl(Cow<'a, str>),
|
||||
/// Works in signs, but only on the root text component
|
||||
RunCommand(String),
|
||||
RunCommand(Cow<'a, str>),
|
||||
/// Replaces the contents of the chat box with the text, not necessarily a
|
||||
/// command.
|
||||
SuggestCommand(String),
|
||||
SuggestCommand(Cow<'a, str>),
|
||||
/// Only usable within written books. Changes the page of the book. Indexing
|
||||
/// starts at 1.
|
||||
ChangePage(i32),
|
||||
/// Copies the given text to system clipboard
|
||||
CopyToClipboard(String),
|
||||
CopyToClipboard(Cow<'a, str>),
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Text;
|
||||
@@ -5,17 +7,17 @@ use crate::Text;
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "action", content = "contents", rename_all = "snake_case")]
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
pub enum HoverEvent {
|
||||
pub enum HoverEvent<'a> {
|
||||
/// Displays a tooltip with the given text.
|
||||
ShowText(Text),
|
||||
ShowText(Text<'a>),
|
||||
/// Shows an item.
|
||||
ShowItem {
|
||||
/// Resource identifier of the item
|
||||
id: String,
|
||||
id: Cow<'a, str>,
|
||||
/// Number of the items in the stack
|
||||
count: Option<i32>,
|
||||
/// NBT information about the item (sNBT format)
|
||||
tag: String,
|
||||
tag: Cow<'a, str>,
|
||||
},
|
||||
/// Shows an entity.
|
||||
ShowEntity {
|
||||
@@ -24,9 +26,9 @@ pub enum HoverEvent {
|
||||
/// Resource identifier of the entity
|
||||
#[serde(rename = "type")]
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
kind: Option<String>,
|
||||
kind: Option<Cow<'a, str>>,
|
||||
/// Optional custom name for the entity
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
name: Option<Text>,
|
||||
name: Option<Text<'a>>,
|
||||
},
|
||||
}
|
||||
+29
-45
@@ -1,4 +1,5 @@
|
||||
use core::str;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use click::ClickEvent;
|
||||
use color::Color;
|
||||
@@ -12,25 +13,34 @@ pub mod color;
|
||||
pub mod hover;
|
||||
pub mod style;
|
||||
|
||||
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct Text(pub Box<TextComponent>);
|
||||
pub struct Text<'a>(pub Box<TextComponent<'a>>);
|
||||
|
||||
// Represents a Text component
|
||||
// Reference: https://wiki.vg/Text_formatting#Text_components
|
||||
#[derive(Clone, Default, Debug, Deserialize)]
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TextComponent {
|
||||
pub struct TextComponent<'a> {
|
||||
/// The actual text
|
||||
#[serde(flatten)]
|
||||
pub content: TextContent,
|
||||
pub content: TextContent<'a>,
|
||||
/// Style of the text. Bold, Italic, underline, Color...
|
||||
/// Also has `ClickEvent
|
||||
#[serde(flatten)]
|
||||
pub style: Style,
|
||||
pub style: Style<'a>,
|
||||
}
|
||||
|
||||
impl serde::Serialize for TextComponent {
|
||||
impl<'a> TextComponent<'a> {
|
||||
pub fn text(text: &'a str) -> Self {
|
||||
Self {
|
||||
content: TextContent::Text { text: text.into() },
|
||||
style: Style::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::Serialize for TextComponent<'a> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
@@ -39,7 +49,7 @@ impl serde::Serialize for TextComponent {
|
||||
}
|
||||
}
|
||||
|
||||
impl TextComponent {
|
||||
impl<'a> TextComponent<'a> {
|
||||
pub fn color(mut self, color: Color) -> Self {
|
||||
self.style.color = Some(color);
|
||||
self
|
||||
@@ -87,13 +97,13 @@ impl TextComponent {
|
||||
}
|
||||
|
||||
/// Allows for events to occur when the player clicks on text. Only work in chat.
|
||||
pub fn click_event(mut self, event: ClickEvent) -> Self {
|
||||
pub fn click_event(mut self, event: ClickEvent<'a>) -> Self {
|
||||
self.style.click_event = Some(event);
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
|
||||
pub fn hover_event(mut self, event: HoverEvent) -> Self {
|
||||
pub fn hover_event(mut self, event: HoverEvent<'a>) -> Self {
|
||||
self.style.hover_event = Some(event);
|
||||
self
|
||||
}
|
||||
@@ -104,9 +114,9 @@ impl TextComponent {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct TempStruct<'a> {
|
||||
#[serde(flatten)]
|
||||
text: &'a TextContent,
|
||||
text: &'a TextContent<'a>,
|
||||
#[serde(flatten)]
|
||||
style: &'a Style,
|
||||
style: &'a Style<'a>,
|
||||
}
|
||||
let astruct = TempStruct {
|
||||
text: &self.content,
|
||||
@@ -118,50 +128,24 @@ impl TextComponent {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for TextComponent {
|
||||
fn from(value: String) -> Self {
|
||||
Self {
|
||||
content: TextContent::Text { text: value },
|
||||
style: Style::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for TextComponent {
|
||||
fn from(value: &str) -> Self {
|
||||
Self {
|
||||
content: TextContent::Text {
|
||||
text: value.to_string(),
|
||||
},
|
||||
style: Style::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum TextContent {
|
||||
pub enum TextContent<'a> {
|
||||
/// Raw Text
|
||||
Text { text: String },
|
||||
Text { text: Cow<'a, str> },
|
||||
/// Translated text
|
||||
Translate {
|
||||
translate: String,
|
||||
translate: Cow<'a, str>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
with: Vec<Text>,
|
||||
with: Vec<Text<'a>>,
|
||||
},
|
||||
/// Displays the name of one or more entities found by a selector.
|
||||
EntityNames {
|
||||
selector: String,
|
||||
selector: Cow<'a, str>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
separator: Option<Text>,
|
||||
separator: Option<Cow<'a, str>>,
|
||||
},
|
||||
/// A keybind identifier
|
||||
/// https://minecraft.fandom.com/wiki/Controls#Configurable_controls
|
||||
Keybind { keybind: String },
|
||||
}
|
||||
|
||||
impl Default for TextContent {
|
||||
fn default() -> Self {
|
||||
Self::Text { text: "".into() }
|
||||
}
|
||||
Keybind { keybind: Cow<'a, str> },
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use crate::{
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
||||
pub struct Style {
|
||||
pub struct Style<'a> {
|
||||
/// Changes the color to render the content
|
||||
pub color: Option<Color>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -33,13 +33,13 @@ pub struct Style {
|
||||
pub insertion: Option<String>,
|
||||
/// Allows for events to occur when the player clicks on text. Only work in chat.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub click_event: Option<ClickEvent>,
|
||||
pub click_event: Option<ClickEvent<'a>>,
|
||||
/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub hover_event: Option<HoverEvent>,
|
||||
pub hover_event: Option<HoverEvent<'a>>,
|
||||
}
|
||||
|
||||
impl Style {
|
||||
impl<'a> Style<'a> {
|
||||
pub fn color(mut self, color: Color) -> Self {
|
||||
self.color = Some(color);
|
||||
self
|
||||
@@ -87,13 +87,13 @@ impl Style {
|
||||
}
|
||||
|
||||
/// Allows for events to occur when the player clicks on text. Only work in chat.
|
||||
pub fn click_event(mut self, event: ClickEvent) -> Self {
|
||||
pub fn click_event(mut self, event: ClickEvent<'a>) -> Self {
|
||||
self.click_event = Some(event);
|
||||
self
|
||||
}
|
||||
|
||||
/// Allows for a tooltip to be displayed when the player hovers their mouse over text.
|
||||
pub fn hover_event(mut self, event: HoverEvent) -> Self {
|
||||
pub fn hover_event(mut self, event: HoverEvent<'a>) -> Self {
|
||||
self.hover_event = Some(event);
|
||||
self
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user