rustc_errors/markdown/
mod.rs

1//! A simple markdown parser that can write formatted text to the terminal
2//!
3//! Entrypoint is `MdStream::parse_str(...)`
4
5use std::io;
6
7mod parse;
8mod term;
9
10/// An AST representation of a Markdown document
11#[derive(Clone, Debug, Default, PartialEq)]
12pub struct MdStream<'a>(Vec<MdTree<'a>>);
13
14impl<'a> MdStream<'a> {
15    /// Parse a markdown string to a tokenstream
16    #[must_use]
17    pub fn parse_str(s: &str) -> MdStream<'_> {
18        parse::entrypoint(s)
19    }
20
21    /// Write formatted output to an anstream buffer
22    pub fn write_anstream_buf(&self, buf: &mut Vec<u8>) -> io::Result<()> {
23        term::entrypoint(self, buf)
24    }
25}
26
27/// Create an anstream buffer with the `Always` color choice
28pub fn create_stdout_bufwtr() -> anstream::Stdout {
29    anstream::Stdout::always(std::io::stdout())
30}
31
32/// A single tokentree within a Markdown document
33#[derive(Clone, Debug, PartialEq)]
34pub enum MdTree<'a> {
35    /// Leaf types
36    Comment(&'a str),
37    CodeBlock {
38        txt: &'a str,
39        lang: Option<&'a str>,
40    },
41    CodeInline(&'a str),
42    Strong(&'a str),
43    Emphasis(&'a str),
44    Strikethrough(&'a str),
45    PlainText(&'a str),
46    /// [Foo](www.foo.com) or simple anchor <www.foo.com>
47    Link {
48        disp: &'a str,
49        link: &'a str,
50    },
51    /// `[Foo link][ref]`
52    RefLink {
53        disp: &'a str,
54        id: Option<&'a str>,
55    },
56    /// [ref]: www.foo.com
57    LinkDef {
58        id: &'a str,
59        link: &'a str,
60    },
61    /// Break bewtween two paragraphs (double `\n`), not directly parsed but
62    /// added later
63    ParagraphBreak,
64    /// Break bewtween two lines (single `\n`)
65    LineBreak,
66    HorizontalRule,
67    Heading(u8, MdStream<'a>),
68    OrderedListItem(u16, MdStream<'a>),
69    UnorderedListItem(MdStream<'a>),
70}
71
72impl<'a> From<Vec<MdTree<'a>>> for MdStream<'a> {
73    fn from(value: Vec<MdTree<'a>>) -> Self {
74        Self(value)
75    }
76}