2 releases

0.2.1 Sep 25, 2024
0.2.0 Jul 10, 2023

#235 in Procedural macros

Download history 116520/week @ 2024-11-30 211331/week @ 2024-12-07 166113/week @ 2024-12-14 41443/week @ 2024-12-21 77915/week @ 2024-12-28 175910/week @ 2025-01-04 161382/week @ 2025-01-11 133352/week @ 2025-01-18 159958/week @ 2025-01-25 170421/week @ 2025-02-01 184317/week @ 2025-02-08 190647/week @ 2025-02-15 235698/week @ 2025-02-22 228143/week @ 2025-03-01 234063/week @ 2025-03-08 183671/week @ 2025-03-15

924,668 downloads per month
Used in 81 crates (4 directly)

MIT license

14KB
231 lines

sea-bae

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

This fork upgrades syn to 2.0 and heck to 0.4, mainly for use in sea-orm.


lib.rs:

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

~290–730KB
~17K SLoC