tailwind_rs_core/config/
toml_config.rs

1//! TOML configuration structures for tailwind-rs
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// TOML representation of the main configuration
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct TailwindConfigToml {
9    pub build: BuildConfigToml,
10    pub theme: crate::theme::ThemeToml,
11    pub responsive: ResponsiveConfigToml,
12    pub plugins: Option<Vec<String>>,
13    pub custom: Option<HashMap<String, toml::Value>>,
14}
15
16/// TOML representation of build configuration
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct BuildConfigToml {
19    pub input: Option<Vec<String>>,
20    pub output: Option<String>,
21    pub watch: Option<bool>,
22    pub minify: Option<bool>,
23    pub source_maps: Option<bool>,
24    pub purge: Option<bool>,
25    pub additional_css: Option<Vec<String>>,
26    pub postcss_plugins: Option<Vec<String>>,
27}
28
29/// TOML representation of responsive configuration
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct ResponsiveConfigToml {
32    pub breakpoints: HashMap<String, u32>,
33    pub container_centering: bool,
34    pub container_padding: u32,
35}
36
37impl From<super::TailwindConfig> for TailwindConfigToml {
38    fn from(config: super::TailwindConfig) -> Self {
39        Self {
40            build: BuildConfigToml {
41                input: Some(config.build.input),
42                output: Some(config.build.output),
43                watch: Some(config.build.watch),
44                minify: Some(config.build.minify),
45                source_maps: Some(config.build.source_maps),
46                purge: Some(config.build.purge),
47                additional_css: Some(config.build.additional_css),
48                postcss_plugins: Some(config.build.postcss_plugins),
49            },
50            theme: config.theme.into(),
51            responsive: ResponsiveConfigToml {
52                breakpoints: super::TailwindConfig::convert_breakpoints_to_toml(
53                    &config.responsive.breakpoints,
54                ),
55                container_centering: false, // Default value since this field doesn't exist in ResponsiveConfig
56                container_padding: 0, // Default value since this field doesn't exist in ResponsiveConfig
57            },
58            plugins: Some(config.plugins),
59            custom: Some(super::TailwindConfig::convert_json_to_toml_values(
60                &config.custom,
61            )),
62        }
63    }
64}
65
66impl From<ResponsiveConfigToml> for crate::responsive::ResponsiveConfig {
67    fn from(toml_config: ResponsiveConfigToml) -> Self {
68        let mut breakpoints = HashMap::new();
69        for (key, width) in toml_config.breakpoints {
70            if let Ok(breakpoint) = key.parse::<crate::responsive::Breakpoint>() {
71                breakpoints.insert(
72                    breakpoint,
73                    crate::responsive::responsive_config::BreakpointConfig {
74                        min_width: width,
75                        max_width: None,
76                        enabled: true,
77                        media_query: None,
78                    },
79                );
80            }
81        }
82
83        Self {
84            breakpoints,
85            defaults: crate::responsive::responsive_config::ResponsiveDefaults {
86                default_breakpoint: crate::responsive::Breakpoint::Sm,
87                include_base: true,
88                mobile_first: true,
89            },
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn test_toml_config_conversion() {
100        let config = crate::config::TailwindConfig::new();
101        let toml_config: TailwindConfigToml = config.clone().into();
102
103        assert_eq!(toml_config.build.output, Some(config.build.output));
104        assert_eq!(toml_config.build.minify, Some(config.build.minify));
105    }
106
107    #[test]
108    fn test_responsive_config_conversion() {
109        let mut breakpoints = HashMap::new();
110        breakpoints.insert("sm".to_string(), 640);
111        breakpoints.insert("md".to_string(), 768);
112
113        let toml_config = ResponsiveConfigToml {
114            breakpoints,
115            container_centering: true,
116            container_padding: 16,
117        };
118
119        let responsive_config: crate::responsive::ResponsiveConfig = toml_config.into();
120        assert!(!responsive_config.breakpoints.is_empty());
121    }
122}