Crates.io | yui_internal |
lib.rs | yui_internal |
version | 0.1.5 |
source | src |
created_at | 2020-06-15 12:41:15.003986 |
updated_at | 2020-07-10 11:38:41.561257 |
description | Internal functions, struct for derive in Yui |
homepage | |
repository | https://github.com/dark-flames/Yui/tree/master/yui_internal |
max_upload_size | |
id | 254219 |
size | 24,832 |
Yui is an attribute reader for Rust.
Yui provides a derive macro YuiAttribute
to create attribute structure by struct, StructStruct
, TupleStruct
and NoFieldStruct
are all supported.
use yui::YuiAttribute;
#[derive(YuiAttribute)]
struct NoField;
#[derive(YuiAttribute)]
struct Tuple(i32, String);
#[derive(YuiAttribute)]
struct Struct {
int: i32,
float: f64,
bool: bool,
}
String
in Rust.bool
in Rust.enum_value=true
option.String
Skey.
If you want to make a field optional, use Option<T>
on the field type.use yui::{YuiEnumValue, YuiAttribute};
#[derive(YuiAttribute)]
struct Bar;
#[derive(YuiEnumValue)]
enum SomeEnum {
A,
B
}
#[derive(YuiAttribute)]
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
#[attribute_field(enum_value=true)]
pub enum_field: SomeEnum, // have to add enum_value option
pub list: Vec<i32>, // nested type of vec can`t be Object, Vec or HashMap
pub map: std::collections::HashMap<String, SomeEnum>,
pub optional: Option<i32> // optional field
}
alias
#[derive(YuiAttribute)]
struct Foo {
#[attribute_field(alias = "i32")]
pub int32: i32,
}
default
Object
, Vec
or HashMap
fields can`t have default value.
#[derive(YuiAttribute)]
struct Foo {
#[attribute_field(default = 1024)]
pub int32: i32
}
enum_value
enum_value=true
on Enum type field.Use derive YuiEnumValue
on Enum to create a Enum value type.
use yui::YuiEnumValue;
#[derive(YuiEnumValue)]
enum SomeEnum {
A,
B
}
And then, the enum can be used as a field type.
variant_value
attributeuse yui::YuiEnumValue;
#[derive(YuiEnumValue)]
enum SomeEnum {
#[variant_value("aaa")] // default is 'a'
A,
B
}
syn
andquote
yui::AttributeStructs<T>
can be used in parse_macro_input!
let attributes = syn::parse_macro_inpit!(input as yui::AttributeStructs<Foo>);
If you want to parse attribute from syn::Meta
, use yui::AttributeStruct::from_meta()
.
And attribute structure with value can be convert to token automatically. But the visibility of each field must be public.
use proc_macro::TokenStream;
#[derive(YuiAttribute)]
struct Foo {
#[attribute_field(default = 1024)]
pub int32: i32
}
fn derive_fn(input: TokenStream) -> TokenStream {
let attributes = syn::parse_macro_input!(input as yui::AttributeStructs<Foo>);
let attrs = attributes.attrs;
TokenStream::from(quote::quote! {
fn get_attrs() -> Vec<Foo> {
vec![#(#attrs),*]
}
})
}
If you want to use builtin reader generator, enable generate-reader
feature.
Macro generate_reader
is used to generate a derive macro.
use yui::generate_reader;
generated_reader!(
MyDerive,
[StructAttribute1, StructAttribute2],
[FieldAttribute1, FieldAttribute2]
);
The macro will generate a public derive, it can be use to read attributes of struct
,enum
or union
, and record the metadata by generate impl
block.
Use the generated derive macro on a struct, and you can use the macro has_attribute
and get_attribute
to process attributes of the struct.
The feature require nightly rustc because proc_macro_hygiene
is required.
#![feature(proc_macro_hygiene)]
use yui::{get_attribute, has_attribute};
#[derive(MyDerive)]
#[StructAttribute1("some parameters")]
struct Foo {
#[FieldAttribute1("some parameters")]
field: i32
}
fn some_fn() {
assert!(has_attribute!(Foo, StructAttribute1));
assert!(has_attribute!(Foo::field, FieldAttribute1));
let struct_attr1: Option<StructAttribute1> = get_attribute!(Foo, StructAttribute1);
let field_attr1: Option<StructAttribute1> = get_attribute!(Foo::field, StructAttribute1);
}