#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub struct SourceSpan {
pub start_line: usize,
pub end_line: usize,
pub start_byte: usize,
pub end_byte: usize,
}
impl SourceSpan {
pub fn to_fields(&self) -> SpanFields {
SpanFields {
start_line: self.start_line,
end_line: self.end_line,
start_byte: self.start_byte,
end_byte: self.end_byte,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SpanFields {
pub start_line: usize,
pub end_line: usize,
pub start_byte: usize,
pub end_byte: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SpannedBlock {
pub block: Block,
pub span: SourceSpan,
pub id: String,
pub heading_path: Vec<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Block {
Heading { level: u8, text: Vec<Inline> },
Paragraph(Vec<Inline>),
CodeBlock { lang: Option<String>, code: String },
LiveComponent {
source: String,
lang: Option<String>,
},
Diagram { kind: DiagramKind, source: String },
List {
ordered: bool,
start: usize,
items: Vec<ListItem>,
},
Blockquote(Vec<SpannedBlock>),
Table {
headers: Vec<Vec<Inline>>,
rows: Vec<Vec<Vec<Inline>>>,
},
ThematicBreak,
Html(String),
Center(Vec<SpannedBlock>),
Div {
attrs: String,
children: Vec<SpannedBlock>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct ListItem {
pub inlines: Vec<Inline>,
pub children: Vec<Block>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagramKind {
Mermaid,
Math,
}
impl DiagramKind {
pub fn attr(self) -> &'static str {
match self {
DiagramKind::Mermaid => "mermaid",
DiagramKind::Math => "math",
}
}
pub fn source_lang(self) -> &'static str {
match self {
DiagramKind::Mermaid => "mermaid",
DiagramKind::Math => "latex",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Inline {
Text(String),
Strong(Vec<Inline>),
Emphasis(Vec<Inline>),
Code(String),
Link { text: Vec<Inline>, url: String },
Image { alt: String, url: String },
InlineHtml(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn span_fields_serializes_the_pinned_shape() {
let json = serde_json::to_string(
&SourceSpan {
start_line: 3,
end_line: 9,
start_byte: 42,
end_byte: 512,
}
.to_fields(),
)
.unwrap();
assert_eq!(
json,
r#"{"start_line":3,"end_line":9,"start_byte":42,"end_byte":512}"#
);
}
}