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

#2294 in Procedural macros

Download history 3823/week @ 2024-08-18 3268/week @ 2024-08-25 3191/week @ 2024-09-01 3529/week @ 2024-09-08 1937/week @ 2024-09-15 3473/week @ 2024-09-22 1402/week @ 2024-09-29 2437/week @ 2024-10-06 3210/week @ 2024-10-13 2455/week @ 2024-10-20 2076/week @ 2024-10-27 3681/week @ 2024-11-03 2649/week @ 2024-11-10 2259/week @ 2024-11-17 1727/week @ 2024-11-24 2105/week @ 2024-12-01

8,964 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