9 releases

0.1.7 Dec 5, 2021
0.1.6 Aug 30, 2020
0.1.5 Jul 11, 2020
0.1.4 Mar 4, 2020
0.0.1 Nov 28, 2019

#451 in Procedural macros

Download history 15349/week @ 2024-03-14 10122/week @ 2024-03-21 27243/week @ 2024-03-28 9468/week @ 2024-04-04 11412/week @ 2024-04-11 11531/week @ 2024-04-18 10399/week @ 2024-04-25 8260/week @ 2024-05-02 7873/week @ 2024-05-09 16239/week @ 2024-05-16 14578/week @ 2024-05-23 7638/week @ 2024-05-30 13767/week @ 2024-06-06 9886/week @ 2024-06-13 9915/week @ 2024-06-20 11016/week @ 2024-06-27

46,352 downloads per month
Used in 71 crates (10 directly)

MIT license

14KB
237 lines

bae

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:

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 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

~2MB
~44K SLoC