cargo_docs_md/generator/
config.rs

1//! Configuration for markdown rendering.
2//!
3//! This module provides [`RenderConfig`] for controlling how documentation
4//! is rendered, and [`SourceConfig`] for source code integration options.
5
6use std::path::PathBuf;
7
8/// Configuration options for markdown rendering.
9#[derive(Debug, Clone)]
10#[expect(
11    clippy::struct_excessive_bools,
12    reason = "CLI flags map directly to these bools; cache optimization unnecessary here"
13)]
14pub struct RenderConfig {
15    /// Generate table of contents for modules with more than this many items.
16    pub toc_threshold: usize,
17
18    /// Generate quick reference tables at the top of modules.
19    pub quick_reference: bool,
20
21    /// Group impl blocks by category (Derive, Conversion, Iterator, etc.).
22    pub group_impls: bool,
23
24    /// Hide trivial derive implementations (Clone, Copy, Debug, etc.).
25    pub hide_trivial_derives: bool,
26
27    /// Generate method-level anchors for deep linking.
28    pub method_anchors: bool,
29
30    /// Include full method documentation instead of first-paragraph summaries.
31    ///
32    /// When `false` (default), method docs in impl blocks show only the first
33    /// paragraph (up to the first blank line). When `true`, the complete
34    /// documentation is included.
35    pub full_method_docs: bool,
36
37    /// Source code integration options.
38    pub include_source: SourceConfig,
39}
40
41/// Configuration for source code integration.
42///
43/// Requires the `source-parsing` feature to have any effect.
44#[derive(Debug, Clone, Default)]
45#[expect(
46    clippy::struct_excessive_bools,
47    reason = "TODO: Consider cache line efficiency later"
48)]
49pub struct SourceConfig {
50    /// Include function bodies in collapsible sections.
51    pub function_bodies: bool,
52
53    /// Show actual values for constants and statics.
54    pub const_values: bool,
55
56    /// Include private items in a separate section.
57    pub private_items: bool,
58
59    /// Add <file:line> references to items.
60    pub source_locations: bool,
61
62    /// Path to the `.source_*` directory containing collected dependency sources.
63    ///
64    /// When set, source location references will use paths relative to this directory
65    /// and generate clickable links. When `None`, absolute paths from rustdoc JSON
66    /// are displayed without links.
67    pub source_dir: Option<PathBuf>,
68}
69
70impl Default for RenderConfig {
71    fn default() -> Self {
72        Self {
73            toc_threshold: 10,
74            quick_reference: true,
75            group_impls: true,
76            hide_trivial_derives: false,
77            method_anchors: true,
78            full_method_docs: false,
79            include_source: SourceConfig::default(),
80        }
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn render_config_default_values() {
90        let config = RenderConfig::default();
91
92        assert_eq!(config.toc_threshold, 10);
93        assert!(config.quick_reference);
94        assert!(config.group_impls);
95        assert!(!config.hide_trivial_derives);
96        assert!(config.method_anchors);
97        assert!(!config.full_method_docs);
98    }
99
100    #[test]
101    fn source_config_default_values() {
102        let config = SourceConfig::default();
103
104        assert!(!config.function_bodies);
105        assert!(!config.const_values);
106        assert!(!config.private_items);
107        assert!(!config.source_locations);
108    }
109
110    #[test]
111    fn render_config_source_config_default() {
112        let config = RenderConfig::default();
113        let source = &config.include_source;
114
115        // Source config within RenderConfig should also have defaults
116        assert!(!source.function_bodies);
117        assert!(!source.const_values);
118        assert!(!source.private_items);
119        assert!(!source.source_locations);
120    }
121
122    #[test]
123    fn render_config_custom_values() {
124        let config = RenderConfig {
125            toc_threshold: 5,
126            quick_reference: false,
127            group_impls: false,
128            hide_trivial_derives: true,
129            method_anchors: false,
130            full_method_docs: true,
131            include_source: SourceConfig {
132                function_bodies: true,
133                const_values: true,
134                private_items: true,
135                source_locations: true,
136                source_dir: Some(PathBuf::from(".source_12345")),
137            },
138        };
139
140        assert_eq!(config.toc_threshold, 5);
141        assert!(!config.quick_reference);
142        assert!(!config.group_impls);
143        assert!(config.hide_trivial_derives);
144        assert!(!config.method_anchors);
145        assert!(config.full_method_docs);
146        assert!(config.include_source.function_bodies);
147        assert!(config.include_source.const_values);
148        assert!(config.include_source.private_items);
149        assert!(config.include_source.source_locations);
150        assert!(config.include_source.source_dir.is_some());
151    }
152}