pyo3_stub_gen/docgen/
config.rs

1//! Configuration for documentation generation
2
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6/// Configuration for documentation generation from pyproject.toml
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct DocGenConfig {
9    /// Output directory for generated documentation
10    #[serde(rename = "output-dir", default = "default_output_dir")]
11    pub output_dir: PathBuf,
12
13    /// Name of the JSON output file
14    #[serde(rename = "json-output", default = "default_json_output")]
15    pub json_output: String,
16
17    /// Generate separate .rst pages for each module (default: true)
18    #[serde(rename = "separate-pages", default = "default_separate_pages")]
19    pub separate_pages: bool,
20
21    /// Custom intro message for index.rst (default: standard message, empty string to omit)
22    #[serde(rename = "intro-message", default)]
23    pub intro_message: Option<String>,
24
25    /// Custom title for index.rst (default: "{package_name} API Reference", empty string to use "API Reference")
26    #[serde(rename = "index-title", default)]
27    pub index_title: Option<String>,
28
29    /// Generate module contents tables (default: false)
30    #[serde(rename = "contents-table", default)]
31    pub contents_table: bool,
32}
33
34impl Default for DocGenConfig {
35    fn default() -> Self {
36        Self {
37            output_dir: default_output_dir(),
38            json_output: default_json_output(),
39            separate_pages: default_separate_pages(),
40            intro_message: None,
41            index_title: None,
42            contents_table: false,
43        }
44    }
45}
46
47fn default_output_dir() -> PathBuf {
48    PathBuf::from("docs/api")
49}
50
51fn default_json_output() -> String {
52    "api_reference.json".to_string()
53}
54
55fn default_separate_pages() -> bool {
56    true
57}
58
59impl DocGenConfig {
60    /// Convert output_dir to relative POSIX path for JSON serialization
61    pub fn to_relative_posix_path(&self, base_dir: &std::path::Path) -> String {
62        let relative_path = if self.output_dir.is_absolute() {
63            self.output_dir
64                .strip_prefix(base_dir)
65                .unwrap_or(&self.output_dir)
66        } else {
67            &self.output_dir
68        };
69
70        // Convert to POSIX format (forward slashes)
71        relative_path
72            .components()
73            .filter_map(|c| match c {
74                std::path::Component::Normal(s) => Some(s.to_string_lossy().into_owned()),
75                _ => None,
76            })
77            .collect::<Vec<_>>()
78            .join("/")
79    }
80}