1 unstable release
0.0.1 | Aug 15, 2022 |
---|
#24 in #attr
Used in 2 crates
43KB
1K
SLoC
Kefta
simplified attribute parsing w/ proc-macros
Key Features
- derive macros for easily parsing structures
- built-in "decent" error messages
- build-in attribute parsing
- optional
syn
support
Feature Toggles
literal
- literal parsingutil
- pre-made types and utility traitssyn
- syn support viaSyn<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
~12–465KB
~11K SLoC