Lines
100 %
Functions
75 %
Branches
use std::collections::HashMap;
use server::command::FinanceEntity;
use sqlx::types::Uuid;
fn tag_value(tags: &HashMap<String, FinanceEntity>, key: &str) -> Option<String> {
match tags.get(key) {
Some(FinanceEntity::Tag(tag)) => Some(tag.tag_value.clone()),
_ => None,
}
/// Symbol and name of a commodity; each falls back to the other tag, then to the id.
#[must_use]
pub fn commodity_labels(id: Uuid, tags: &HashMap<String, FinanceEntity>) -> (String, String) {
let symbol = tag_value(tags, "symbol");
let name = tag_value(tags, "name");
let fallback = || id.to_string();
(
symbol
.clone()
.or_else(|| name.clone())
.unwrap_or_else(fallback),
name.or(symbol).unwrap_or_else(fallback),
)
#[cfg(test)]
mod tests;