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.
41///
42/// This is the main entry point for library usage.
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 markdown = converter::convert_to_markdown(&crate_data, options.include_private)?;
69 writer::write_markdown(options.output_dir, &markdown)?;
70 Ok(())
71}
72
73/// Convert rustdoc JSON data (already loaded) to markdown.
74///
75/// Use this if you want more control over the loading and writing process.
76///
77/// # Arguments
78///
79/// * `json_data` - The rustdoc JSON as a string
80/// * `include_private` - Whether to include private items
81///
82/// # Returns
83///
84/// Returns the markdown as a String, or an error.
85pub fn convert_json_string(json_data: &str, include_private: bool) -> Result<String> {
86 let crate_data: rustdoc_types::Crate = serde_json::from_str(json_data)?;
87 converter::convert_to_markdown(&crate_data, include_private)
88}