dioxus_docs_kit/config.rs
1//! Builder for constructing a `DocsRegistry`.
2
3use crate::registry::DocsRegistry;
4use std::collections::HashMap;
5
6/// Theme configuration for the documentation site.
7///
8/// Controls which DaisyUI theme(s) are applied and whether a toggle button is shown.
9#[derive(Clone, Debug)]
10pub struct ThemeConfig {
11 /// The default theme name (must match a DaisyUI theme defined in `tailwind.css`).
12 pub default_theme: String,
13 /// If set, enables a light/dark toggle button. Tuple is `(light_theme, dark_theme)`.
14 pub toggle_themes: Option<(String, String)>,
15 /// localStorage key used to persist the user's theme preference.
16 pub storage_key: String,
17}
18
19/// Builder for constructing a [`DocsRegistry`].
20///
21/// # Example
22///
23/// ```rust,ignore
24/// let registry = DocsConfig::new(nav_json, content_map)
25/// .with_openapi("api-reference", spec_yaml)
26/// .with_default_path("getting-started/introduction")
27/// .build();
28/// ```
29pub struct DocsConfig {
30 nav_json: String,
31 content_map: HashMap<&'static str, &'static str>,
32 openapi_specs: Vec<(String, String)>,
33 default_path: Option<String>,
34 api_group_name: Option<String>,
35 theme: Option<ThemeConfig>,
36}
37
38impl DocsConfig {
39 /// Create a new builder from a `_nav.json` string and a content map.
40 ///
41 /// The content map is typically generated by `build.rs` using `include_str!()`.
42 pub fn new(nav_json: &str, content_map: HashMap<&'static str, &'static str>) -> Self {
43 Self {
44 nav_json: nav_json.to_string(),
45 content_map,
46 openapi_specs: Vec::new(),
47 default_path: None,
48 api_group_name: None,
49 theme: None,
50 }
51 }
52
53 /// Add an OpenAPI specification.
54 ///
55 /// - `prefix`: The URL prefix for this spec's endpoints (e.g. "api-reference").
56 /// - `yaml`: The raw YAML string of the OpenAPI spec.
57 pub fn with_openapi(mut self, prefix: &str, yaml: &str) -> Self {
58 self.openapi_specs
59 .push((prefix.to_string(), yaml.to_string()));
60 self
61 }
62
63 /// Set the default documentation path for redirects.
64 ///
65 /// Defaults to the first page in the first nav group if not set.
66 pub fn with_default_path(mut self, path: &str) -> Self {
67 self.default_path = Some(path.to_string());
68 self
69 }
70
71 /// Set the display name for the API Reference sidebar group.
72 ///
73 /// Defaults to "API Reference".
74 pub fn with_api_group_name(mut self, name: &str) -> Self {
75 self.api_group_name = Some(name.to_string());
76 self
77 }
78
79 /// Set a single theme (no toggle button).
80 ///
81 /// The theme name must match a DaisyUI theme defined in the consumer's `tailwind.css`.
82 pub fn with_theme(mut self, theme: &str) -> Self {
83 self.theme = Some(ThemeConfig {
84 default_theme: theme.to_string(),
85 toggle_themes: None,
86 storage_key: "docs-theme".to_string(),
87 });
88 self
89 }
90
91 /// Enable a light/dark theme toggle.
92 ///
93 /// - `light`: Name of the light DaisyUI theme.
94 /// - `dark`: Name of the dark DaisyUI theme.
95 /// - `default`: Which of the two to use on first visit (`light` or `dark`).
96 pub fn with_theme_toggle(mut self, light: &str, dark: &str, default: &str) -> Self {
97 self.theme = Some(ThemeConfig {
98 default_theme: default.to_string(),
99 toggle_themes: Some((light.to_string(), dark.to_string())),
100 storage_key: "docs-theme".to_string(),
101 });
102 self
103 }
104
105 /// Build the [`DocsRegistry`].
106 ///
107 /// Parses all documents, builds the search index, and parses OpenAPI specs.
108 pub fn build(self) -> DocsRegistry {
109 DocsRegistry::from_config(self)
110 }
111
112 // Accessors for DocsRegistry::from_config
113 pub(crate) fn nav_json(&self) -> &str {
114 &self.nav_json
115 }
116
117 pub(crate) fn content_map(&self) -> &HashMap<&'static str, &'static str> {
118 &self.content_map
119 }
120
121 pub(crate) fn openapi_specs(&self) -> &[(String, String)] {
122 &self.openapi_specs
123 }
124
125 pub(crate) fn default_path_value(&self) -> Option<&str> {
126 self.default_path.as_deref()
127 }
128
129 pub(crate) fn api_group_name_value(&self) -> Option<&str> {
130 self.api_group_name.as_deref()
131 }
132
133 pub(crate) fn theme_config(&self) -> Option<&ThemeConfig> {
134 self.theme.as_ref()
135 }
136}