TedDriggs / darling

A Rust proc-macro attribute parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: using darling internal tools to build darling-style macros

smmoosavi opened this issue · comments

Hi, thanks for the great package.

If I want to create darling-style macros, for example, FromItemImpl, FromImplItemMethod, etc can I use darling internal tools? If yes, can you help me with which tools will help me? which trait should I implement.

#[derive(FromImplItemMethod)
darling(attributes(foo))
struct MyMethod {
  // macro-defined items
  ident: Ident,
  attrs: Vec<syn::Attribute>,
  args: Args<MyArgs>, // similar to darling::Data
  returns: Option<Type>,

  // user-defined items
  skip: bool,
  name: Option<String> 
}

I thought about doing this, but have landed on recommending the use of FromAttributes instead. There are now too many things that can take attributes, and as a result the number of trait impls that darling increases to an unsustainable level. Using FromAttributes, optionally in conjunction with the WithOriginal struct, provides a good way to take advantage of darling's error handling, parsing, and support for multiple attributes without needing the crate's API surface to increase in size by an order of magnitude.

My question is not about adding FromItemImpl to darling but about creating it in my crate. can I use darling internal tools?

Internal tools are crate-internal, so you cannot use them. You’re free to inspect the code in options and codegen to see how the internals work, but you won’t be able to reuse those specific pieces directly in your project.

That said, it’s probably easier to create the macro you want using darling than the darling-internal impls, since I had to hand-write a lot of what darling generates.