GithubHelp home page GithubHelp logo

bae's Introduction

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.

bae's People

Contributors

davidpdrsn avatar farkal avatar urkle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bae's Issues

Parse attributes without prefix

I'm trying bae as an alternative to darling. With darling, I implemented prefix-less attributes for my library:

#[derive(Modal)] // My derive macro
#[name = "Modal title"]
struct MyModal {
    #[name = "First input label"]
    #[placeholder = "Your first input goes here"]
    #[min_length = 5]
    #[min_length = 500]
    first_input: String,
    #[name = "Second input label"]
    #[paragraph]
    second_input: Option<String>,
}

However, bae seems to require all attributes to be enclosed in a common identifier (the FromAttributes-derived struct name):

#[derive(bae::FromAttributes)]
struct Poise { /* ... */ }

#[poise(name = "Modal title")]
struct MyModal {
    #[poise(name = "First input label")]
    #[poise(placeholder = "Your first input goes here")]
    #[poise(min_length = 5)]
    #[poise(min_length = 500)]
    first_input: String,
    #[poise(name = "Second input label")]
    #[poise(paragraph)]
    second_input: Option<String>,
}

Implemented here:

bae/src/lib.rs

Line 167 in 3b018d1

Some(ident) if ident == #attr_name => {

Would a PR be accepted to add support for the first snippet's syntax? For example with a #[no_prefix] attribute on the FromAttributes-derived struct

Parse multiple attributes

How to parse multiple attributes into a single struct? For example

// Library proc macro code
#[derive(bae::FromAttributes)]
struct Poise {
    name: Option<syn::LitStr>,
    placeholder: Option<syn::LitStr>,
    min_length: Option<syn::LitInt>,
    max_length: Option<syn::LitInt>,
}

// User code
struct MyModal {
    #[poise(name = "First input label")]
    #[poise(placeholder = "Your first input goes here")]
    #[poise(min_length = 5)]
    #[poise(min_length = 500)]
    first_input: String,
}

When parsing the field attributes with Poise::from_attributes(&field.attrs), only the first attributes is parsed:

Poise { name: Some(LitStr { token: "First input label" }), placeholder: None, min_length: None, max_length: None, paragraph: None }

I assume this is because the parser code immediately returns upon encountering the first valid attribute:

bae/src/lib.rs

Lines 165 to 173 in 3b018d1

for attr in attrs {
match attr.path.get_ident() {
Some(ident) if ident == #attr_name => {
return Some(syn::parse2::<Self>(attr.tokens.clone())).transpose()
}
// Ignore other attributes
_ => {},
}
}

Would a PR for parsing all valid attributes be accepted?

Support for doc comments

Hey, I can't find any support for doc comments is that intentional or something that you think is reasonable to support?

Multiple FromAttributes structs with same name?

Use case:

#[derive(MyDeriveMacro)]
#[my_derive_macro(/* struct attributes */)]
struct Foo {
    #[my_derive_macro(/* field attributes */)]
    bar: syn::LitIdent,
}

The set of valid attributes on the struct vs on a field is different, but the name my_derive_macro should stay the same. How to achieve this with bae? Is this the best way?:

mod struct_attrs {
    #[derive(bae::FromAttributes)]
    pub struct MyDeriveMacro { /* struct attributes */ }
}
use struct_attrs::MyDeriveMacro as StructAttrs;

mod field_attrs {
    #[derive(bae::FromAttributes)]
    pub struct MyDeriveMacro { /* field attributes */ }
}
use field_attrs::MyDeriveMacro as FieldAttrs;

A #[bae(name = "...")]/#[bae(rename = "...")]/#[bae(prefix = "...")] attribute on FromAttributes-derived structs could solve this nicely:

#[derive(bae::FromAttributes)]
#[bae(name = "my_derive_macro")]
pub struct StructAttrs { /* struct attributes */ }

#[derive(bae::FromAttributes)]
#[bae(name = "my_derive_macro")]
pub struct FieldAttrs { /* field attributes */ }

Would such a PR be accepted?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.