flm_core/stdlib/features/cells/handler.rs
1//! The content handler that the `cells` feature contributes to the
2//! [`floats`](crate::stdlib::features::floats) feature: [`CellsContent`],
3//! named `cells`, which admits exactly one table of cells as a float's
4//! content.
5//!
6//! A float type declares the content handlers it admits
7//! ([`FloatType::handlers`](crate::stdlib::features::floats::FloatType::handlers)).
8//! The default `table` float type lists `cells` first, and lists the
9//! feature's scoped package
10//! [`ENVIRONMENT_PACKAGE`](super::ENVIRONMENT_PACKAGE) among the scoped
11//! packages of its body, so that `\begin{cells}` can be written there. The
12//! handler is contributed to the floats feature's service slot
13//! [`CONTENT_SLOT`](crate::stdlib::features::floats::CONTENT_SLOT) as a peer
14//! contribution, regardless of the switch
15//! [`CellsConfig::global`](super::CellsConfig::global): a registry without
16//! the `floats` feature drops the contribution with one Note, and still
17//! builds.
18
19use techy::core::node::NodeRef;
20
21use crate::lang::Flm;
22use crate::model::element::element_of;
23use crate::stdlib::features::floats::FloatContentHandler;
24
25use super::ENVIRONMENT;
26
27/// The name of the feature's content handler, `cells`: a float type lists
28/// this name among its content handlers to admit one table of cells.
29pub const HANDLER: &str = "cells";
30
31/// The content handler [`HANDLER`] (`cells`): it admits exactly one table of
32/// cells, a node of [`ENVIRONMENT`] (`flm.cells.cells`), and refuses any
33/// other content: two tables, text, a table beside a paragraph, or nothing
34/// at all. Its refusal reads "exactly one table of cells", which the float's
35/// rule reports as the end of the sentence "a ‘table’ must hold …". See the
36/// [module documentation](self).
37#[derive(Debug, Default, Clone, Copy)]
38pub struct CellsContent;
39
40impl FloatContentHandler for CellsContent {
41 fn name(&self) -> &str {
42 HANDLER
43 }
44
45 fn admit(&self, content: &[NodeRef<'_, Flm>]) -> Result<(), String> {
46 match content {
47 [one] if element_of(one).name.as_str() == ENVIRONMENT => Ok(()),
48 _ => Err(String::from("exactly one table of cells")),
49 }
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56 use crate::stdlib::features::floats::CELLS_HANDLER;
57
58 #[test]
59 fn the_handler_is_the_one_the_default_table_type_names() {
60 assert_eq!(CellsContent.name(), HANDLER);
61 assert_eq!(HANDLER, CELLS_HANDLER);
62 }
63
64 #[test]
65 fn no_content_is_refused() {
66 assert_eq!(CellsContent.admit(&[]), Err(String::from("exactly one table of cells")));
67 }
68}