Compile-time annotation parser for rust
annotation-rs provides a derive macro Annotation to create annotation structure by struct, StructStruct, TupleStruct and NoFieldStruct are all supported.
```rust
use annotation_rs::Annotation;
struct NoField;
struct Tuple(i32, String);
struct Struct { int: i32, float: f64, bool: bool, } ```
String in Rust.bool in Rust.enum_value=trueoption.String Skey.
If you want to make a field optional, use Option<T> on the field type.```rust use annotation_rs::{AnnotationEnumValue, Annotation};
struct Bar;
enum SomeEnum { A, B }
struct Foo {
pub string: String,
pub bool: bool,
pub int: i32, // or other integer types like u32 ...
pub float: f32, // or other float types like f64
pub object: Bar, // any defined object
#[field(enumvalue=true)]
pub enumfield: SomeEnum, // have to add enum_value option
pub list: Vec
alias\
Generated reader will parse the field with the given name instead of its field name in Rust.
rust
#[derive(Annotation)]
struct Foo {
#[field(alias = "i32")]
pub int32: i32,
}
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 or HashMap fields can`t have default value.
rust
#[derive(Annotation)]
struct Foo {
#[field(default = 1024)]
pub int32: i32
}
enum_value\
use enum_value=true on Enum type field.Use derive AnnotationEnumValue on Enum to create a Enum value type.
```rust
use annotation_rs::AnnotationEnumValue;
enum SomeEnum {
A,
B
}
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).
rust
use annotation_rs::AnnotationEnumValue;
enum SomeEnum { #[variant_value("aaa")] // default is 'a' A, B } ```
synandquoteannotation_rs::AnnotationStructures<T> can be used in parse_macro_input!
rust
let annotations = syn::parse_macro_inpit!(input as annotation_rs::AnnotationStructures<Foo>);
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.
```rust
use proc_macro::TokenStream;
struct Foo { #[field(default = 1024)] pub int32: i32 }
fn derivefn(input: TokenStream) -> TokenStream {
let annotations = syn::parsemacroinput!(input as annotationrs::AnnotationStructures
TokenStream::from(quote::quote! {
fn get_attrs() -> Vec<Foo> {
vec![#(#attrs),*]
}
})
} ```
If you want to use builtin reader generator, enable annotation_reader feature.
Macro generate_reader is used to generate a derive macro.
```rust
use annotationrs::generatereader;
generated_reader!( MyDerive, [StructAttribute1, StructAttribute2], [FieldAttribute1, FieldAttribute2] );
``
The macro will generate a public derive, it can be use to read annotations ofstruct,enumorunion, and record the metadata by generateimpl` block.
Use the generated derive macro on a struct, and you can use the macro has_annotation and get_annotationto process annotations of the struct.
The feature require nightly rustc because proc_macro_hygiene is required.
```rust
use annotationrs::{getannotation, has_annotation};
struct Foo { #[FieldAttribute1("some parameters")] field: i32 }
fn somefn() {
assert!(hasannotation!(Foo, StructAttribute1));
assert!(hasannotation!(Foo::field, FieldAttribute1));
let structattr1: Option