Crates.io | syner |
lib.rs | syner |
version | 0.3.0 |
source | src |
created_at | 2023-02-03 02:44:32.076374 |
updated_at | 2023-02-03 21:14:48.190943 |
description | A procedural macro to generate a parser for attributes from a struct |
homepage | |
repository | https://github.com/28Smiles/syner |
max_upload_size | |
id | 775348 |
size | 18,233 |
Syner is a simple, fast and safe way to parse attributes from syn. It is designed to be used with the syn crate.
Definition of your attributes is done using a procedural macro. This allows you to define your attributes in a type safe way.
You create a struct that represents your attribute and then use the #[derive(Syner)]
macro to generate the parsing code.
#[derive(Syner)]
struct Test {
pub some: String,
pub maybe: Option<String>,
#[syner(default)]
pub is_default: bool,
pub inner: Inner,
pub inner_list: Vec<Inner>,
pub inner_bools: Vec<bool>,
pub inner_maybe: Option<Inner>,
}
#[derive(Syner, PartialEq, Debug)]
struct Inner {
pub some: String,
pub is_default: bool,
}
This will parse the following attribute:
#[test(
some = "hello",
inner(
some = "inner",
is_default = true
),
inner_list(
inner(
some = "inner_list0",
is_default = true
),
inner(
some = "inner_list1",
is_default = false
),
inner(
some = "inner_list2",
is_default = true
)
),
inner_bools(true, false, true)
)]
struct TestStruct {}
You can parse the attribute using the parse_attrs
function.
It takes an iterator of syn::Attribute
and returns a Result
with the parsed attribute.
let attrs = Test::parse_attrs(&item.attrs)?;
Syner supports the following types:
String
- Parses the value as a stringbool
- Parses the value as a booleani8
, i16
, i32
, i64
, i128
, isize
- Parses the value as a signed integeru8
, u16
, u32
, u64
, u128
, usize
- Parses the value as an unsigned integerf32
, f64
- Parses the value as a floatT
- Parses the value as <name>(T)
if T
is a struct that implements Syner
Option<T>
- Parses the value as T
if it is presentVec<T>
- Parses the value as <name>(T...)
Annotating a field with #[syner(default)]
will make it optional and use the default value if it is not present.
You can also use #[syner(default = "<expr>")]
to specify a default value.
The name of the field is used as the name of the attribute except if the field is of type Vec<T>
,
in which case the name of the struct (lowercase
) is used.
For the top level attribute the lowercase name of the struct is used as a default name.
If you want to use a different name you can annotate the struct with #[syner(name = "<name>")]
.
This project is licensed under the MIT License - see the LICENSE file for details