use std::sync::Arc;
use crate::core::registry::{BuildError, FeatureBuilder};
use crate::stdlib::features::numbering::{
COUNTERS_SLOT, CounterDecl, CounterStyleDecl, JoinDecl, PARENT_SETS_SLOT, ParentSetDecl,
PrefixWords, STYLES_SLOT,
};
use super::{HeadingDecl, HeadingsConfig, LEVELS_PARENT_SET};
pub const NUMBER_ROLE: &str = "flm.heading.number";
pub const WORD: &str = "§\u{202F}";
fn style_of(kind: &HeadingDecl) -> Option<CounterStyleDecl> {
let mut style = CounterStyleDecl::new(kind.style.clone()?);
style.number_format = kind.number_format.clone();
style.prefix_words = PrefixWords::new(WORD, WORD);
style.join = JoinDecl::compact();
Some(style)
}
fn counter_of(kind: &HeadingDecl) -> Option<CounterDecl> {
let decl = CounterDecl::new(kind.counter.clone()?, kind.style.clone()?)
.docs(format!("The `{}` headings of the document.", kind.name));
Some(match &kind.within {
Some(within) => decl.within(within.clone()),
None => decl,
})
}
fn levels_of(config: &HeadingsConfig) -> ParentSetDecl {
let mut kinds: Vec<&HeadingDecl> =
config.headings.iter().filter(|kind| kind.counter.is_some()).collect();
kinds.sort_by_key(|kind| kind.level);
ParentSetDecl::new(LEVELS_PARENT_SET, kinds.into_iter().filter_map(|kind| kind.counter.clone()))
}
pub(super) fn declare(b: &mut FeatureBuilder<'_>, config: &HeadingsConfig) -> Result<(), BuildError> {
for kind in &config.headings {
let (Some(style), Some(counter)) = (style_of(kind), counter_of(kind)) else { continue };
b.peer_slot::<CounterStyleDecl>(STYLES_SLOT, Arc::new(style))?;
b.peer_slot::<CounterDecl>(COUNTERS_SLOT, Arc::new(counter))?;
}
b.peer_slot::<ParentSetDecl>(PARENT_SETS_SLOT, Arc::new(levels_of(config)))?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_shipped_kind_has_a_counter_named_after_it() {
let config = HeadingsConfig::new();
for kind in &config.headings {
let counter = counter_of(kind).expect("a counter");
assert_eq!(counter.name.as_str(), format!("flm.headings.{}", kind.name));
assert_eq!(style_of(kind).expect("a style").name.as_str(), format!("flm.headings.style.{}", kind.name));
}
}
#[test]
fn each_shipped_kind_but_the_first_is_numbered_within_the_one_above_it_and_the_fourth_within_any_of_the_three_above_it() {
use crate::model::names::Name;
use crate::stdlib::features::numbering::{Parents, WithinJoinerSpec};
let config = HeadingsConfig::new();
assert!(config.headings[0].within.is_none());
for (index, kind) in config.headings.iter().enumerate().skip(1) {
let within = kind.within.as_ref().expect("a kind below the first");
if index == 3 {
continue;
}
assert_eq!(within.parents, Parents::One(config.headings[index - 1].counter.clone().expect("a counter")));
}
let fourth = config.headings[3].within.as_ref().expect("the fourth kind");
assert_eq!(
fourth.parents,
Parents::Any(vec![
Name::from("flm.headings.section"),
Name::from("flm.headings.subsection"),
Name::from("flm.headings.subsubsection"),
])
);
assert_eq!(fourth.joiner, WithinJoinerSpec::Bare);
}
#[test]
fn a_counter_is_declared_in_its_own_style() {
for kind in &HeadingsConfig::new().headings {
let decl = counter_of(kind).expect("a counter");
assert_eq!(Some(&decl.default_style), kind.style.as_ref());
}
}
#[test]
fn the_levels_run_outermost_first_over_the_kinds_with_a_counter() {
let mut kinds = HeadingsConfig::new().headings;
kinds.insert(0, HeadingDecl::new("aside", 2).no_counter());
kinds.push(HeadingDecl::new("chapter", 1));
let levels = levels_of(&HeadingsConfig::new().headings(kinds));
let names: Vec<&str> = levels.counters.iter().map(|name| name.as_str()).collect();
assert_eq!(
names,
[
"flm.headings.section",
"flm.headings.chapter",
"flm.headings.subsection",
"flm.headings.subsubsection",
"flm.headings.paragraph",
"flm.headings.subparagraph",
]
);
assert_eq!(levels.name.as_str(), LEVELS_PARENT_SET);
}
#[test]
fn a_kind_with_no_counter_declares_nothing() {
let kind = HeadingDecl::new("aside", 3).no_counter();
assert!(counter_of(&kind).is_none());
assert!(style_of(&kind).is_none());
}
}