sir_macro/
lib.rs

1mod compile_style_item;
2
3use crate::compile_style_item::{inner_compile_global_style, inner_compile_style_item};
4use proc_macro::TokenStream;
5use rand::prelude::SliceRandom;
6use rand::{thread_rng, Rng};
7use std::iter;
8
9#[proc_macro]
10pub fn compile_style_item(item: TokenStream) -> TokenStream {
11    inner_compile_style_item(generate_class_name(&mut thread_rng()), item.into()).into()
12}
13
14#[proc_macro]
15pub fn compile_global_style(item: TokenStream) -> TokenStream {
16    inner_compile_global_style(item.into()).into()
17}
18
19const ID_LENGTH: usize = 15;
20const ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz0123456789";
21
22fn generate_class_name(mut rng: &mut impl Rng) -> String {
23    let chars: Vec<_> = ALPHABET.chars().collect();
24
25    let class = iter::once(&'_')
26        .chain((0..ID_LENGTH).map(|_| chars.choose(&mut rng).unwrap()))
27        .collect();
28
29    class
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use rand::prelude::StdRng;
36    use rand::SeedableRng;
37
38    #[test]
39    fn test_generate_class_name() {
40        let mut rng = StdRng::seed_from_u64(42);
41        // snapshot
42        assert_eq!(generate_class_name(&mut rng), "_t5bwm0ra7vs5of3");
43    }
44}