use super::facts::FactId;
use super::ids::AnchorId;
use super::xref::{SourceLoc, Target};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum Block {
Heading {
level: u8,
text: String,
anchor: AnchorId,
},
Paragraph {
content: Vec<Inline>,
},
Markdown {
markdown: MarkdownText,
},
Code {
lang: String,
code: String,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
},
List {
ordered: bool,
items: Vec<Vec<Inline>>,
},
Table {
columns: Vec<String>,
rows: Vec<Vec<Vec<Inline>>>,
},
Callout {
kind: CalloutKind,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
body: Vec<Block>,
},
Diagram {
kind: DiagramKind,
source: String,
links: Vec<(String, Target)>,
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
},
Cards {
cards: Vec<Card>,
},
Stats {
stats: Vec<Stat>,
},
Symbol {
symbol: Box<SymbolBlock>,
},
Narrative {
slot: String,
#[serde(skip_serializing_if = "Option::is_none")]
text: Option<MarkdownText>,
fallback: Vec<Block>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum Inline {
Text {
text: String,
},
Code {
code: String,
},
Strong {
content: Vec<Inline>,
},
Emph {
content: Vec<Inline>,
},
Link {
to: Target,
content: Vec<Inline>,
},
Fact {
fact: FactId,
},
}
impl Inline {
pub fn text(s: impl Into<String>) -> Self {
Inline::Text { text: s.into() }
}
pub fn code(s: impl Into<String>) -> Self {
Inline::Code { code: s.into() }
}
pub fn strong(s: impl Into<String>) -> Self {
Inline::Strong {
content: vec![Inline::text(s)],
}
}
pub fn link(to: Target, label: impl Into<String>) -> Self {
Inline::Link {
to,
content: vec![Inline::text(label)],
}
}
pub fn code_link(to: Target, code: impl Into<String>) -> Self {
Inline::Link {
to,
content: vec![Inline::code(code)],
}
}
pub fn fact(id: FactId) -> Self {
Inline::Fact { fact: id }
}
}
impl Block {
pub fn heading(level: u8, text: impl Into<String>) -> Self {
let text = text.into();
Block::Heading {
level,
anchor: super::ids::anchor_for(&text),
text,
}
}
pub fn para(content: Vec<Inline>) -> Self {
Block::Paragraph { content }
}
pub fn text(s: impl Into<String>) -> Self {
Block::Paragraph {
content: vec![Inline::text(s)],
}
}
pub fn note(body: impl Into<String>) -> Self {
Block::Callout {
kind: CalloutKind::Note,
title: None,
body: vec![Block::text(body)],
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MarkdownText {
pub source: String,
pub origin: MarkdownOrigin,
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<SourceLoc>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum MarkdownOrigin {
Doc,
Comment,
Commit,
Llm,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum CalloutKind {
Note,
Tip,
Caution,
Danger,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DiagramKind {
Mermaid,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Card {
pub title: String,
pub to: Target,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Stat {
pub label: String,
pub fact: FactId,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SymbolBlock {
pub id: super::ids::SymbolId,
pub anchor: AnchorId,
pub kind: String,
pub name: String,
pub lang: String,
pub signature: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc: Option<MarkdownText>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub params: Vec<ParamRow>,
#[serde(skip_serializing_if = "Option::is_none")]
pub returns: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecated: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub badges: Vec<String>,
pub source: SourceLoc,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ParamRow {
pub name: String,
pub ty: String,
}