Skip to main content

base_markdown/
align_kind.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4#[cfg(feature = "serde")]
5use core::fmt;
6
7/// Alignment of phrasing content.
8///
9/// Used to align the contents of table cells within a table.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum AlignKind {
12    /// Left alignment.
13    ///
14    /// See the `left` value of the `text-align` CSS property.
15    ///
16    /// ```markdown
17    ///   | | aaa |
18    /// > | | :-- |
19    ///       ^^^
20    /// ```
21    Left,
22    /// Right alignment.
23    ///
24    /// See the `right` value of the `text-align` CSS property.
25    ///
26    /// ```markdown
27    ///   | | aaa |
28    /// > | | --: |
29    ///       ^^^
30    /// ```
31    Right,
32    /// Center alignment.
33    ///
34    /// See the `center` value of the `text-align` CSS property.
35    ///
36    /// ```markdown
37    ///   | | aaa |
38    /// > | | :-: |
39    ///       ^^^
40    /// ```
41    Center,
42    /// No alignment.
43    ///
44    /// Phrasing content is aligned as defined by the host environment.
45    ///
46    /// ```markdown
47    ///   | | aaa |
48    /// > | | --- |
49    ///       ^^^
50    /// ```
51    None,
52}
53
54/// Implement serde according to <https://github.com/syntax-tree/mdast#aligntype>
55#[cfg(feature = "serde")]
56impl serde::ser::Serialize for AlignKind {
57    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
58    where
59        S: serde::ser::Serializer,
60    {
61        match self {
62            AlignKind::Left => serializer.serialize_unit_variant("AlignKind", 0, "left"),
63            AlignKind::Right => serializer.serialize_unit_variant("AlignKind", 1, "right"),
64            AlignKind::Center => serializer.serialize_unit_variant("AlignKind", 2, "center"),
65            AlignKind::None => serializer.serialize_none(),
66        }
67    }
68}
69
70#[cfg(feature = "serde")]
71struct AlignKindVisitor;
72
73#[cfg(feature = "serde")]
74impl serde::de::Visitor<'_> for AlignKindVisitor {
75    type Value = AlignKind;
76
77    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
78        formatter.write_str("'left', 'right', 'center' or null")
79    }
80
81    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
82    where
83        E: serde::de::Error,
84    {
85        match v {
86            "left" => Ok(AlignKind::Left),
87            "right" => Ok(AlignKind::Right),
88            "center" => Ok(AlignKind::Center),
89            &_ => Err(serde::de::Error::invalid_type(
90                serde::de::Unexpected::Str(v),
91                &self,
92            )),
93        }
94    }
95
96    fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
97    where
98        E: serde::de::Error,
99    {
100        match v {
101            b"left" => Ok(AlignKind::Left),
102            b"right" => Ok(AlignKind::Right),
103            b"center" => Ok(AlignKind::Center),
104            &_ => Err(serde::de::Error::invalid_type(
105                serde::de::Unexpected::Bytes(v),
106                &self,
107            )),
108        }
109    }
110
111    fn visit_none<E>(self) -> Result<Self::Value, E>
112    where
113        E: serde::de::Error,
114    {
115        Ok(AlignKind::None)
116    }
117
118    fn visit_unit<E>(self) -> Result<Self::Value, E>
119    where
120        E: serde::de::Error,
121    {
122        Ok(AlignKind::None)
123    }
124}
125
126#[cfg(feature = "serde")]
127impl<'de> serde::de::Deserialize<'de> for AlignKind {
128    fn deserialize<D>(deserializer: D) -> Result<AlignKind, D::Error>
129    where
130        D: serde::de::Deserializer<'de>,
131    {
132        deserializer.deserialize_any(AlignKindVisitor)
133    }
134}