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 Jul 9, 2019
0.1.1 Oct 30, 2018

#2216 in Procedural macros

Download history 4559/week @ 2024-07-19 3998/week @ 2024-07-26 3472/week @ 2024-08-02 3376/week @ 2024-08-09 3431/week @ 2024-08-16 3227/week @ 2024-08-23 3366/week @ 2024-08-30 3533/week @ 2024-09-06 2209/week @ 2024-09-13 3019/week @ 2024-09-20 2163/week @ 2024-09-27 1999/week @ 2024-10-04 3000/week @ 2024-10-11 2834/week @ 2024-10-18 2009/week @ 2024-10-25 3280/week @ 2024-11-01

11,623 downloads per month
Used in 8 crates (3 directly)

MIT/Apache

18KB
376 lines

Build Status

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