#proc-macro-attributes #parser #syn #default

kefta

proc-macro for simplified attribute parsing

1 unstable release

0.0.1 Aug 15, 2022

#67 in #proc-macro-attributes

GPL-3.0-or-later

49KB
1K SLoC

Kefta

a simple attribute parser

Key Features

  • derive macros for easily parsing structures
  • built-in "decent" error messages
  • build-in attribute parsing
  • optional syn support

Feature Toggles

  • literal - literal parsing
  • util - pre-made types and utility traits
  • syn - syn support via Syn<impl syn::Parse>

Examples

use kefta::{Attr, parse_attr};

// derive `Attr` onto your struct
#[derive(Attr)]
struct MyAttrs {
    // a marker field,
    alive: bool,

    // an optional field
    #[attr(optional)]
    name: Option<String>,

    // a required field
    #[attr(required)]
    value: i32,

    // a default field (defaults to 0)
    count: u32,

    // a renamed field
    #[attr(optional, name="desc")]
    description: Option<String>,

    // a field with an alias
    #[attr(alias="color")]
    colour: Option<String>,

    // a field with multiple values
    #[attr(multiple)]
    jobs: Vec<String>,

    // an optional, aliased field, with multiple values
    #[attr(multiple, optional, alias="chores")]
    tasks: Option<Vec<String>>,
    /* you get the point */
}

// parse in your derive-macro
// * this uses the syn crate and `syn` feature
#[proc_macro_derive(Human, attributes(human))]
pub fn test_macro(item: TokenStream) -> TokenStream {
    // parse with `syn`
    let input = syn::parse_macro_input!(item as DeriveInput);

    // parse the attributes with the `parse_attr!` macro
    // it contains a `return TokenStream`, so you don't have to handle errors.
    let attrs = parse_attr!(input.attrs => MyAttrs);

    // print out one of our fields
    println!("Name:  {:?}", attrs.name);

    TokenStream::new()
}

You can use attributes like so

#[derive(Human)]
#[human(name="Jimmy", value=10, alive)]
#[human(jobs="foo", jobs="bar", jobs="baz")]
pub struct Jimmy;

Dependencies

~1.5MB
~37K SLoC