Skip to main content

web/pages/commodity/
label.rs

1use std::collections::HashMap;
2
3use server::command::FinanceEntity;
4use sqlx::types::Uuid;
5
6fn tag_value(tags: &HashMap<String, FinanceEntity>, key: &str) -> Option<String> {
7    match tags.get(key) {
8        Some(FinanceEntity::Tag(tag)) => Some(tag.tag_value.clone()),
9        _ => None,
10    }
11}
12
13/// Symbol and name of a commodity; each falls back to the other tag, then to the id.
14#[must_use]
15pub fn commodity_labels(id: Uuid, tags: &HashMap<String, FinanceEntity>) -> (String, String) {
16    let symbol = tag_value(tags, "symbol");
17    let name = tag_value(tags, "name");
18    let fallback = || id.to_string();
19    (
20        symbol
21            .clone()
22            .or_else(|| name.clone())
23            .unwrap_or_else(fallback),
24        name.or(symbol).unwrap_or_else(fallback),
25    )
26}
27
28#[cfg(test)]
29mod tests;