use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Document {
pub blocks: Vec<Block>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub footnotes: Vec<FootnoteDefinition>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Block {
Heading { level: u8, content: Vec<Inline> },
Paragraph { content: Vec<Inline> },
CodeBlock {
language: Option<String>,
code: String,
},
BlockQuote { blocks: Vec<Block> },
Alert {
alert_type: AlertType,
title: Option<String>,
blocks: Vec<Block>,
},
List(List),
Table(Table),
ThematicBreak,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlertType {
Note,
Tip,
Important,
Warning,
Caution,
}
impl AlertType {
pub fn default_title(self) -> &'static str {
match self {
AlertType::Note => "NOTE",
AlertType::Tip => "TIP",
AlertType::Important => "IMPORTANT",
AlertType::Warning => "WARNING",
AlertType::Caution => "CAUTION",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct List {
pub ordered: bool,
pub start: u32,
pub items: Vec<ListItem>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ListItem {
pub blocks: Vec<Block>,
pub task: Option<bool>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Align {
None,
Left,
Center,
Right,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Table {
pub align: Vec<Align>,
pub head: Vec<Vec<Inline>>,
pub rows: Vec<Vec<Vec<Inline>>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FootnoteDefinition {
pub label: String,
pub blocks: Vec<Block>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "data", rename_all = "snake_case")]
pub enum Inline {
Text(String),
Strong(Vec<Inline>),
Emphasis(Vec<Inline>),
Strikethrough(Vec<Inline>),
Subscript(Vec<Inline>),
Code(String),
Link { href: String, content: Vec<Inline> },
Math { latex: String, display: bool },
Image {
src: String,
alt: String,
title: String,
},
FootnoteReference { label: String },
SoftBreak,
HardBreak,
}
impl Inline {
pub fn text(s: impl Into<String>) -> Self {
Inline::Text(s.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn document_default_is_empty() {
assert_eq!(Document::default().blocks.len(), 0);
}
#[test]
fn shapes_round_trip_through_json() {
let doc = Document {
blocks: vec![
Block::Heading {
level: 2,
content: vec![Inline::text("Title")],
},
Block::Paragraph {
content: vec![
Inline::text("see "),
Inline::Link {
href: "#title".into(),
content: vec![Inline::Emphasis(vec![Inline::text("here")])],
},
],
},
Block::List(List {
ordered: true,
start: 3,
items: vec![ListItem {
blocks: vec![Block::Paragraph {
content: vec![Inline::text("one")],
}],
task: None,
}],
}),
],
footnotes: vec![],
};
let json = serde_json::to_string(&doc).expect("serialize");
let back: Document = serde_json::from_str(&json).expect("deserialize");
assert_eq!(doc, back, "IR must survive a JSON round trip unchanged");
}
}