use std::{fmt, ops::Range};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum HeadingLevel {
H1,
H2,
H3,
H4,
H5,
H6,
}
impl HeadingLevel {
pub const fn get(self) -> u8 {
match self {
Self::H1 => 1,
Self::H2 => 2,
Self::H3 => 3,
Self::H4 => 4,
Self::H5 => 5,
Self::H6 => 6,
}
}
}
impl fmt::Display for HeadingLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.get().fmt(f)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct HeadingId(String);
impl HeadingId {
pub(crate) fn from_generated(value: String) -> Self {
Self(value)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for HeadingId {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for HeadingId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct LinkId(usize);
impl LinkId {
pub(crate) const fn from_index(index: usize) -> Self {
Self(index)
}
pub const fn index(self) -> usize {
self.0
}
}
impl fmt::Display for LinkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SourceSpan {
start: usize,
end: usize,
}
impl SourceSpan {
pub(crate) fn from_range(range: Range<usize>) -> Self {
debug_assert!(range.start <= range.end);
Self {
start: range.start,
end: range.end,
}
}
pub const fn start(self) -> usize {
self.start
}
pub const fn end(self) -> usize {
self.end
}
pub const fn len(self) -> usize {
self.end - self.start
}
pub const fn is_empty(self) -> bool {
self.start == self.end
}
pub const fn range(self) -> Range<usize> {
self.start..self.end
}
pub fn get(self, source: &str) -> Option<&str> {
source.get(self.range())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct LinkDestination(String);
impl LinkDestination {
pub(crate) fn from_parser(value: String) -> Self {
Self(value)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for LinkDestination {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for LinkDestination {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Document {
pub(crate) blocks: Vec<Block>,
pub(crate) headings: Vec<Heading>,
pub(crate) links: Vec<Link>,
}
impl Document {
pub fn blocks(&self) -> &[Block] {
&self.blocks
}
pub fn headings(&self) -> &[Heading] {
&self.headings
}
pub fn links(&self) -> &[Link] {
&self.links
}
pub fn link(&self, id: LinkId) -> Option<&Link> {
self.links.get(id.index())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Block {
pub(crate) kind: BlockKind,
pub(crate) span: SourceSpan,
}
impl Block {
pub fn kind(&self) -> &BlockKind {
&self.kind
}
pub const fn span(&self) -> SourceSpan {
self.span
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListKind {
Unordered,
Ordered {
start: u64,
},
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TaskState {
#[default]
Plain,
Unchecked,
Checked,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CodeBlockKind {
Indented,
Fenced(String),
}
impl CodeBlockKind {
pub fn info(&self) -> Option<&str> {
match self {
Self::Indented => None,
Self::Fenced(info) => Some(info),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BlockKind {
Paragraph(Vec<Inline>),
Heading {
level: HeadingLevel,
id: HeadingId,
content: Vec<Inline>,
},
Quote(Vec<Block>),
List {
kind: ListKind,
items: Vec<ListItem>,
},
Code {
kind: CodeBlockKind,
text: String,
},
Table(Table),
Rule,
Literal(String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListItem {
pub(crate) task: TaskState,
pub(crate) blocks: Vec<Block>,
}
impl ListItem {
pub const fn task(&self) -> TaskState {
self.task
}
pub fn blocks(&self) -> &[Block] {
&self.blocks
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Alignment {
#[default]
None,
Left,
Center,
Right,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableCell(Vec<Inline>);
impl TableCell {
pub(crate) fn from_inlines(inlines: Vec<Inline>) -> Self {
Self(inlines)
}
pub fn inlines(&self) -> &[Inline] {
&self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TableRow(Vec<TableCell>);
impl TableRow {
pub(crate) fn from_cells(cells: Vec<TableCell>) -> Self {
Self(cells)
}
pub fn cells(&self) -> &[TableCell] {
&self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Table {
alignments: Vec<Alignment>,
header: TableRow,
rows: Vec<TableRow>,
}
impl Table {
pub(crate) fn new(alignments: Vec<Alignment>, header: TableRow, rows: Vec<TableRow>) -> Self {
Self {
alignments,
header,
rows,
}
}
pub fn alignments(&self) -> &[Alignment] {
&self.alignments
}
pub const fn header(&self) -> &TableRow {
&self.header
}
pub fn rows(&self) -> &[TableRow] {
&self.rows
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Inline {
pub(crate) kind: InlineKind,
pub(crate) span: SourceSpan,
}
impl Inline {
pub fn kind(&self) -> &InlineKind {
&self.kind
}
pub const fn span(&self) -> SourceSpan {
self.span
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InlineKind {
Text(String),
Emphasis(Vec<Inline>),
Strong(Vec<Inline>),
Strikethrough(Vec<Inline>),
Code(String),
Link {
id: LinkId,
content: Vec<Inline>,
},
Image {
destination: LinkDestination,
title: Option<String>,
alt: Vec<Inline>,
},
SoftBreak,
HardBreak,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Heading {
pub(crate) id: HeadingId,
pub(crate) level: HeadingLevel,
pub(crate) text: String,
pub(crate) span: SourceSpan,
}
impl Heading {
pub const fn id(&self) -> &HeadingId {
&self.id
}
pub const fn level(&self) -> HeadingLevel {
self.level
}
pub fn text(&self) -> &str {
&self.text
}
pub const fn span(&self) -> SourceSpan {
self.span
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Link {
pub(crate) id: LinkId,
pub(crate) destination: LinkDestination,
pub(crate) title: Option<String>,
pub(crate) text: String,
pub(crate) span: SourceSpan,
}
impl Link {
pub const fn id(&self) -> LinkId {
self.id
}
pub const fn destination(&self) -> &LinkDestination {
&self.destination
}
pub fn title(&self) -> Option<&str> {
self.title.as_deref()
}
pub fn text(&self) -> &str {
&self.text
}
pub const fn span(&self) -> SourceSpan {
self.span
}
}
pub fn plain_text(inlines: &[Inline]) -> String {
let mut out = String::new();
for inline in inlines {
match inline.kind() {
InlineKind::Text(s) | InlineKind::Code(s) => out.push_str(s),
InlineKind::Emphasis(c)
| InlineKind::Strong(c)
| InlineKind::Strikethrough(c)
| InlineKind::Link { content: c, .. }
| InlineKind::Image { alt: c, .. } => out.push_str(&plain_text(c)),
InlineKind::SoftBreak | InlineKind::HardBreak => out.push(' '),
}
}
out
}