3 releases

0.1.9 May 12, 2022
0.1.8 Oct 17, 2021
0.1.7 Oct 17, 2021

#1478 in Procedural macros

Download history 213/week @ 2023-12-15 163/week @ 2023-12-22 113/week @ 2023-12-29 160/week @ 2024-01-05 236/week @ 2024-01-12 188/week @ 2024-01-19 200/week @ 2024-01-26 174/week @ 2024-02-02 169/week @ 2024-02-09 280/week @ 2024-02-16 224/week @ 2024-02-23 295/week @ 2024-03-01 224/week @ 2024-03-08 165/week @ 2024-03-15 274/week @ 2024-03-22 274/week @ 2024-03-29

987 downloads per month
Used in 15 crates (4 directly)

MIT license

7KB

better-bae

This crate is a fork of bae

better-bae is a crate for proc macro authors, which simplifies parsing of attributes. It is heavily inspired by darling but has a significantly simpler API.

See the docs for more info.


lib.rs:

This crate is a fork of bae.

better-bae is a crate for proc macro authors, which simplifies parsing of attributes. It is heavily inspired by darling but has a significantly simpler API.

use better_bae::FromAttributes;

#[derive(
    Debug,
    Eq,
    PartialEq,

    // This will add two functions:
    // ```
    // fn from_attributes(attrs: &[syn::Attribute]) -> Result<MyAttr, syn::Error>
    // fn try_from_attributes(attrs: &[syn::Attribute]) -> Result<Option<MyAttr>, syn::Error>
    // ```
    //
    // `try_from_attributes` returns `Ok(None)` if the attribute is missing, `Ok(Some(_))` if
    // its there and is valid, `Err(_)` otherwise.
    FromAttributes,
)]
pub struct MyAttr {
    // Anything that implements `syn::parse::Parse` is supported.
    mandatory_type: syn::Type,
    mandatory_ident: syn::Ident,

    // Fields wrapped in `Option` are optional and default to `None` if
    // not specified in the attribute.
    optional_missing: Option<syn::Type>,
    optional_given: Option<syn::Type>,

    // A "switch" is something that doesn't take arguments.
    // All fields with type `Option<()>` are considered swiches.
    // They default to `None`.
    switch: Option<()>,
}

// `MyAttr` is now equipped to parse attributes named `my_attr`. For example:
//
//     #[my_attr(
//         switch,
//         mandatory_ident = foo,
//         mandatory_type = SomeType,
//         optional_given = OtherType,
//     )]
//     struct Foo {
//         ...
//     }

// the input and output type would normally be `proc_macro::TokenStream` but those
// types cannot be used outside the compiler itself.
fn my_proc_macro(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
    let item_struct = syn::parse2::<syn::ItemStruct>(input).unwrap();

    let my_attr = MyAttr::from_attributes(&item_struct.attrs).unwrap();

    assert_eq!(
        my_attr.mandatory_type,
        syn::parse_str::<syn::Type>("SomeType").unwrap()
    );

    assert_eq!(my_attr.optional_missing, None);

    assert_eq!(
        my_attr.optional_given,
        Some(syn::parse_str::<syn::Type>("OtherType").unwrap())
    );

    assert_eq!(my_attr.mandatory_ident, syn::parse_str::<syn::Ident>("foo").unwrap());

    assert_eq!(my_attr.switch.is_some(), true);

    // ...
    #
    # quote::quote! {}
}
#

Dependencies

~1.5MB
~34K SLoC