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 = "TODO: Consider cache line efficiency later"
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    /// Source code integration options.
31    pub include_source: SourceConfig,
32}
33
34/// Configuration for source code integration.
35///
36/// Requires the `source-parsing` feature to have any effect.
37#[derive(Debug, Clone, Default)]
38#[expect(
39    clippy::struct_excessive_bools,
40    reason = "TODO: Consider cache line efficiency later"
41)]
42pub struct SourceConfig {
43    /// Include function bodies in collapsible sections.
44    pub function_bodies: bool,
45
46    /// Show actual values for constants and statics.
47    pub const_values: bool,
48
49    /// Include private items in a separate section.
50    pub private_items: bool,
51
52    /// Add <file:line> references to items.
53    pub source_locations: bool,
54
55    /// Path to the `.source_*` directory containing collected dependency sources.
56    ///
57    /// When set, source location references will use paths relative to this directory
58    /// and generate clickable links. When `None`, absolute paths from rustdoc JSON
59    /// are displayed without links.
60    pub source_dir: Option<PathBuf>,
61}
62
63impl Default for RenderConfig {
64    fn default() -> Self {
65        Self {
66            toc_threshold: 10,
67            quick_reference: true,
68            group_impls: true,
69            hide_trivial_derives: false,
70            method_anchors: true,
71            include_source: SourceConfig::default(),
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn render_config_default_values() {
82        let config = RenderConfig::default();
83
84        assert_eq!(config.toc_threshold, 10);
85        assert!(config.quick_reference);
86        assert!(config.group_impls);
87        assert!(!config.hide_trivial_derives);
88        assert!(config.method_anchors);
89    }
90
91    #[test]
92    fn source_config_default_values() {
93        let config = SourceConfig::default();
94
95        assert!(!config.function_bodies);
96        assert!(!config.const_values);
97        assert!(!config.private_items);
98        assert!(!config.source_locations);
99    }
100
101    #[test]
102    fn render_config_source_config_default() {
103        let config = RenderConfig::default();
104        let source = &config.include_source;
105
106        // Source config within RenderConfig should also have defaults
107        assert!(!source.function_bodies);
108        assert!(!source.const_values);
109        assert!(!source.private_items);
110        assert!(!source.source_locations);
111    }
112
113    #[test]
114    fn render_config_custom_values() {
115        let config = RenderConfig {
116            toc_threshold: 5,
117            quick_reference: false,
118            group_impls: false,
119            hide_trivial_derives: true,
120            method_anchors: false,
121            include_source: SourceConfig {
122                function_bodies: true,
123                const_values: true,
124                private_items: true,
125                source_locations: true,
126                source_dir: Some(PathBuf::from(".source_12345")),
127            },
128        };
129
130        assert_eq!(config.toc_threshold, 5);
131        assert!(!config.quick_reference);
132        assert!(!config.group_impls);
133        assert!(config.hide_trivial_derives);
134        assert!(!config.method_anchors);
135        assert!(config.include_source.function_bodies);
136        assert!(config.include_source.const_values);
137        assert!(config.include_source.private_items);
138        assert!(config.include_source.source_locations);
139        assert!(config.include_source.source_dir.is_some());
140    }
141}