tailwindcss_atomic/
stats.rs1use std::collections::HashMap;
2
3use crate::classes::rewrite_class_string;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct AtomicStats {
8 pub input_bytes: usize,
9 pub output_bytes: usize,
10 pub utilities: usize,
12 pub atomic_rules: usize,
14 pub declarations: usize,
16 pub shared_hashes: usize,
18 pub elapsed_us: u64,
19 pub changed: bool,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct ClassStringStats {
25 pub before_bytes: usize,
26 pub after_bytes: usize,
27}
28
29pub fn compute_atomic_stats(
30 input: &str,
31 class_map: &HashMap<String, String>,
32 output_css: &str,
33 changed: bool,
34 elapsed_us: u64,
35) -> AtomicStats {
36 let mut hash_uses: HashMap<&str, usize> = HashMap::new();
37 let mut declarations = 0usize;
38 for hashes in class_map.values() {
39 for hash in hashes.split_whitespace() {
40 if hash.is_empty() {
41 continue;
42 }
43 declarations += 1;
44 *hash_uses.entry(hash).or_insert(0) += 1;
45 }
46 }
47
48 AtomicStats {
49 input_bytes: input.len(),
50 output_bytes: output_css.len(),
51 utilities: class_map.len(),
52 atomic_rules: hash_uses.len(),
53 declarations,
54 shared_hashes: hash_uses.values().filter(|count| **count > 1).count(),
55 elapsed_us,
56 changed,
57 }
58}
59
60pub fn class_string_stats(
61 class_str: &str,
62 class_map: &HashMap<String, String>,
63) -> ClassStringStats {
64 let after = rewrite_class_string(class_str, class_map);
65 ClassStringStats {
66 before_bytes: class_str.len(),
67 after_bytes: after.len(),
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74 use crate::atomicize_stylesheet;
75
76 #[test]
77 fn measures_input_output_and_utility_counts() {
78 let css = ".flex { display: flex } .p-4 { padding: 1rem }";
79 let out = atomicize_stylesheet(css).unwrap();
80 assert!(out.stats.changed);
81 assert_eq!(out.stats.input_bytes, css.len());
82 assert!(out.stats.output_bytes > 0);
83 assert_eq!(out.stats.utilities, 2);
84 assert_eq!(out.stats.atomic_rules, 2);
85 assert_eq!(out.stats.declarations, 2);
86 assert_eq!(out.stats.shared_hashes, 0);
87 }
88
89 #[test]
90 fn splits_multi_declaration_utilities() {
91 let css = ".p-4 { padding: 1rem; margin: 0 }";
92 let out = atomicize_stylesheet(css).unwrap();
93 assert_eq!(out.stats.utilities, 1);
94 assert_eq!(out.stats.declarations, 2);
95 assert_eq!(out.stats.atomic_rules, 2);
96 assert_eq!(out.stats.shared_hashes, 0);
97 }
98
99 #[test]
100 fn hashes_include_the_utility_name() {
101 let css = ".flex { display: flex } .inline-flex { display: flex }";
102 let out = atomicize_stylesheet(css).unwrap();
103 assert_eq!(out.stats.utilities, 2);
104 assert_eq!(out.stats.atomic_rules, 2);
105 assert_eq!(out.stats.shared_hashes, 0);
106 assert_ne!(out.class_map["flex"], out.class_map["inline-flex"]);
107 }
108
109 #[test]
110 fn class_string_stats_tracks_rewrite_length() {
111 let mut map = HashMap::new();
112 map.insert("flex".into(), "_aaaaaa".into());
113 let stats = class_string_stats("flex", &map);
114 assert_eq!(stats.before_bytes, 4);
115 assert_eq!(stats.after_bytes, 7);
116 }
117}