use super::AttributeMap;
use crate::tree::{Element, HeadingLevel};
use strum_macros::IntoStaticStr;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct Container<'t> {
#[serde(rename = "type")]
ctype: ContainerType,
elements: Vec<Element<'t>>,
}
impl<'t> Container<'t> {
#[inline]
pub fn new(ctype: ContainerType, elements: Vec<Element<'t>>) -> Self {
Container { ctype, elements }
}
#[inline]
pub fn ctype(&self) -> ContainerType {
self.ctype
}
#[inline]
pub fn elements(&self) -> &[Element<'t>] {
&self.elements
}
}
impl<'t> From<Container<'t>> for Vec<Element<'t>> {
#[inline]
fn from(container: Container<'t>) -> Vec<Element<'t>> {
let Container { elements, .. } = container;
elements
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct StyledContainer<'t> {
#[serde(rename = "type")]
ctype: StyledContainerType,
elements: Vec<Element<'t>>,
attributes: AttributeMap<'t>,
}
impl<'t> StyledContainer<'t> {
#[inline]
pub fn new(
ctype: StyledContainerType,
elements: Vec<Element<'t>>,
attributes: AttributeMap<'t>,
) -> Self {
StyledContainer {
ctype,
elements,
attributes,
}
}
#[inline]
pub fn ctype(&self) -> StyledContainerType {
self.ctype
}
#[inline]
pub fn elements(&self) -> &[Element<'t>] {
&self.elements
}
#[inline]
pub fn attributes(&self) -> &AttributeMap<'t> {
&self.attributes
}
}
impl<'t> From<StyledContainer<'t>> for Vec<Element<'t>> {
#[inline]
fn from(container: StyledContainer<'t>) -> Vec<Element<'t>> {
let StyledContainer { elements, .. } = container;
elements
}
}
#[derive(
Serialize, Deserialize, IntoStaticStr, Debug, Copy, Clone, Hash, PartialEq, Eq,
)]
#[serde(rename_all = "kebab-case")]
pub enum ContainerType {
Paragraph,
Strong,
Emphasis,
Underline,
Superscript,
Subscript,
Strikethrough,
Monospace,
Header(HeadingLevel),
}
impl ContainerType {
#[inline]
pub fn name(self) -> &'static str {
self.into()
}
#[inline]
pub fn html_tag(self) -> &'static str {
match self {
ContainerType::Paragraph => "p",
ContainerType::Strong => "strong",
ContainerType::Emphasis => "italics",
ContainerType::Underline => "u",
ContainerType::Superscript => "sup",
ContainerType::Subscript => "sub",
ContainerType::Strikethrough => "s",
ContainerType::Monospace => "tt",
ContainerType::Header(level) => level.html_tag(),
}
}
}
impl slog::Value for ContainerType {
fn serialize(
&self,
_: &slog::Record,
key: slog::Key,
serializer: &mut dyn slog::Serializer,
) -> slog::Result {
serializer.emit_str(key, self.name())
}
}
#[derive(
Serialize, Deserialize, IntoStaticStr, Debug, Copy, Clone, Hash, PartialEq, Eq,
)]
#[serde(rename_all = "kebab-case")]
pub enum StyledContainerType {
Span,
Div,
Mark,
Blockquote,
Insertion,
Deletion,
Hidden,
Invisible,
Size,
}
impl StyledContainerType {
#[inline]
pub fn name(self) -> &'static str {
self.into()
}
#[inline]
pub fn html_tag_and_class(self) -> (&'static str, Option<&'static str>) {
match self {
StyledContainerType::Span => ("span", None),
StyledContainerType::Div => ("div", None),
StyledContainerType::Mark => ("mark", None),
StyledContainerType::Blockquote => ("blockquote", None),
StyledContainerType::Insertion => ("ins", None),
StyledContainerType::Deletion => ("del", None),
StyledContainerType::Hidden => ("span", Some("hidden")),
StyledContainerType::Invisible => ("span", Some("invisible")),
StyledContainerType::Size => ("span", None),
}
}
}
impl slog::Value for StyledContainerType {
fn serialize(
&self,
_: &slog::Record,
key: slog::Key,
serializer: &mut dyn slog::Serializer,
) -> slog::Result {
serializer.emit_str(key, self.name())
}
}