Skip to main content

marki_parse/
section.rs

1/// A range of inline elements stored contiguously in the inline pool.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct InlineSpan {
4    pub start: u32,
5    pub len: u32,
6}
7
8impl InlineSpan {
9    pub const EMPTY: Self = Self { start: 0, len: 0 };
10
11    #[inline]
12    #[must_use]
13    pub const fn new(start: u32, len: u32) -> Self {
14        Self { start, len }
15    }
16
17    #[inline]
18    #[must_use]
19    pub const fn is_empty(self) -> bool {
20        self.len == 0
21    }
22}
23
24/// A range of `InlineSpan` elements stored contiguously in the span pool.
25/// Used by list sections to avoid per-list `Vec<InlineSpan>` heap allocations.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub struct SpanSlice {
28    pub start: u32,
29    pub len: u32,
30}
31
32impl SpanSlice {
33    pub const EMPTY: Self = Self { start: 0, len: 0 };
34
35    #[inline]
36    #[must_use]
37    pub const fn new(start: u32, len: u32) -> Self {
38        Self { start, len }
39    }
40
41    #[inline]
42    #[must_use]
43    pub const fn is_empty(self) -> bool {
44        self.len == 0
45    }
46}
47
48/// The delimiter used after the number in an ordered list item (`1.` vs `1)`).
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50#[repr(u8)]
51pub enum OrderedListDelimiter {
52    Dot = b'.',
53    Paren = b')',
54}
55
56impl OrderedListDelimiter {
57    #[must_use]
58    pub const fn byte(self) -> u8 {
59        self as u8
60    }
61
62    #[must_use]
63    pub const fn from_byte(b: u8) -> Option<Self> {
64        match b {
65            b'.' => Some(Self::Dot),
66            b')' => Some(Self::Paren),
67            _ => None,
68        }
69    }
70}
71
72impl PartialEq<u8> for OrderedListDelimiter {
73    fn eq(&self, other: &u8) -> bool {
74        self.byte() == *other
75    }
76}
77
78impl PartialEq<OrderedListDelimiter> for u8 {
79    fn eq(&self, other: &OrderedListDelimiter) -> bool {
80        *self == other.byte()
81    }
82}
83
84/// A block-level element of a Markdown document.
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum Section<'src> {
87    Heading {
88        level: u8,
89        content: InlineSpan,
90    },
91    Paragraph {
92        content: InlineSpan,
93    },
94    CodeBlock {
95        language: Option<&'src str>,
96        code: &'src str,
97    },
98    UnorderedList {
99        items: SpanSlice,
100    },
101    OrderedList {
102        start: u32,
103        delimiter: OrderedListDelimiter,
104        items: SpanSlice,
105    },
106    Blockquote {
107        content: InlineSpan,
108    },
109    HorizontalRule,
110}