From e3845a0dbfce87a7be9b31ef201588b0a4f232a9 Mon Sep 17 00:00:00 2001 From: Kezi Date: Thu, 4 Mar 2021 04:28:43 +0100 Subject: [PATCH] protocol: fix panic in chat formatting, closes #505 (#506) * remove panic on chat thing * fmt --- protocol/src/format.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/protocol/src/format.rs b/protocol/src/format.rs index 36744cd..6f433c1 100644 --- a/protocol/src/format.rs +++ b/protocol/src/format.rs @@ -43,8 +43,8 @@ impl Component { }) } else if v.get("text").is_some() { Component::Text(TextComponent::from_value(v, modifier)) - } else if v.get("translate").is_some() { - let translate_key = v.get("translate").unwrap().as_str().unwrap(); + } else if let Some(translate) = v.get("translate") { + let translate_key = translate.as_str().unwrap_or_default(); if let Some(serde_json::Value::Array(args)) = v.get("with") { // TODO: recursively build components, avoid throwing away all but "text" let text_args: Vec<&str> = args @@ -56,14 +56,18 @@ impl Component { if let Some(serde_json::Value::Array(extra)) = obj.get("extra") { if let Some(item) = extra.get(0) { if let Some(text) = item.get("text") { - return text.as_str().unwrap(); + return text.as_str().unwrap_or_default(); } } } - obj.get("text").unwrap().as_str().unwrap() + if let Some(text) = obj.get("text") { + text.as_str().unwrap_or_default() + } else { + Default::default() + } } else { - v.as_str().unwrap() + v.as_str().unwrap_or_default() } }) .collect();