forge_doc/writer/
markdown.rs
1use crate::{AsDoc, AsDocResult};
2
3#[derive(Debug)]
5pub enum Markdown<'a> {
6 H1(&'a str),
8 H2(&'a str),
10 H3(&'a str),
12 Italic(&'a str),
14 Bold(&'a str),
16 Link(&'a str, &'a str),
18 Code(&'a str),
20 CodeBlock(&'a str, &'a str),
22}
23
24impl AsDoc for Markdown<'_> {
25 fn as_doc(&self) -> AsDocResult {
26 let doc = match self {
27 Self::H1(val) => format!("# {val}"),
28 Self::H2(val) => format!("## {val}"),
29 Self::H3(val) => format!("### {val}"),
30 Self::Italic(val) => format!("*{val}*"),
31 Self::Bold(val) => format!("**{val}**"),
32 Self::Link(val, link) => format!("[{val}]({link})"),
33 Self::Code(val) => format!("`{val}`"),
34 Self::CodeBlock(lang, val) => format!("```{lang}\n{val}\n```"),
35 };
36 Ok(doc)
37 }
38}
39
40impl std::fmt::Display for Markdown<'_> {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 f.write_fmt(format_args!("{}", self.as_doc()?))
43 }
44}