Refactor flair parsing

This commit is contained in:
spikecodes 2021-01-13 18:19:40 -08:00
parent f95ef51017
commit dd027bff4b

View file

@ -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<Value>>, text_flair: Option<&str>) -> Vec<FlairPart> { pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec<Value>>, text_flair: Option<&str>) -> Vec<FlairPart> {
let mut result: Vec<FlairPart> = Vec::new(); match flair_type.as_str() {
if flair_type == "richtext" && !rich_flair.is_none() { "richtext" => match rich_flair {
for part in rich_flair.unwrap() { Some(rich) => rich.iter().map(|part| {
let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); let value = |name: &str| part[name].as_str().unwrap_or_default();
let value = if flair_part_type == "text" { FlairPart {
part["t"].as_str().unwrap_or_default().to_string() flair_part_type: value("e").to_string(),
} else if flair_part_type == "emoji" { value: match value("e") {
format_url(part["u"].as_str().unwrap_or_default()) "text" => value("t").to_string(),
} else { "emoji" => format_url(value("u")),
"".to_string() _ => String::new()
};
result.push(FlairPart { flair_part_type, value });
} }
} else if flair_type == "text" && !text_flair.is_none() { }
result.push(FlairPart { }).collect::<Vec<FlairPart>>(),
None => Vec::new()
},
"text" => match text_flair {
Some(text) => vec![FlairPart {
flair_part_type: "text".to_string(), flair_part_type: "text".to_string(),
value: text_flair.unwrap().to_string(), value: text.to_string(),
}); }],
None => Vec::new()
},
_ => Vec::new()
} }
result
} }
pub fn time(unix_time: i64) -> String { pub fn time(unix_time: i64) -> String {