Expand description

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;

Modules

library error types

internal parsing of attributes

Macros

parse an array of attributes

Structs

map for parsing an array of attribute nodes

an attribute node

Enums

data of an attribute node

Traits

parse a token input into a given struct

a defined structure of attributes

parse a value from a single attribute node

Derive Macros

attribute for creating attribute structures