use serde::{
Serialize,
ser::{SerializeMap, Serializer},
};
use super::location::Location;
use super::title::Title;
pub const UNNUMBERED_SECTION_STYLES: &[&str] = &[
"preface",
"abstract",
"dedication",
"colophon",
"bibliography",
"glossary",
"index",
"appendix",
];
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[non_exhaustive]
pub struct Anchor {
pub id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub xreflabel: Option<String>,
pub location: Location,
}
impl Anchor {
#[must_use]
pub fn new(id: String, location: Location) -> Self {
Self {
id,
xreflabel: None,
location,
}
}
#[must_use]
pub fn with_xreflabel(mut self, xreflabel: Option<String>) -> Self {
self.xreflabel = xreflabel;
self
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct TocEntry {
pub id: String,
pub title: Title,
pub level: u8,
pub xreflabel: Option<String>,
pub numbered: bool,
pub style: Option<String>,
}
impl Serialize for TocEntry {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_map(None)?;
state.serialize_entry("id", &self.id)?;
state.serialize_entry("title", &self.title)?;
state.serialize_entry("level", &self.level)?;
if self.xreflabel.is_some() {
state.serialize_entry("xreflabel", &self.xreflabel)?;
}
if self.style.is_some() {
state.serialize_entry("style", &self.style)?;
}
state.end()
}
}