11use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
22use std:: borrow:: Cow ;
3+ use std:: fmt:: { self , Display , Formatter } ;
34use std:: str:: FromStr ;
45use uuid:: Uuid ;
56
@@ -64,7 +65,7 @@ impl From<Color> for Text {
6465 }
6566}
6667
67- #[ derive( Debug , Copy , Clone , PartialEq , Serialize , Deserialize ) ]
68+ #[ derive( Copy , Clone , Debug , PartialEq , Eq , Serialize , Deserialize ) ]
6869#[ serde( rename_all = "snake_case" ) ]
6970pub enum Style {
7071 Bold ,
@@ -95,7 +96,7 @@ impl From<Style> for Text {
9596 }
9697}
9798
98- #[ derive( Debug , PartialEq ) ]
99+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
99100/// Represent all possible keybinds in vanilla.
100101pub enum Keybind {
101102 Attack ,
@@ -108,7 +109,7 @@ pub enum Keybind {
108109 Sneak ,
109110 Sprint ,
110111 Drop ,
111- Iventory ,
112+ Inventory ,
112113 Chat ,
113114 ListPlayers ,
114115 PickBlock ,
@@ -176,7 +177,7 @@ where
176177 "key_key.sneak" => Keybind :: Sneak ,
177178 "key_key.sprint" => Keybind :: Sprint ,
178179 "key_key.drop" => Keybind :: Drop ,
179- "key_key.inventory" => Keybind :: Iventory ,
180+ "key_key.inventory" => Keybind :: Inventory ,
180181 "key_key.chat" => Keybind :: Chat ,
181182 "key_key.playerlist" => Keybind :: ListPlayers ,
182183 "key_key.pickItem" => Keybind :: PickBlock ,
@@ -217,7 +218,7 @@ impl From<&Keybind> for String {
217218 Keybind :: Sneak => "key_key.sneak" ,
218219 Keybind :: Sprint => "key_key.sprint" ,
219220 Keybind :: Drop => "key_key.drop" ,
220- Keybind :: Iventory => "key_key.inventory" ,
221+ Keybind :: Inventory => "key_key.inventory" ,
221222 Keybind :: Chat => "key_key.chat" ,
222223 Keybind :: ListPlayers => "key_key.playerlist" ,
223224 Keybind :: PickBlock => "key_key.pickItem" ,
@@ -246,7 +247,7 @@ impl From<&Keybind> for String {
246247 }
247248}
248249
249- #[ derive( Debug , PartialEq ) ]
250+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
250251/// Represent all possible translation keys in vanilla.
251252pub enum Translate {
252253 ChatTypeText ,
@@ -309,7 +310,7 @@ impl<'a> From<&Translate> for String {
309310 }
310311}
311312
312- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
313+ #[ derive( Clone , Debug , PartialEq , Eq , Serialize , Deserialize ) ]
313314#[ serde( tag = "action" , content = "value" ) ]
314315// TODO: Accept any json primitive as string
315316pub enum Click {
@@ -322,14 +323,14 @@ pub enum Click {
322323}
323324
324325#[ serde_with:: skip_serializing_none]
325- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
326+ #[ derive( Clone , Debug , PartialEq , Eq , Serialize , Deserialize ) ]
326327pub struct Entity {
327328 id : Uuid ,
328329 ty : Option < Cow < ' static , str > > ,
329330 name : Cow < ' static , str > ,
330331}
331332
332- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
333+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
333334#[ serde( tag = "action" , content = "value" ) ]
334335// TODO: Accept any json primitive as string
335336pub enum Hover {
@@ -342,7 +343,7 @@ pub enum Hover {
342343 ShowEntity ( Entity ) ,
343344}
344345
345- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
346+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
346347#[ serde( untagged) ]
347348/// Text component can either be Text, Translate, Score, Selector, Keybind, or Nbt.
348349pub enum TextValue {
@@ -424,7 +425,7 @@ impl TextValue {
424425}
425426
426427#[ serde_with:: skip_serializing_none]
427- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
428+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
428429/// Text json object that holds all styles.
429430pub struct TextComponent {
430431 #[ serde( flatten) ]
@@ -895,7 +896,7 @@ impl From<Text> for TextComponent {
895896}
896897
897898/// Text can either be a json String, Object, or an Array.
898- #[ derive( Debug , PartialEq , Serialize , Deserialize ) ]
899+ #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
899900#[ serde( untagged) ]
900901pub enum Text {
901902 String ( Cow < ' static , str > ) ,
@@ -952,6 +953,18 @@ impl Text {
952953 }
953954}
954955
956+ impl From < Text > for String {
957+ fn from ( text : Text ) -> Self {
958+ TextRoot ( text) . into ( )
959+ }
960+ }
961+
962+ impl Display for Text {
963+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
964+ f. write_str ( & serde_json:: to_string ( self ) . unwrap ( ) )
965+ }
966+ }
967+
955968impl From < TextComponent > for Text {
956969 fn from ( component : TextComponent ) -> Self {
957970 Text :: Component ( Box :: new ( component) )
@@ -994,19 +1007,13 @@ impl std::ops::Add<Text> for Text {
9941007 }
9951008}
9961009
997- impl From < Text > for String {
998- fn from ( text : Text ) -> String {
999- serde_json:: to_string ( & text) . unwrap ( )
1000- }
1001- }
1002-
10031010/// Ensures Text is either an Array or Object.
10041011/// This is required at some places when sending to the client.
10051012pub struct TextRoot ( Text ) ;
10061013
10071014impl From < TextRoot > for String {
10081015 fn from ( text : TextRoot ) -> String {
1009- text. 0 . into ( )
1016+ text. 0 . to_string ( )
10101017 }
10111018}
10121019
0 commit comments