//! TAG ICONS AS DATA — the starter catalogue (`icons.json`): each icon id of //! Notes' own drawn set with the common words, in several languages, that //! pick it for a tag. A tag is matched by its last word (`home/garden` → //! `garden`), without case or accents; a tag that matches none wears the //! fallback (`hash`). A person's own choice wins over all of it (the core //! keeps that). use serde::Deserialize; use super::find::fold_char; #[derive(Debug, Clone, Deserialize)] pub struct Icon { pub id: String, pub words: Vec, } #[derive(Debug, Clone, Deserialize)] pub struct Catalogue { pub fallback: String, pub icons: Vec, } /// The catalogue this build ships. pub fn catalogue() -> &'static Catalogue { static SHIPPED: std::sync::OnceLock = std::sync::OnceLock::new(); SHIPPED.get_or_init(|| serde_json::from_str(include_str!("icons.json")).expect("the tag icon catalogue reads")) } fn folded(word: &str) -> String { word.trim().chars().map(fold_char).collect() } /// Is `id` an icon a tag may wear (one of the catalogue's, or the fallback)? pub fn known(id: &str) -> bool { let shipped = catalogue(); id == shipped.fallback || shipped.icons.iter().any(|icon| icon.id == id) } /// The icon the catalogue picks for a tag: by its last word, then the /// fallback. pub fn for_tag(tag: &str) -> &'static str { let shipped = catalogue(); let last = folded(tag.rsplit('/').next().unwrap_or(tag)); shipped .icons .iter() .find(|icon| icon.words.iter().any(|word| folded(word) == last)) .map_or(shipped.fallback.as_str(), |icon| icon.id.as_str()) }