annotation-rs
Compile-time annotation parser for rust
Features
Annotation
annotation-rs provides a derive macro Annotation
to create annotation structure by struct, StructStruct
, TupleStruct
and NoFieldStruct
are all supported.
use Annotation;
;
;
Types
- String:
String
in Rust. - Bool:
bool
in Rust. - Integer: any integer types in Rust.
- Float: any float types in Rust.
- Object: other annotation structure.
- Enum: defined enum, remember to use
enum_value=true
option. - Vec: Vec of T(T can`t be Object, Vec or HashMap).
- HashMap<String, T>: HashMap of T mapping by
String
Skey. If you want to make a field optional, useOption<T>
on the field type.
use ;
;
Options
alias
Generated reader will parse the field with the given name instead of its field name in Rust.default
Set the default value for this field. If the value is not present when parsing, the default value will be set to the field, even the field is optional.Object
,Vec
orHashMap
fields can`t have default value.enum_value
useenum_value=true
on Enum type field.
Enum
Use derive AnnotationEnumValue
on Enum to create a Enum value type.
use AnnotationEnumValue;
And then, the enum can be used as a field type.
variant_value
attribute
Customize a string corresponding value to variant(default is the snake case of variant name in Rust).
use AnnotationEnumValue;
Parse annotations with syn
andquote
annotation_rs::AnnotationStructures<T>
can be used in parse_macro_input!
let annotations = parse_macro_inpit!;
If you want to parse annotation from syn::Meta
, use annotation_rs::AnnotationStructure::from_meta()
.
And annotation structure with value can be convert to token automatically. But the visibility of each field must be public.
use TokenStream;
Generate derive macro
If you want to use builtin reader generator, enable annotation_reader
feature.
Macro generate_reader
is used to generate a derive macro.
use generate_reader;
generated_reader!;
The macro will generate a public derive, it can be use to read annotations of struct
,enum
or union
, and record the metadata by generate impl
block.
Read annotations
Use the generated derive macro on a struct, and you can use the macro has_annotation
and get_annotation
to process annotations of the struct.
The feature require nightly rustc because proc_macro_hygiene
is required.
use ;