use crate::registry::DocsRegistry;
#[cfg(feature = "highlight")]
use dioxus_code::Theme;
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq)]
pub struct ThemeConfig {
pub default_theme: String,
pub toggle_themes: Option<(String, String)>,
pub storage_key: String,
}
#[cfg(feature = "highlight")]
#[derive(Clone, Copy, Debug)]
pub enum CodeThemeConfig {
Fixed(Theme),
Adaptive {
light: Theme,
dark: Theme,
},
}
#[cfg(feature = "highlight")]
impl Default for CodeThemeConfig {
fn default() -> Self {
Self::Adaptive {
light: Theme::GITHUB_LIGHT,
dark: Theme::TOKYO_NIGHT,
}
}
}
pub struct DocsConfig {
nav_json: String,
content_map: HashMap<&'static str, &'static str>,
openapi_specs: Vec<(String, String)>,
default_path: Option<String>,
api_group_name: Option<String>,
theme: Option<ThemeConfig>,
#[cfg(feature = "highlight")]
code_theme: CodeThemeConfig,
}
impl DocsConfig {
pub fn new(nav_json: &str, content_map: HashMap<&'static str, &'static str>) -> Self {
Self {
nav_json: nav_json.to_string(),
content_map,
openapi_specs: Vec::new(),
default_path: None,
api_group_name: None,
theme: None,
#[cfg(feature = "highlight")]
code_theme: CodeThemeConfig::default(),
}
}
pub fn with_openapi(mut self, prefix: &str, yaml: &str) -> Self {
self.openapi_specs
.push((prefix.to_string(), yaml.to_string()));
self
}
pub fn with_default_path(mut self, path: &str) -> Self {
self.default_path = Some(path.to_string());
self
}
pub fn with_api_group_name(mut self, name: &str) -> Self {
self.api_group_name = Some(name.to_string());
self
}
pub fn with_theme(mut self, theme: &str) -> Self {
self.theme = Some(ThemeConfig {
default_theme: theme.to_string(),
toggle_themes: None,
storage_key: "docs-theme".to_string(),
});
self
}
pub fn with_theme_toggle(mut self, light: &str, dark: &str, default: &str) -> Self {
self.theme = Some(ThemeConfig {
default_theme: default.to_string(),
toggle_themes: Some((light.to_string(), dark.to_string())),
storage_key: "docs-theme".to_string(),
});
self
}
#[cfg(feature = "highlight")]
pub fn with_code_theme(mut self, theme: Theme) -> Self {
self.code_theme = CodeThemeConfig::Fixed(theme);
self
}
#[cfg(feature = "highlight")]
pub fn with_code_themes(mut self, light: Theme, dark: Theme) -> Self {
self.code_theme = CodeThemeConfig::Adaptive { light, dark };
self
}
pub fn build(self) -> DocsRegistry {
self.try_build()
.unwrap_or_else(|e| panic!("dioxus-docs-kit: {e}"))
}
pub fn try_build(self) -> Result<DocsRegistry, crate::error::DocsKitError> {
DocsRegistry::try_from_config(self)
}
pub(crate) fn nav_json(&self) -> &str {
&self.nav_json
}
pub(crate) fn content_map(&self) -> &HashMap<&'static str, &'static str> {
&self.content_map
}
pub(crate) fn openapi_specs(&self) -> &[(String, String)] {
&self.openapi_specs
}
pub(crate) fn default_path_value(&self) -> Option<&str> {
self.default_path.as_deref()
}
pub(crate) fn api_group_name_value(&self) -> Option<&str> {
self.api_group_name.as_deref()
}
pub(crate) fn theme_config(&self) -> Option<&ThemeConfig> {
self.theme.as_ref()
}
#[cfg(feature = "highlight")]
pub(crate) fn code_theme_value(&self) -> CodeThemeConfig {
self.code_theme
}
}