rustdoc_json_to_markdown/
lib.rs

1//! Convert rustdoc JSON output to clean, LLM-friendly markdown documentation.
2//!
3//! This library provides functionality to parse rustdoc's JSON output and convert it
4//! to well-formatted markdown files suitable for LLM consumption and text-based viewing.
5//!
6//! # Example
7//!
8//! ```no_run
9//! use rustdoc_json_to_markdown::{convert_json_file, ConversionOptions};
10//! use std::path::Path;
11//!
12//! let options = ConversionOptions {
13//!     input_path: Path::new("target/doc/my_crate.json"),
14//!     output_dir: Path::new("docs"),
15//!     include_private: false,
16//! };
17//!
18//! convert_json_file(&options).expect("Conversion failed");
19//! ```
20
21pub mod parser;
22pub mod converter;
23pub mod writer;
24
25pub use rustdoc_types;
26
27use anyhow::Result;
28use std::path::Path;
29
30/// Options for converting rustdoc JSON to markdown.
31pub struct ConversionOptions<'a> {
32    /// Path to the input rustdoc JSON file
33    pub input_path: &'a Path,
34    /// Directory where markdown files will be written
35    pub output_dir: &'a Path,
36    /// Whether to include private items in the output
37    pub include_private: bool,
38}
39
40/// Convert a rustdoc JSON file to markdown (multi-file output).
41///
42/// This is the main entry point for library usage. Generates one file per module.
43///
44/// # Arguments
45///
46/// * `options` - Configuration for the conversion
47///
48/// # Returns
49///
50/// Returns `Ok(())` on success, or an error if the conversion fails.
51///
52/// # Example
53///
54/// ```no_run
55/// use rustdoc_json_to_markdown::{convert_json_file, ConversionOptions};
56/// use std::path::Path;
57///
58/// let options = ConversionOptions {
59///     input_path: Path::new("target/doc/my_crate.json"),
60///     output_dir: Path::new("docs"),
61///     include_private: false,
62/// };
63///
64/// convert_json_file(&options).expect("Conversion failed");
65/// ```
66pub fn convert_json_file(options: &ConversionOptions) -> Result<()> {
67    let crate_data = parser::load_rustdoc_json(options.input_path)?;
68    let output = converter::convert_to_markdown_multifile(&crate_data, options.include_private)?;
69
70    // Write to crate-specific subdirectory
71    let crate_output_dir = options.output_dir.join(&output.crate_name);
72    writer::write_markdown_multifile(&crate_output_dir, &output)?;
73    Ok(())
74}
75
76
77/// Convert rustdoc JSON data (already loaded) to markdown.
78///
79/// Use this if you want more control over the loading and writing process.
80///
81/// # Arguments
82///
83/// * `json_data` - The rustdoc JSON as a string
84/// * `include_private` - Whether to include private items
85///
86/// # Returns
87///
88/// Returns the markdown as a String, or an error.
89pub fn convert_json_string(json_data: &str, include_private: bool) -> Result<String> {
90    let crate_data: rustdoc_types::Crate = serde_json::from_str(json_data)?;
91    converter::convert_to_markdown(&crate_data, include_private)
92}