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
125 .input
126 .unwrap_or_else(|| vec!["src/**/*.rs".to_string()]),
127 output: toml_config
128 .output
129 .unwrap_or_else(|| "dist/styles.css".to_string()),
130 watch: toml_config.watch.unwrap_or(false),
131 minify: toml_config.minify.unwrap_or(false),
132 source_maps: toml_config.source_maps.unwrap_or(false),
133 purge: toml_config.purge.unwrap_or(true),
134 additional_css: toml_config.additional_css.unwrap_or_default(),
135 postcss_plugins: toml_config.postcss_plugins.unwrap_or_default(),
136 }
137 }
138}
139
140#[cfg(test)]
141mod tests {
142 use super::*;
143
144 #[test]
145 fn test_build_config_creation() {
146 let config = BuildConfig::new();
147 assert!(!config.input.is_empty());
148 assert!(!config.output.is_empty());
149 assert!(!config.watch);
150 assert!(!config.minify);
151 assert!(config.purge);
152 }
153
154 #[test]
155 fn test_build_config_validation() {
156 let mut config = BuildConfig::new();
157 assert!(config.validate().is_ok());
158
159 config.output = "".to_string();
160 assert!(config.validate().is_err());
161
162 config.output = "dist/styles.css".to_string();
163 config.input.clear();
164 assert!(config.validate().is_err());
165 }
166
167 #[test]
168 fn test_build_config_methods() {
169 let mut config = BuildConfig::new();
170
171 config.add_input("src/**/*.rsx");
172 assert!(config.input.contains(&"src/**/*.rsx".to_string()));
173
174 config.set_output("build/styles.css");
175 assert_eq!(config.output, "build/styles.css");
176
177 config.enable_watch();
178 assert!(config.watch);
179
180 config.enable_minify();
181 assert!(config.minify);
182
183 config.add_css("src/custom.css");
184 assert!(config
185 .additional_css
186 .contains(&"src/custom.css".to_string()));
187 }
188}