diff --git a/crates/synapse-core/src/tags.rs b/crates/synapse-core/src/tags.rs index 3df79b6..ebf4aa9 100644 --- a/crates/synapse-core/src/tags.rs +++ b/crates/synapse-core/src/tags.rs @@ -17,7 +17,7 @@ use std::collections::{HashMap, HashSet}; pub const MAX_TAG_CHARS: usize = 48; /// A tag with the number of pages filed under it (explicitly or through a type). -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct TagInfo { pub name: String, pub count: u32, @@ -25,6 +25,12 @@ pub struct TagInfo { pub suggested: bool, /// Emoji of the type ([`crate::TypeDef::icon`]; types only). pub icon: Option, + /// Pages listing it among their explicit tags (`tag_catalog` only). + pub explicit: u32, + /// Pages having it as their type (`tag_catalog` only). + pub typed: u32, + /// Pages carrying it because their hub has this title (`tag_catalog` only). + pub from_hub: u32, } /// Cleans a tag name: leading `#` dropped, whitespace collapsed. `None` when nothing is left. @@ -137,7 +143,7 @@ impl Graph { /// Every tag in use (listed, implied by a type or by the page's hub): spelling of its first /// page, with its page count. Most used first, then by name. pub fn tag_catalog(&self) -> Result> { - let mut used: HashMap = HashMap::new(); + let mut used: HashMap = HashMap::new(); let mut sources = schema::tag_sources(self.conn(), None)?; sources.sort_by(|a, b| a.id.cmp(&b.id)); for src in sources { @@ -145,19 +151,19 @@ impl Graph { for t in src.names() { let key = title_key(t); if seen.insert(key.clone()) { - used.entry(key).or_insert((t.clone(), 0)).1 += 1; + let info = used.entry(key.clone()).or_insert_with(|| TagInfo { + name: t.clone(), + ..Default::default() + }); + info.count += 1; + let is = |n: &String| title_key(n) == key; + info.explicit += u32::from(src.tags.iter().any(is)); + info.typed += u32::from(src.types.iter().take(1).any(is)); + info.from_hub += u32::from(src.hub.iter().any(is)); } } } - let mut out: Vec = used - .into_values() - .map(|(name, count)| TagInfo { - name, - count, - suggested: false, - icon: None, - }) - .collect(); + let mut out: Vec = used.into_values().collect(); out.sort_by(|a, b| { b.count .cmp(&a.count) diff --git a/crates/synapse-core/src/types.rs b/crates/synapse-core/src/types.rs index f35f83c..d54c5c4 100644 --- a/crates/synapse-core/src/types.rs +++ b/crates/synapse-core/src/types.rs @@ -116,6 +116,7 @@ impl Graph { count: *count, suggested: suggested(k), icon: live_icon(k), + ..Default::default() }) .collect(); out.sort_by(|a, b| { @@ -135,6 +136,7 @@ impl Graph { count: 0, suggested: false, icon: d.icon.clone(), + ..Default::default() })); for s in SUGGESTED_TYPES { let k = title_key(s); @@ -145,6 +147,7 @@ impl Graph { count: 0, suggested: true, icon: def.and_then(|d| d.icon.clone()), + ..Default::default() }); } } diff --git a/crates/synapse-core/tests/structure.rs b/crates/synapse-core/tests/structure.rs index fdc2e38..2d3b2b0 100644 --- a/crates/synapse-core/tests/structure.rs +++ b/crates/synapse-core/tests/structure.rs @@ -1089,6 +1089,30 @@ fn renaming_a_type_keeps_its_icon_and_removing_drops_it() { assert_eq!(type_row(&g, "lugar"), Some(("lugar".into(), 1, None))); } +#[test] +fn tag_catalog_says_where_each_tag_comes_from() { + let (_d, g) = graph(); + let h = hub(&g, "Anime"); + let a = page(&g, "A", &["x"]); + let b = page(&g, "B", &["x"]); + g.add_to_hub(a, h).unwrap(); + g.add_page_tag(a, "idea").unwrap(); + g.set_page_type(b, Some("Idea")).unwrap(); + g.add_page_tag(b, "idea").unwrap(); + let cat = g.tag_catalog().unwrap(); + let get = |n: &str| cat.iter().find(|t| t.name.eq_ignore_ascii_case(n)).unwrap(); + let idea = get("idea"); + assert_eq!( + (idea.count, idea.explicit, idea.typed, idea.from_hub), + (2, 2, 1, 0) + ); + let anime = get("anime"); + assert_eq!( + (anime.count, anime.explicit, anime.typed, anime.from_hub), + (1, 0, 0, 1) + ); +} + #[test] fn type_definitions_sync_last_writer_wins() { let (_a, a) = graph();