Skip to content

Commit 676c191

Browse files
committed
Make macros accept idents where atom is a valid atom
This means that local_name!(html) will work as well as local_name!("html") Signed-off-by: Nico Burns <[email protected]>
1 parent eb5ad11 commit 676c191

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

string-cache-codegen/lib.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
6969
#![recursion_limit = "128"]
7070

71+
use proc_macro2::{Ident, Literal, TokenStream};
7172
use quote::quote;
7273
use std::collections::BTreeSet;
7374
use std::fs::File;
@@ -185,8 +186,7 @@ impl AtomType {
185186
/// Write generated code to destination [`Vec<u8>`] and return it as [`String`]
186187
///
187188
/// Used mostly for testing or displaying a value.
188-
pub fn write_to_string(&mut self, mut destination: Vec<u8>) -> io::Result<String>
189-
{
189+
pub fn write_to_string(&mut self, mut destination: Vec<u8>) -> io::Result<String> {
190190
destination.write_all(
191191
self.to_tokens()
192192
.to_string()
@@ -223,6 +223,27 @@ impl AtomType {
223223
let empty_string_index = atoms.iter().position(|s| s.is_empty()).unwrap() as u32;
224224
let indices = 0..atoms.len() as u32;
225225

226+
let atom_idents: Vec<Ident> = atoms
227+
.iter()
228+
.filter(|atom| {
229+
atom.chars()
230+
.next()
231+
.is_some_and(|c| c.is_alphabetic() || c == '_')
232+
})
233+
.filter(|atom| atom.chars().all(|c| c.is_alphanumeric() || c == '_'))
234+
.map(|atom| new_term(atom))
235+
.collect();
236+
let inline_str_idents: Vec<Ident> = inline_strs
237+
.iter()
238+
.filter(|atom| {
239+
atom.chars()
240+
.next()
241+
.is_some_and(|c| c.is_alphabetic() || c == '_')
242+
})
243+
.filter(|s| s.chars().all(|c| c.is_alphanumeric() || c == '_'))
244+
.map(|s| new_term(s))
245+
.collect();
246+
226247
let hashes: Vec<u32> = atoms
227248
.iter()
228249
.map(|string| {
@@ -249,8 +270,9 @@ impl AtomType {
249270
Some(ref doc) => quote!(#[doc = #doc]),
250271
None => quote!(),
251272
};
252-
let new_term =
253-
|string: &str| proc_macro2::Ident::new(string, proc_macro2::Span::call_site());
273+
fn new_term(string: &str) -> Ident {
274+
Ident::new(string, proc_macro2::Span::call_site())
275+
}
254276
let static_set_name = new_term(&format!("{}StaticSet", type_name));
255277
let type_name = new_term(type_name);
256278
let macro_name = new_term(&*self.macro_name);
@@ -323,6 +345,12 @@ impl AtomType {
323345
#(
324346
(#inline_strs) => { #module::#inline_const_names };
325347
)*
348+
#(
349+
(#atom_idents) => { #module::#const_names };
350+
)*
351+
#(
352+
(#inline_str_idents) => { #module::#inline_const_names };
353+
)*
326354
}
327355
}
328356
}
@@ -340,11 +368,13 @@ impl AtomType {
340368
fn test_iteration_order() {
341369
let x1 = crate::AtomType::new("foo::Atom", "foo_atom!")
342370
.atoms(&["x", "xlink", "svg", "test"])
343-
.write_to_string(Vec::new()).expect("write to string cache x1");
371+
.write_to_string(Vec::new())
372+
.expect("write to string cache x1");
344373

345374
let x2 = crate::AtomType::new("foo::Atom", "foo_atom!")
346375
.atoms(&["x", "xlink", "svg", "test"])
347-
.write_to_string(Vec::new()).expect("write to string cache x2");
376+
.write_to_string(Vec::new())
377+
.expect("write to string cache x2");
348378

349379
assert_eq!(x1, x2);
350380
}

0 commit comments

Comments
 (0)