tailwind_rs_core/config/
build.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub struct BuildConfig {
8    pub input: Vec<String>,
10    pub output: String,
12    pub watch: bool,
14    pub minify: bool,
16    pub source_maps: bool,
18    pub purge: bool,
20    pub additional_css: Vec<String>,
22    pub postcss_plugins: Vec<String>,
24}
25
26impl BuildConfig {
27    pub fn new() -> Self {
29        Self {
30            input: vec!["src/**/*.rs".to_string()],
31            output: "dist/styles.css".to_string(),
32            watch: false,
33            minify: false,
34            source_maps: false,
35            purge: true,
36            additional_css: Vec::new(),
37            postcss_plugins: Vec::new(),
38        }
39    }
40
41    pub fn add_input(&mut self, path: impl Into<String>) {
43        self.input.push(path.into());
44    }
45
46    pub fn set_output(&mut self, path: impl Into<String>) {
48        self.output = path.into();
49    }
50
51    pub fn enable_watch(&mut self) {
53        self.watch = true;
54    }
55
56    pub fn disable_watch(&mut self) {
58        self.watch = false;
59    }
60
61    pub fn enable_minify(&mut self) {
63        self.minify = true;
64    }
65
66    pub fn disable_minify(&mut self) {
68        self.minify = false;
69    }
70
71    pub fn enable_source_maps(&mut self) {
73        self.source_maps = true;
74    }
75
76    pub fn disable_source_maps(&mut self) {
78        self.source_maps = false;
79    }
80
81    pub fn enable_purge(&mut self) {
83        self.purge = true;
84    }
85
86    pub fn disable_purge(&mut self) {
88        self.purge = false;
89    }
90
91    pub fn add_css(&mut self, path: impl Into<String>) {
93        self.additional_css.push(path.into());
94    }
95
96    pub fn add_postcss_plugin(&mut self, plugin: impl Into<String>) {
98        self.postcss_plugins.push(plugin.into());
99    }
100
101    pub fn validate(&self) -> Result<(), String> {
103        if self.output.is_empty() {
104            return Err("Output path cannot be empty".to_string());
105        }
106
107        if self.input.is_empty() {
108            return Err("Input paths cannot be empty".to_string());
109        }
110
111        Ok(())
112    }
113}
114
115impl Default for BuildConfig {
116    fn default() -> Self {
117        Self::new()
118    }
119}
120
121impl From<super::toml_config::BuildConfigToml> for BuildConfig {
122    fn from(toml_config: super::toml_config::BuildConfigToml) -> Self {
123        Self {
124            input: toml_config.input.unwrap_or_else(|| vec!["src/**/*.rs".to_string()]),
125            output: toml_config.output.unwrap_or_else(|| "dist/styles.css".to_string()),
126            watch: toml_config.watch.unwrap_or(false),
127            minify: toml_config.minify.unwrap_or(false),
128            source_maps: toml_config.source_maps.unwrap_or(false),
129            purge: toml_config.purge.unwrap_or(true),
130            additional_css: toml_config.additional_css.unwrap_or_default(),
131            postcss_plugins: toml_config.postcss_plugins.unwrap_or_default(),
132        }
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139
140    #[test]
141    fn test_build_config_creation() {
142        let config = BuildConfig::new();
143        assert!(!config.input.is_empty());
144        assert!(!config.output.is_empty());
145        assert!(!config.watch);
146        assert!(!config.minify);
147        assert!(config.purge);
148    }
149
150    #[test]
151    fn test_build_config_validation() {
152        let mut config = BuildConfig::new();
153        assert!(config.validate().is_ok());
154
155        config.output = "".to_string();
156        assert!(config.validate().is_err());
157
158        config.output = "dist/styles.css".to_string();
159        config.input.clear();
160        assert!(config.validate().is_err());
161    }
162
163    #[test]
164    fn test_build_config_methods() {
165        let mut config = BuildConfig::new();
166        
167        config.add_input("src/**/*.rsx");
168        assert!(config.input.contains(&"src/**/*.rsx".to_string()));
169
170        config.set_output("build/styles.css");
171        assert_eq!(config.output, "build/styles.css");
172
173        config.enable_watch();
174        assert!(config.watch);
175
176        config.enable_minify();
177        assert!(config.minify);
178
179        config.add_css("src/custom.css");
180        assert!(config.additional_css.contains(&"src/custom.css".to_string()));
181    }
182}