//! Text components from commands: `ComponentArgument` reads an SNBT (or JSON) //! value, `ComponentSerialization` turns it into styled text. use crate::text::{Style, Text, named_color}; use serde_json::{Map, Value}; /// `SnbtGrammar`, enough for components: compounds, lists, quoted and /// unquoted strings, numbers (with type suffixes) and booleans. Returns the /// value and the bytes consumed. pub fn parse_snbt(input: &str) -> Result<(Value, usize), String> { let mut p = Parser { s: input.as_bytes(), i: 0, }; let v = p.value()?; Ok((v, p.i)) } struct Parser<'a> { s: &'a [u8], i: usize, } impl Parser<'_> { fn ws(&mut self) { while self.i < self.s.len() && self.s[self.i].is_ascii_whitespace() { self.i += 1; } } fn peek(&self) -> Option { self.s.get(self.i).copied() } fn expect(&mut self, c: u8) -> Result<(), String> { self.ws(); if self.peek() == Some(c) { self.i += 1; Ok(()) } else { Err(format!("expected '{}'", c as char)) } } fn value(&mut self) -> Result { self.ws(); match self.peek() { Some(b'{') => { self.i += 1; let mut map = Map::new(); self.ws(); if self.peek() == Some(b'}') { self.i += 1; return Ok(Value::Object(map)); } loop { self.ws(); let key = self.string()?; self.expect(b':')?; map.insert(key, self.value()?); self.ws(); match self.peek() { Some(b',') => self.i += 1, Some(b'}') => { self.i += 1; return Ok(Value::Object(map)); } _ => return Err("expected ',' or '}'".into()), } } } Some(b'[') => { self.i += 1; let mut list = Vec::new(); self.ws(); // Typed arrays [B;...], [I;...], [L;...]. if matches!( self.s.get(self.i..self.i + 2), Some([b'B' | b'I' | b'L', b';']) ) { self.i += 2; } self.ws(); if self.peek() == Some(b']') { self.i += 1; return Ok(Value::Array(list)); } loop { list.push(self.value()?); self.ws(); match self.peek() { Some(b',') => self.i += 1, Some(b']') => { self.i += 1; return Ok(Value::Array(list)); } _ => return Err("expected ',' or ']'".into()), } } } Some(b'"' | b'\'') => Ok(Value::String(self.string()?)), Some(_) => { let word = self.unquoted(); if word.is_empty() { return Err("expected a value".into()); } Ok(match word.as_str() { "true" => Value::Bool(true), "false" => Value::Bool(false), _ => { let digits = word .trim_end_matches(['b', 'B', 's', 'S', 'l', 'L', 'f', 'F', 'd', 'D']); match (digits.parse::(), digits.parse::()) { (Ok(n), _) if digits.len() + 1 >= word.len() => Value::from(n), (_, Ok(f)) if digits.len() + 1 >= word.len() => Value::from(f), _ => Value::String(word), } } }) } None => Err("expected a value".into()), } } fn unquoted(&mut self) -> String { let start = self.i; while let Some(c) = self.peek() { if c.is_ascii_alphanumeric() || matches!(c, b'_' | b'-' | b'.' | b'+') { self.i += 1; } else { break; } } String::from_utf8_lossy(&self.s[start..self.i]).into_owned() } fn string(&mut self) -> Result { self.ws(); let Some(q @ (b'"' | b'\'')) = self.peek() else { return Ok(self.unquoted()); }; self.i += 1; let mut out = Vec::new(); while let Some(c) = self.peek() { self.i += 1; match c { b'\\' => { let Some(e) = self.peek() else { break }; self.i += 1; out.push(match e { b'n' => b'\n', b't' => b'\t', other => other, }); } c if c == q => return Ok(String::from_utf8_lossy(&out).into_owned()), c => out.push(c), } } Err("unterminated string".into()) } } /// The style fields of a component object. fn object_style(o: &serde_json::Map) -> Style { let mut style = Style::default(); if let Some(c) = o.get("color").and_then(Value::as_str) { style.color = named_color(c).or_else(|| { c.strip_prefix('#') .and_then(|h| u32::from_str_radix(h, 16).ok()) }); } let flag = |k: &str| o.get(k).and_then(Value::as_bool); style.bold = flag("bold"); style.italic = flag("italic"); style.underlined = flag("underlined"); style.strikethrough = flag("strikethrough"); style.obfuscated = flag("obfuscated"); style.font = o.get("font").and_then(crate::text::font_id); // ClickEvent.CODEC: the actions a chat line acts on here. A component // from a server never opens a file. if let Some(e) = o.get("click_event").and_then(Value::as_object) { let text = |k: &str| e.get(k).and_then(Value::as_str).map(str::to_owned); style.click = match e.get("action").and_then(Value::as_str) { Some("suggest_command") => text("command").map(crate::text::ClickEvent::SuggestCommand), Some("copy_to_clipboard") => { text("value").map(crate::text::ClickEvent::CopyToClipboard) } _ => None, }; } // HoverEvent.ShowText. if let Some(e) = o.get("hover_event").and_then(Value::as_object) && e.get("action").and_then(Value::as_str) == Some("show_text") && let Some(value) = e.get("value") { style.hover = Some(Box::new(to_text(value, &mut |s| Text::literal(s)))); } style } /// `ComponentSerialization.CODEC` decoding into text. `selector` components /// are resolved by `resolve` (ComponentUtils.updateForEntity). pub fn to_text(v: &Value, resolve: &mut dyn FnMut(&str) -> Text) -> Text { to_text_with(v, resolve, &mut |_| None) } /// `to_text` with a hook resolving other contents (`nbt`) from the object. pub fn to_text_with( v: &Value, resolve: &mut dyn FnMut(&str) -> Text, contents: &mut dyn FnMut(&serde_json::Map) -> Option, ) -> Text { match v { Value::String(s) => Text::literal(s.clone()), Value::Number(n) => Text::literal(n.to_string()), Value::Bool(b) => Text::literal(b.to_string()), // A list is its first element with the rest as siblings. Value::Array(list) => { let Some((first, rest)) = list.split_first() else { return Text::empty(); }; let mut base = to_text_with(first, resolve, contents); // The siblings take the first component's own style, even when // its text is empty. let style = match first { Value::Object(o) => object_style(o), _ => base .spans .first() .map(|s| s.style.clone()) .unwrap_or_default(), }; for sibling in rest { base.push(to_text_with(sibling, resolve, contents).with_style(&style)); } base } Value::Object(o) => { let style = object_style(o); let content = if let Some(t) = contents(o) { t } else if let Some(t) = o.get("text") { match t { Value::String(s) => Text::literal(s.clone()), other => Text::literal(other.to_string()), } } else if let Some(key) = o.get("translate").and_then(Value::as_str) { let args: Vec = o .get("with") .and_then(Value::as_array) .map(|a| { a.iter() .map(|x| to_text_with(x, resolve, contents)) .collect() }) .unwrap_or_default(); let translated = Text::translate(key, &args); match o.get("fallback").and_then(Value::as_str) { Some(fallback) if translated.plain() == key => Text::translate(fallback, &args), _ => translated, } } else if let Some(sel) = o.get("selector").and_then(Value::as_str) { resolve(sel) } else if let Some(key) = o.get("keybind").and_then(Value::as_str) { crate::key_display::display_name(key) } else { Text::empty() }; let mut out = content.with_style(&style); for extra in o .get("extra") .and_then(Value::as_array) .into_iter() .flatten() { out.push(to_text_with(extra, resolve, contents).with_style(&style)); } out } Value::Null => Text::empty(), } } #[cfg(test)] mod tests { use super::*; #[test] fn snbt_and_json_components() { let input = "{text:'Hi ',color:red,extra:[{text:\"there\",bold:1b}]} rest"; let (v, n) = parse_snbt(input).unwrap(); assert_eq!(&input[n..], " rest"); let t = to_text(&v, &mut |_| Text::empty()); assert_eq!(t.plain(), "Hi there"); assert_eq!(t.spans[1].style.color, named_color("red")); let (v, _) = parse_snbt("[\"a\",{\"text\":\"b\"}]").unwrap(); assert_eq!(to_text(&v, &mut |_| Text::empty()).plain(), "ab"); } #[test] fn the_font_is_inherited_and_kept() { let (v, _) = parse_snbt(r#"{text:"a",font:"alt",extra:["b",{text:"c",font:"minecraft:uniform"}]}"#) .unwrap(); let t = to_text(&v, &mut |_| Text::empty()); let fonts: Vec<_> = t.chars().map(|(_, s)| s.font.clone()).collect(); let (alt, uniform) = ( Some("minecraft:alt".into()), Some("minecraft:uniform".into()), ); assert_eq!(fonts, [alt.clone(), alt, uniform]); // Back to JSON with the field, which a re-read keeps. let again = to_text(&t.component(), &mut |_| Text::empty()); let kept: Vec<_> = again.chars().map(|(_, s)| s.font.clone()).collect(); assert_eq!(kept, fonts); // Not an identifier: no font. let (v, _) = parse_snbt(r#"{text:"a",font:"Bad Font"}"#).unwrap(); let t = to_text(&v, &mut |_| Text::empty()); assert_eq!(t.spans[0].style.font, None); } }