nargo-style-processor 0.0.1

Nargo style processor
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#![warn(missing_docs)]

use nargo_types::{Error, Result, Span};
use notify::{self, Watcher};
use oak_core::{ParseSession, parse_one_pass};
use oak_css::{CssLanguage, CssParser};
use oak_scss::{ScssLanguage, ScssParser};
use oak_stylus::{StylusLanguage, StylusParser};
use std::path::Path;

/// CSS 预处理器类型
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PreprocessorType {
    /// 标准 CSS
    Css,
    /// SCSS/SASS
    Scss,
    /// Less
    Less,
    /// Stylus
    Stylus,
}

use std::collections::HashMap;

/// CSS 处理器,用于处理和优化 CSS 代码
#[derive(Default)]
pub struct StyleProcessor {
    /// 是否启用 CSS 压缩
    ///
    /// 当设置为 `true` 时,处理器会移除空白字符、注释和不必要的字符,
    /// 并压缩颜色值,以减少 CSS 文件大小。
    pub minify: bool,
    /// 是否移除未使用的样式
    ///
    /// 当设置为 `true` 时,处理器会根据提供的已使用选择器列表,
    /// 移除未被使用的 CSS 规则。
    pub remove_unused: bool,
    /// CSS 预处理器类型
    ///
    /// 用于指定要使用的 CSS 预处理器,如 SCSS、Less 或 Stylus。
    pub preprocessor: Option<PreprocessorType>,
    /// 缓存已处理的代码,提高性能
    ///
    /// 缓存键为 (代码内容, 预处理器类型) 的元组,值为处理后的代码。
    cache: HashMap<(String, PreprocessorType), String>,
    /// 缓存压缩后的 CSS 代码,提高性能
    ///
    /// 缓存键为原始 CSS 代码,值为压缩后的 CSS 代码。
    minify_cache: HashMap<String, String>,
}

impl StyleProcessor {
    /// 创建新的样式处理器
    pub fn new() -> Self {
        Self { minify: false, remove_unused: false, preprocessor: None, cache: HashMap::new(), minify_cache: HashMap::new() }
    }

    /// 设置是否启用压缩
    pub fn with_minify(mut self, minify: bool) -> Self {
        self.minify = minify;
        self
    }

    /// 设置是否移除未使用的样式
    pub fn with_remove_unused(mut self, remove_unused: bool) -> Self {
        self.remove_unused = remove_unused;
        self
    }

    /// 设置 CSS 预处理器类型
    pub fn with_preprocessor(mut self, preprocessor: PreprocessorType) -> Self {
        self.preprocessor = Some(preprocessor);
        self
    }

    /// 处理 CSS 代码
    ///
    /// # 参数
    /// * `css` - 要处理的 CSS 代码
    /// * `used_selectors` - 已使用的选择器列表,用于移除未使用的样式
    ///
    /// # 返回值
    /// 处理后的 CSS 代码
    pub fn process(&mut self, css: &str, used_selectors: Option<&Vec<String>>) -> Result<String> {
        let mut processed = css.to_string();

        // 处理预处理器代码
        if let Some(preprocessor) = self.preprocessor.clone() {
            processed = self.process_preprocessor(&processed, &preprocessor)?;
        }

        // 移除未使用的样式
        if self.remove_unused && used_selectors.is_some() {
            processed = self.remove_unused_styles(&processed, used_selectors.unwrap())?;
        }

        // 压缩 CSS
        if self.minify {
            processed = self.minify_css(&processed);
        }
        else {
            // 添加处理注释
            processed = format!("/* Processed by Nargo Style Processor */\n{}", processed);
        }

        Ok(processed)
    }

    /// 处理预处理器代码
    fn process_preprocessor(&mut self, code: &str, preprocessor: &PreprocessorType) -> Result<String> {
        let cache_key = (code.to_string(), preprocessor.clone());

        Self::get_or_compute(&mut self.cache, cache_key, || {
            match preprocessor {
                PreprocessorType::Css => {
                    // 使用 oak-css 解析 CSS
                    let language = CssLanguage::default();
                    let parser = CssParser::new(&language);
                    let mut session = ParseSession::new(1024);
                    let result = parse_one_pass(&parser, code, &mut session);
                    result.result.map_err(|e| Error::external_error("oak-css".to_string(), e.to_string(), Span::default()))?;
                    Ok(code.to_string())
                }
                PreprocessorType::Scss => {
                    // 使用 oak-scss 解析 SCSS
                    let language = ScssLanguage::default();
                    let parser = ScssParser::new(&language);
                    let mut session = ParseSession::new(1024);
                    let result = parse_one_pass(&parser, code, &mut session);
                    result.result.map_err(|e| Error::external_error("oak-scss".to_string(), e.to_string(), Span::default()))?;
                    Ok(code.to_string())
                }
                PreprocessorType::Less => Ok(code.to_string()),
                PreprocessorType::Stylus => {
                    // 使用 oak-stylus 解析 Stylus
                    let language = StylusLanguage::default();
                    let parser = StylusParser::new(&language);
                    let mut session = ParseSession::new(1024);
                    let result = parse_one_pass(&parser, code, &mut session);
                    result.result.map_err(|e| Error::external_error("oak-stylus".to_string(), e.to_string(), Span::default()))?;
                    Ok(code.to_string())
                }
            }
        })
    }

    /// 通用缓存处理方法
    ///
    /// # 参数
    /// * `cache` - 缓存 HashMap
    /// * `key` - 缓存键
    /// * `compute` - 计算函数,当缓存中不存在时执行
    ///
    /// # 返回值
    /// 缓存的值或计算的结果
    fn get_or_compute<K, V, F>(cache: &mut HashMap<K, V>, key: K, compute: F) -> Result<V>
    where
        K: std::hash::Hash + Eq,
        V: Clone,
        F: FnOnce() -> Result<V>,
    {
        if let Some(cached) = cache.get(&key) {
            Ok(cached.clone())
        }
        else {
            let value = compute()?;
            cache.insert(key, value.clone());
            Ok(value)
        }
    }

    /// 移除未使用的样式
    fn remove_unused_styles(&self, css: &str, used_selectors: &Vec<String>) -> Result<String> {
        // 简单的未使用样式移除实现
        let mut result = String::new();
        let mut remaining = css.to_string();

        while !remaining.is_empty() {
            // 查找规则开始
            if let Some(start_idx) = remaining.find('{') {
                // 提取选择器
                let selector = remaining[..start_idx].trim().to_string();

                // 查找规则结束
                let mut brace_count = 1;
                let mut end_idx = start_idx + 1;

                while end_idx < remaining.len() && brace_count > 0 {
                    if remaining.chars().nth(end_idx) == Some('{') {
                        brace_count += 1;
                    }
                    else if remaining.chars().nth(end_idx) == Some('}') {
                        brace_count -= 1;
                    }
                    end_idx += 1;
                }

                // 提取规则
                let rule = remaining[..end_idx].to_string();

                // 检查选择器是否被使用
                if self.is_selector_used(&selector, used_selectors) {
                    result.push_str(&rule);
                    result.push_str("\n");
                }

                // 更新剩余内容
                remaining = remaining[end_idx..].trim_start().to_string();
            }
            else {
                // 没有更多规则,添加剩余内容
                result.push_str(&remaining);
                break;
            }
        }

        Ok(result)
    }

    /// 检查选择器是否被使用
    fn is_selector_used(&self, selector: &str, used_selectors: &Vec<String>) -> bool {
        // 简单实现:检查选择器是否在使用列表中
        let selectors = selector.split(',').map(|s| s.trim());

        for s in selectors {
            if used_selectors.contains(&s.to_string()) {
                return true;
            }
        }

        false
    }

    /// 压缩 CSS
    ///
    /// # 参数
    /// * `css` - 要压缩的 CSS 代码
    ///
    /// # 返回值
    /// 压缩后的 CSS 代码
    fn minify_css(&mut self, css: &str) -> String {
        // 检查缓存
        if let Some(cached) = self.minify_cache.get(css) {
            return cached.clone();
        }

        let minified = self.perform_minification(css);

        // 存入缓存
        self.minify_cache.insert(css.to_string(), minified.clone());

        minified
    }

    /// 执行 CSS 压缩的核心逻辑
    fn perform_minification(&self, css: &str) -> String {
        let mut result = String::with_capacity(css.len());
        let mut in_comment = false;
        let mut in_string = false;
        let mut string_delimiter = '"';
        let mut chars = css.chars();

        while let Some(c) = chars.next() {
            if in_comment {
                // 处理注释
                if c == '*' {
                    if let Some(next) = chars.next() {
                        if next == '/' {
                            in_comment = false;
                        }
                    }
                }
                continue;
            }

            if in_string {
                // 处理字符串
                result.push(c);
                if c == string_delimiter {
                    // 检查是否被转义
                    let is_escaped = self.is_escaped(&result);
                    if !is_escaped {
                        in_string = false;
                    }
                }
                continue;
            }

            // 处理其他字符
            match c {
                '"' | '\'' => {
                    result.push(c);
                    in_string = true;
                    string_delimiter = c;
                }
                '/' => {
                    if let Some(next) = chars.next() {
                        if next == '*' {
                            in_comment = true;
                        }
                        else {
                            result.push('/');
                            result.push(next);
                        }
                    }
                    else {
                        result.push('/');
                    }
                }
                ' ' | '\t' | '\n' | '\r' => {
                    // 跳过所有空白字符
                }
                '#' => {
                    result.push('#');
                    // 读取并压缩颜色值
                    let mut hex_chars = String::new();
                    for _ in 0..6 {
                        if let Some(next) = chars.next() {
                            if next.is_ascii_hexdigit() {
                                hex_chars.push(next);
                            }
                            else {
                                result.push_str(&hex_chars);
                                result.push(next);
                                break;
                            }
                        }
                        else {
                            result.push_str(&hex_chars);
                            break;
                        }
                    }
                    // 尝试压缩颜色值
                    if hex_chars.len() == 6 {
                        if let Some(compressed) = self.compress_color(&hex_chars) {
                            result.push_str(&compressed);
                        }
                        else {
                            result.push_str(&hex_chars);
                        }
                    }
                    else if !hex_chars.is_empty() {
                        result.push_str(&hex_chars);
                    }
                }
                _ => {
                    result.push(c);
                }
            }
        }

        // 移除不必要的分号
        let trimmed = self.remove_unnecessary_semicolons(&result);

        trimmed
    }

    /// 检查字符是否被转义
    fn is_escaped(&self, result: &String) -> bool {
        let mut backslash_count = 0;
        for ch in result.chars().rev() {
            if ch == '\\' {
                backslash_count += 1;
            }
            else {
                break;
            }
        }
        backslash_count % 2 == 1
    }

    /// 压缩颜色值
    ///
    /// # 参数
    /// * `color` - 6位十六进制颜色值(不包含#)
    ///
    /// # 返回值
    /// 压缩后的颜色值,如 #FFFFFF → #FFF
    fn compress_color(&self, color: &str) -> Option<String> {
        if color.len() != 6 {
            return None;
        }

        let chars: Vec<char> = color.chars().collect();
        if chars[0] == chars[1] && chars[2] == chars[3] && chars[4] == chars[5] { Some(format!("{}{}{}", chars[0], chars[2], chars[4])) } else { None }
    }

    /// 移除不必要的分号
    ///
    /// # 参数
    /// * `css` - 压缩后的 CSS 代码
    ///
    /// # 返回值
    /// 移除不必要分号后的 CSS 代码
    fn remove_unnecessary_semicolons(&self, css: &str) -> String {
        let mut result = String::with_capacity(css.len());
        let mut chars = css.chars().peekable();

        while let Some(c) = chars.next() {
            result.push(c);
        }

        result
    }
}

/// 处理 CSS 代码的便捷函数
///
/// # 参数
/// * `css` - 要处理的 CSS 代码
///
/// # 返回值
/// 处理后的 CSS 代码
pub fn process(css: &str) -> Result<String> {
    let mut processor = StyleProcessor::new();
    processor.process(css, None)
}

/// 处理 CSS 代码并移除未使用的样式
///
/// # 参数
/// * `css` - 要处理的 CSS 代码
/// * `used_selectors` - 已使用的选择器列表
///
/// # 返回值
/// 处理后的 CSS 代码
pub fn process_with_used_selectors(css: &str, used_selectors: &Vec<String>) -> Result<String> {
    let mut processor = StyleProcessor::new().with_remove_unused(true);
    processor.process(css, Some(used_selectors))
}

/// 压缩 CSS 代码
///
/// # 参数
/// * `css` - 要压缩的 CSS 代码
///
/// # 返回值
/// 压缩后的 CSS 代码
pub fn minify(css: &str) -> Result<String> {
    let mut processor = StyleProcessor::new().with_minify(true);
    processor.process(css, None)
}

/// 优化 CSS 代码(移除未使用的样式并压缩)
///
/// # 参数
/// * `css` - 要优化的 CSS 代码
/// * `used_selectors` - 已使用的选择器列表
///
/// # 返回值
/// 优化后的 CSS 代码
pub fn optimize(css: &str, used_selectors: &Vec<String>) -> Result<String> {
    let mut processor = StyleProcessor::new().with_remove_unused(true).with_minify(true);
    processor.process(css, Some(used_selectors))
}

/// 使用预处理器处理 CSS 代码
///
/// # 参数
/// * `css` - 要处理的 CSS 代码
/// * `preprocessor` - CSS 预处理器类型
///
/// # 返回值
/// 处理后的 CSS 代码
pub fn process_with_preprocessor(css: &str, preprocessor: PreprocessorType) -> Result<String> {
    let mut processor = StyleProcessor::new().with_preprocessor(preprocessor);
    processor.process(css, None)
}

/// 监听文件变化并自动重新处理样式
///
/// # 参数
/// * `processor` - 样式处理器实例
/// * `file_path` - 要监听的文件路径
/// * `output_path` - 输出文件路径
/// * `callback` - 文件变化时的回调函数
///
/// # 返回值
/// 监听结果
pub fn watch(processor: &StyleProcessor, file_path: &str, output_path: &str, callback: Option<fn()>) -> Result<()> {
    // 复制处理器配置
    let minify = processor.minify;
    let remove_unused = processor.remove_unused;
    let preprocessor = processor.preprocessor.clone();
    let file_path_str = file_path.to_string();
    let file_path_closure = file_path_str.clone();
    let output_path = output_path.to_string();

    // 创建文件监视器
    match notify::recommended_watcher(move |res: std::result::Result<notify::Event, notify::Error>| {
        match res {
            Ok(event) => {
                if let notify::EventKind::Modify(_) = event.kind {
                    // 文件被修改,重新处理
                    if let Ok(css) = std::fs::read_to_string(&file_path_closure) {
                        let mut processor = StyleProcessor::new().with_minify(minify).with_remove_unused(remove_unused).with_preprocessor(preprocessor.clone().unwrap_or(PreprocessorType::Css));

                        if let Ok(processed) = processor.process(&css, None) {
                            if std::fs::write(&output_path, processed).is_ok() {
                                println!("Style updated: {}", output_path);
                                if let Some(cb) = callback {
                                    cb();
                                }
                            }
                        }
                    }
                }
            }
            Err(e) => println!("watch error: {:?}", e),
        }
    }) {
        Ok(mut watcher) => {
            // 开始监听文件
            if let Err(err) = watcher.watch(Path::new(&file_path_str), notify::RecursiveMode::NonRecursive) {
                return Err(Error::external_error("notify".to_string(), err.to_string(), Span::default()));
            }
            Ok(())
        }
        Err(err) => Err(Error::external_error("notify".to_string(), err.to_string(), Span::default())),
    }
}