Skip to content

Commit

Permalink
Fix serde of table align
Browse files Browse the repository at this point in the history
Closes GH-142.
  • Loading branch information
h7kanna authored Sep 23, 2024
1 parent 4ad5342 commit d0893f4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
88 changes: 82 additions & 6 deletions src/mdast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ pub enum ReferenceKind {
///
/// Used to align the contents of table cells within a table.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
pub enum AlignKind {
/// Left alignment.
///
Expand Down Expand Up @@ -78,10 +73,91 @@ pub enum AlignKind {
/// > | | --- |
/// ^^^
/// ```
// To do: this should serialize in serde as `null`.
None,
}

/// Implement serde according to <https://github.com/syntax-tree/mdast?tab=readme-ov-file#aligntype>
#[cfg(feature = "serde")]
impl serde::ser::Serialize for AlignKind {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
match self {
AlignKind::Left => serializer.serialize_unit_variant("AlignKind", 0, "left"),
AlignKind::Right => serializer.serialize_unit_variant("AlignKind", 1, "right"),
AlignKind::Center => serializer.serialize_unit_variant("AlignKind", 2, "center"),
AlignKind::None => serializer.serialize_none(),
}
}
}

#[cfg(feature = "serde")]
struct AlignKindVisitor;

#[cfg(feature = "serde")]
impl<'de> serde::de::Visitor<'de> for AlignKindVisitor {
type Value = AlignKind;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("'left', 'right', 'center' or null")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
"left" => Ok(AlignKind::Left),
"right" => Ok(AlignKind::Right),
"center" => Ok(AlignKind::Center),
&_ => Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Str(v),
&self,
)),
}
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
b"left" => Ok(AlignKind::Left),
b"right" => Ok(AlignKind::Right),
b"center" => Ok(AlignKind::Center),
&_ => Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Bytes(v),
&self,
)),
}
}

fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(AlignKind::None)
}

fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(AlignKind::None)
}
}

#[cfg(feature = "serde")]
impl<'de> serde::de::Deserialize<'de> for AlignKind {
fn deserialize<D>(deserializer: D) -> Result<AlignKind, D::Error>
where
D: serde::de::Deserializer<'de>,
{
deserializer.deserialize_any(AlignKindVisitor)
}
}

/// Nodes.
#[derive(Clone, Eq, PartialEq)]
#[cfg_attr(
Expand Down
12 changes: 8 additions & 4 deletions tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,26 +606,30 @@ fn serde_heading() -> Result<(), Error> {
fn serde_table() -> Result<(), Error> {
// To do: `"none"` should serialize in serde as `null`.
assert_serde(
"| a | b |\n| - | -: |\n| 1 | 2 |",
"| a | b | c | d |\n| - | :- | -: | :-: |\n| 1 | 2 | 3 | 4 |",
r#"{
"type": "root",
"children": [
{
"type": "table",
"align": ["none", "right"],
"align": [null, "left", "right", "center"],
"children": [
{
"type": "tableRow",
"children": [
{"type": "tableCell", "children": [{"type": "text", "value": "a"}]},
{"type": "tableCell", "children": [{"type": "text", "value": "b"}]}
{"type": "tableCell", "children": [{"type": "text", "value": "b"}]},
{"type": "tableCell", "children": [{"type": "text", "value": "c"}]},
{"type": "tableCell", "children": [{"type": "text", "value": "d"}]}
]
},
{
"type": "tableRow",
"children": [
{"type": "tableCell","children": [{"type": "text", "value": "1"}]},
{"type": "tableCell","children": [{"type": "text", "value": "2"}]}
{"type": "tableCell","children": [{"type": "text", "value": "2"}]},
{"type": "tableCell","children": [{"type": "text", "value": "3"}]},
{"type": "tableCell","children": [{"type": "text", "value": "4"}]}
]
}
]
Expand Down

0 comments on commit d0893f4

Please sign in to comment.