use std::sync::Arc;
use crate::core::registry::{BuildError, FeatureBuilder};
use crate::model::names::Name;
use crate::stdlib::features::numbering::{
COUNTERS_SLOT, CounterDecl, CounterStyleDecl, PluralWords, PrefixWords, STYLES_SLOT,
};
use super::config::{TheoremKind, TheoremsConfig};
pub const COUNTER_PREFIX: &str = "flm.theorems.counter.";
pub const SHARED_COUNTER: &str = "flm.theorems.theorem";
pub const STYLE_PREFIX: &str = "flm.theorems.style.";
pub const DEFAULT_STYLE: &str = "flm.theorems.number";
const TIE: &str = "~";
pub fn counter_name(config: &TheoremsConfig, name: &str) -> Option<Name> {
let (_, class) = config.kind_and_class(name)?;
if !class.numbered {
return None;
}
Some(if class.shared_counter {
Name::from(SHARED_COUNTER)
} else {
Name::from(format!("{COUNTER_PREFIX}{name}").as_str())
})
}
pub fn style_name(name: &str) -> Name {
Name::from(format!("{STYLE_PREFIX}{name}").as_str())
}
fn tied(words: &PrefixWords) -> PrefixWords {
let tie = |plural: &PluralWords| {
PluralWords::new(format!("{}{TIE}", plural.one), format!("{}{TIE}", plural.other))
};
PrefixWords {
base: tie(&words.base),
variants: words.variants.iter().map(|(name, plural)| (name.clone(), tie(plural))).collect(),
}
}
fn style_of(config: &TheoremsConfig, name: &str, kind: &TheoremKind) -> CounterStyleDecl {
let mut style = CounterStyleDecl::new(style_name(name));
style.number_format = config.number_format.clone();
style.prefix_words = tied(&kind.words);
style
}
pub(super) fn declare(b: &mut FeatureBuilder<'_>, config: &TheoremsConfig) -> Result<(), BuildError> {
let mut default = CounterStyleDecl::new(DEFAULT_STYLE);
default.number_format = config.number_format.clone();
b.peer_slot::<CounterStyleDecl>(STYLES_SLOT, Arc::new(default))?;
b.peer_slot::<CounterDecl>(
COUNTERS_SLOT,
Arc::new(
CounterDecl::new(SHARED_COUNTER, DEFAULT_STYLE)
.docs("The theorems of every kind that counts in the shared sequence of numbers."),
),
)?;
for (name, kind) in &config.kinds {
let Some(counter) = counter_name(config, name.as_str()) else { continue };
b.peer_slot::<CounterStyleDecl>(STYLES_SLOT, Arc::new(style_of(config, name.as_str(), kind)))?;
let shared = config.classes.get(&kind.class).is_some_and(|class| class.shared_counter);
if !shared {
b.peer_slot::<CounterDecl>(
COUNTERS_SLOT,
Arc::new(
CounterDecl::new(counter, DEFAULT_STYLE)
.docs(format!("The `{name}` environments of the document.")),
),
)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::stdlib::features::theorems::{TheoremClass, TheoremKind};
#[test]
fn a_kind_counts_in_the_counter_its_class_says() {
let config = TheoremsConfig::new()
.class("exerciselike", TheoremClass::numbered().shared_counter(false))
.kind("exercise", TheoremKind::from_word("exerciselike", "exercise"));
assert_eq!(counter_name(&config, "lemma").as_deref(), Some(SHARED_COUNTER));
assert_eq!(
counter_name(&config, "exercise").map(|name| name.to_string()).as_deref(),
Some("flm.theorems.counter.exercise")
);
let own = config
.class("own", TheoremClass::numbered().shared_counter(false))
.kind("theorem", TheoremKind::from_word("own", "theorem"));
assert_eq!(
counter_name(&own, "theorem").map(|name| name.to_string()).as_deref(),
Some("flm.theorems.counter.theorem")
);
assert_eq!(counter_name(&own, "lemma").as_deref(), Some(SHARED_COUNTER));
assert_eq!(counter_name(&own, "proof"), None);
assert_eq!(counter_name(&own, "nosuchkind"), None);
}
#[test]
fn a_style_ties_every_word_to_the_number() {
let config = TheoremsConfig::new();
let (kind, _) = config.kind_and_class("lemma").expect("shipped");
let style = style_of(&config, "lemma", kind);
assert_eq!(style.name.as_str(), "flm.theorems.style.lemma");
assert_eq!(style.prefix_words.base, PluralWords::new("Lemma~", "Lemmas~"));
assert_eq!(style.prefix_words.variants["abbreviated"], PluralWords::new("Lem.~", "Lems.~"));
assert_eq!(style.delimiters.open, "");
assert_eq!(style.delimiters.close, "");
}
}