6 releases (3 breaking)
0.4.2 | Jan 22, 2020 |
---|---|
0.4.1 | Dec 26, 2019 |
0.3.0 | Jul 9, 2019 |
0.2.1 |
|
0.1.1 | Oct 30, 2018 |
#2216 in Procedural macros
11,623 downloads per month
Used in 8 crates
(3 directly)
18KB
376 lines
Helper APIs for Rust procedural macros
This crates provides helper APIs for procedural macros.
Please read the API documentation on docs.rs
APIs to manipulate with attributes
use proc_macro2::Span;
use syn::LitStr;
fn lit_str(s: &str) -> Lit {
Lit::Str(LitStr::new(s, Span::call_site()))
}
fn test_contains_attribute_impl() {
let attr: Attribute = parse_quote!(#[level0(level1, level1_1(level2, level2_1 = "hello"))]);
let attr = [attr];
assert!(!contains_attribute(&attr, &[]));
assert!(!contains_attribute(&attr, &["not"]));
assert!(!contains_attribute(&attr, &["level0"]));
assert!(contains_attribute(&attr, &["level0", "level1"]));
assert!(!contains_attribute(&attr, &["level0", "level1_1"]),);
assert!(contains_attribute(&attr, &["level0", "level1_1", "level2"]),);
assert!(!contains_attribute(
&attr,
&["level0", "level1_1", "level2_1"]
),);
}
#[test]
fn test_get_attribute_value() {
let attr: Attribute = parse_quote!(#[level0(level1 = "hi", level1_1(level2 = "bye"))]);
let attr = [attr];
assert_eq!(get_attribute_value(&attr, &[""]), None);
assert_eq!(get_attribute_value(&attr, &["not"]), None);
assert_eq!(get_attribute_value(&attr, &["level0"]), None);
assert_eq!(
get_attribute_value(&attr, &["level0", "level1"]),
Some(lit_str("hi"))
);
assert_eq!(get_attribute_value(&attr, &["level0", "level1_1"]), None);
assert_eq!(
get_attribute_value(&attr, &["level0", "level1_1", "level2"]),
Some(lit_str("bye"))
);
}
#[test]
fn test_get_attribute_map() {
assert_eq!(
get_attribute_map(
&[
parse_quote!(#[level9]),
parse_quote!(#[level0(level8)]),
parse_quote!(#[level0(level1 = "hi", level1_1(level2 = "bye"))]),
parse_quote!(#[level0(level1 = "hi", level1_1(level2 = "bye"))]),
parse_quote!(#[gen0(gen1 = "amoeba", gen1_1 = "monad", gen1_2(gen2 = "monoid"))])
],
"."
),
vec![
("level9".to_string(), vec![]),
("level0.level8".to_string(), vec![]),
(
"level0.level1".to_string(),
vec![lit_str("hi"), lit_str("hi")],
),
(
"level0.level1_1.level2".to_string(),
vec![lit_str("bye"), lit_str("bye")],
),
("gen0.gen1".to_string(), vec![lit_str("amoeba")]),
("gen0.gen1_1".to_string(), vec![lit_str("monad")]),
("gen0.gen1_2.gen2".to_string(), vec![lit_str("monoid")]),
]
.into_iter()
.collect()
);
}
Dependencies
~1.5MB
~37K SLoC