Skip to main content

flm_core/stdlib/features/defterm/
config.rs

1//! The configuration of the [`defterm`](super) feature, [`DeftermConfig`]:
2//! the lead-in template that opens every term definition.
3
4use std::hash::{Hash, Hasher};
5
6/// The lead-in template of every term definition by default, `#{term}: `:
7/// the term, a colon and a space, run into the first paragraph of the term
8/// definition, as in `Entropy: A measure of disorder.`
9pub const DEFAULT_LEAD_IN: &str = "#{term}: ";
10
11/// The configuration of the [`Defterm`](super::Defterm) feature: the
12/// **lead-in**, the text that a term definition (a `defterm` environment)
13/// shows before the words of its first paragraph, usually the term being
14/// defined.
15///
16/// The lead-in is a template in the [template
17/// language](crate::guide::concepts_overview#the-template-language-shared-by-features)
18/// (described for authors under [the template
19/// language](crate::guide::language_syntax#the-template-language)), with one
20/// placeholder: `#{term}` is the term as the term definition writes it,
21/// markup included. The rest of the template is ordinary FLM markup, and
22/// markup may enclose the placeholder. `#{term}: ` is the default, and
23/// `\textbf{Definition} (\emph{#{term}}): ` a richer lead-in. A template
24/// without `#{term}` is allowed: every term definition then opens with the
25/// same words. A template may use `#{term}` once at most, because the term is
26/// shown only once, where the term definition is written.
27///
28/// `None` sets no lead-in: a term definition is then its body alone. A blank
29/// template is not a way to say so: it fails the registry build.
30///
31/// The value is what [`Defterm::new`](super::Defterm::new) takes, and a host
32/// passes it through
33/// [`StdlibConfig::defterm`](crate::stdlib::StdlibConfig::defterm). It is
34/// the feature's configuration, fixed when the registry is built (see
35/// [settings, options and
36/// configuration](crate::guide::concepts_overview#settings-options-and-configuration)):
37/// the template is compiled once then, and a template that does not compile
38/// fails the registry build with a message that quotes it. It enters the
39/// registry digest ([`digest_bytes`](DeftermConfig::digest_bytes)).
40///
41/// # Examples
42///
43/// ```
44/// use flm_core::stdlib::features::defterm::{DeftermConfig, DEFAULT_LEAD_IN};
45///
46/// assert_eq!(DeftermConfig::new().lead_in.as_deref(), Some(DEFAULT_LEAD_IN));
47///
48/// let richer = DeftermConfig::new()
49///     .lead_in(Some(r"\textbf{Definition} (\emph{#{term}}): ".to_string()));
50/// assert!(richer.lead_in.is_some());
51///
52/// // No lead-in: a definition is its body alone.
53/// assert!(DeftermConfig::new().lead_in(None).lead_in.is_none());
54/// ```
55#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize)]
56#[derive(schemars::JsonSchema)]
57#[serde(default, deny_unknown_fields)]
58pub struct DeftermConfig {
59    /// The lead-in template, or `None` for no lead-in. [`DEFAULT_LEAD_IN`]
60    /// by default.
61    pub lead_in: Option<String>,
62}
63
64impl Default for DeftermConfig {
65    fn default() -> DeftermConfig {
66        DeftermConfig { lead_in: Some(DEFAULT_LEAD_IN.to_string()) }
67    }
68}
69
70impl DeftermConfig {
71    /// The default configuration: the lead-in [`DEFAULT_LEAD_IN`].
72    pub fn new() -> DeftermConfig {
73        DeftermConfig::default()
74    }
75
76    /// Builder: sets the lead-in template, or `None` for no lead-in.
77    pub fn lead_in(mut self, lead_in: Option<String>) -> DeftermConfig {
78        self.lead_in = lead_in;
79        self
80    }
81
82    /// The configuration written out as bytes: what the feature feeds into
83    /// the registry digest, and what the `Hash` implementation hashes. `-`
84    /// for no lead-in, else `+`, the template's length and the template.
85    pub fn digest_bytes(&self) -> Vec<u8> {
86        let mut out = Vec::new();
87        match &self.lead_in {
88            None => out.push(b'-'),
89            Some(text) => {
90                out.push(b'+');
91                out.extend_from_slice(&(text.len() as u64).to_le_bytes());
92                out.extend_from_slice(text.as_bytes());
93            }
94        }
95        out
96    }
97}
98
99impl Hash for DeftermConfig {
100    fn hash<H: Hasher>(&self, state: &mut H) {
101        state.write(&self.digest_bytes());
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn the_digest_bytes_tell_every_lead_in_apart() {
111        let default = DeftermConfig::new().digest_bytes();
112        let richer =
113            DeftermConfig::new().lead_in(Some("\\textbf{#{term}}: ".to_string())).digest_bytes();
114        let none = DeftermConfig::new().lead_in(None).digest_bytes();
115        let empty = DeftermConfig::new().lead_in(Some(String::new())).digest_bytes();
116        assert_ne!(default, richer);
117        assert_ne!(default, none);
118        assert_ne!(none, empty);
119    }
120}