rustdoc_json_to_markdown/
writer.rs

1//! Markdown file writer.
2
3use anyhow::{Context, Result};
4use std::fs;
5use std::path::Path;
6use crate::converter::MarkdownOutput;
7
8/// Write markdown content to a file in the specified directory.
9pub fn write_markdown(output_dir: &Path, content: &str) -> Result<()> {
10    fs::create_dir_all(output_dir)
11        .with_context(|| format!("Failed to create output directory: {}", output_dir.display()))?;
12
13    let output_file = output_dir.join("index.md");
14
15    fs::write(&output_file, content)
16        .with_context(|| format!("Failed to write file: {}", output_file.display()))?;
17
18    Ok(())
19}
20
21/// Write multi-file markdown output to the specified directory.
22pub fn write_markdown_multifile(output_dir: &Path, output: &MarkdownOutput) -> Result<()> {
23    fs::create_dir_all(output_dir)
24        .with_context(|| format!("Failed to create output directory: {}", output_dir.display()))?;
25
26    for (file_path, content) in &output.files {
27        let full_path = output_dir.join(file_path);
28
29        // Create parent directories if needed
30        if let Some(parent) = full_path.parent() {
31            fs::create_dir_all(parent)
32                .with_context(|| format!("Failed to create directory: {}", parent.display()))?;
33        }
34
35        fs::write(&full_path, content)
36            .with_context(|| format!("Failed to write file: {}", full_path.display()))?;
37    }
38
39    Ok(())
40}