From dd027bff4bcc4e51e0fedff18bd3f9638b0fa543 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 13 Jan 2021 18:19:40 -0800 Subject: [PATCH] Refactor flair parsing --- src/utils.rs | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index a82cb3d..fb80160 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -201,26 +201,30 @@ pub async fn media(data: &serde_json::Value) -> (String, String) { } pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec>, text_flair: Option<&str>) -> Vec { - let mut result: Vec = Vec::new(); - if flair_type == "richtext" && !rich_flair.is_none() { - for part in rich_flair.unwrap() { - let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); - let value = if flair_part_type == "text" { - part["t"].as_str().unwrap_or_default().to_string() - } else if flair_part_type == "emoji" { - format_url(part["u"].as_str().unwrap_or_default()) - } else { - "".to_string() - }; - result.push(FlairPart { flair_part_type, value }); - } - } else if flair_type == "text" && !text_flair.is_none() { - result.push(FlairPart { - flair_part_type: "text".to_string(), - value: text_flair.unwrap().to_string(), - }); + match flair_type.as_str() { + "richtext" => match rich_flair { + Some(rich) => rich.iter().map(|part| { + let value = |name: &str| part[name].as_str().unwrap_or_default(); + FlairPart { + flair_part_type: value("e").to_string(), + value: match value("e") { + "text" => value("t").to_string(), + "emoji" => format_url(value("u")), + _ => String::new() + } + } + }).collect::>(), + None => Vec::new() + }, + "text" => match text_flair { + Some(text) => vec![FlairPart { + flair_part_type: "text".to_string(), + value: text.to_string(), + }], + None => Vec::new() + }, + _ => Vec::new() } - result } pub fn time(unix_time: i64) -> String {