ddoc/config/
toc.rs

1use {
2    crate::*,
3    std::{
4        fmt,
5        str::FromStr,
6    },
7};
8
9/// The settings for the insertion of a table of content in a web page.
10#[derive(Debug, Clone, Copy, Default)]
11pub struct Toc {
12    pub activate_visible_item: bool,
13}
14
15impl FromStr for Toc {
16    type Err = &'static str;
17    fn from_str(s: &str) -> Result<Self, Self::Err> {
18        if s != "toc" {
19            return Err("Toc must be 'toc'");
20        }
21        Ok(Self {
22            activate_visible_item: true,
23        })
24    }
25}
26
27impl From<Attributes> for Toc {
28    fn from(map: Attributes) -> Self {
29        let mut toc_insert = Toc::default();
30        if let Some(v) = map
31            .get("activate-visible-item")
32            .or_else(|| map.get("activate_visible_item"))
33        {
34            if let Some(b) = v.as_bool() {
35                toc_insert.activate_visible_item = b;
36            }
37        }
38        toc_insert
39    }
40}
41
42impl fmt::Display for Toc {
43    fn fmt(
44        &self,
45        f: &mut fmt::Formatter<'_>,
46    ) -> fmt::Result {
47        write!(f, "toc")
48    }
49}
50impl serde::Serialize for Toc {
51    fn serialize<S: serde::Serializer>(
52        &self,
53        serializer: S,
54    ) -> Result<S::Ok, S::Error> {
55        serializer.serialize_str(&self.to_string())
56    }
57}
58impl<'de> serde::Deserialize<'de> for Toc {
59    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
60        let s = String::deserialize(deserializer)?;
61        s.parse().map_err(serde::de::Error::custom)
62    }
63}