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(&config.responsive.breakpoints),
53                container_centering: false, // Default value since this field doesn't exist in ResponsiveConfig
54                container_padding: 0, // Default value since this field doesn't exist in ResponsiveConfig
55            },
56            plugins: Some(config.plugins),
57            custom: Some(super::TailwindConfig::convert_json_to_toml_values(&config.custom)),
58        }
59    }
60}
61
62impl From<ResponsiveConfigToml> for crate::responsive::ResponsiveConfig {
63    fn from(toml_config: ResponsiveConfigToml) -> Self {
64        let mut breakpoints = HashMap::new();
65        for (key, width) in toml_config.breakpoints {
66            if let Ok(breakpoint) = key.parse::<crate::responsive::Breakpoint>() {
67                breakpoints.insert(breakpoint, crate::responsive::responsive_config::BreakpointConfig { 
68                    min_width: width,
69                    max_width: None,
70                    enabled: true,
71                    media_query: None,
72                });
73            }
74        }
75        
76        Self {
77            breakpoints,
78            defaults: crate::responsive::responsive_config::ResponsiveDefaults {
79                default_breakpoint: crate::responsive::Breakpoint::Sm,
80                include_base: true,
81                mobile_first: true,
82            },
83        }
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn test_toml_config_conversion() {
93        let config = crate::config::TailwindConfig::new();
94        let toml_config: TailwindConfigToml = config.clone().into();
95        
96        assert_eq!(toml_config.build.output, Some(config.build.output));
97        assert_eq!(toml_config.build.minify, Some(config.build.minify));
98    }
99
100    #[test]
101    fn test_responsive_config_conversion() {
102        let mut breakpoints = HashMap::new();
103        breakpoints.insert("sm".to_string(), 640);
104        breakpoints.insert("md".to_string(), 768);
105        
106        let toml_config = ResponsiveConfigToml {
107            breakpoints,
108            container_centering: true,
109            container_padding: 16,
110        };
111        
112        let responsive_config: crate::responsive::ResponsiveConfig = toml_config.into();
113        assert!(!responsive_config.breakpoints.is_empty());
114    }
115}