serde_doc/generators/
markdown.rs1use std::io::Write;
2
3use crate::Context;
4
5use super::{Generator, GeneratorConfig};
6use anyhow::{Context as _, Result};
7
8
9pub struct MarkdownGenerator {}
10
11impl MarkdownGenerator {
12 pub fn new() -> Self {
13 MarkdownGenerator {}
14 }
15}
16
17
18impl Generator for MarkdownGenerator {
19 fn generate(&self, ctx: &Context, config: &GeneratorConfig) -> Result<()> {
20 let mut content = String::new();
21 content.push_str("## Structs\n");
23 for file_unit in &ctx.files {
24 for struct_unit in &file_unit.structs {
25
26 content.push_str(&format!("### {}\n", struct_unit.name));
27
28 if let Some(comment) = &struct_unit.doc {
29 content.push_str(&format!("{}\n", comment));
30 }
31 content.push_str("Fields:\n");
32 for field in &struct_unit.fields {
33 content.push_str(&format!("- {}: {}\n", field.name, field.ty));
34 if let Some(comment) = &field.doc {
35 content.push_str(&format!(" - {}\n", comment));
36 }
37 }
38 }
39 }
40
41 match &config.output {
42 Some(output) => {
43
44 let mut file = std::fs::File::create(&output)?;
45
46 file.write(content.as_bytes())
47 .with_context(|| format!("failed to write to file: {:?}", output))?;
48
49 }
50 None => {
51 println!("{}", content);
52 }
53 }
54
55 Ok(())
56 }
57}