Crates.io | codgenhelp |
lib.rs | codgenhelp |
version | 0.0.1 |
source | src |
created_at | 2017-01-02 22:42:47.44191 |
updated_at | 2017-01-02 22:42:47.44191 |
description | Simplifies structure and enum parsing in Macros1.1 |
homepage | https://github.com/valarauca/macrohelper |
repository | https://github.com/valarauca/macrohelper |
max_upload_size | |
id | 7894 |
size | 2,264,717 |
Simplifies the interaction with the syn
parser. Normally I find when working
with codegen extensions a lot of extra information is supplied to the developer
a certain amount of this is noise.
This is my attempt to filter out the noise. As well as provide useful tools to allow for Rust Dev's to interact with partially compiled Rust Code.
.
To use this crate:
depedencies
codgenhelp = "0.0.1"
Below you can find some examples:
Both MacroInput::from_str
and MacroInput::new(TokenTree)
are exposed
to construct the MacroInput
type
####Simple Enum Example
//the example string
let dut = r#"
#[DoTheThing = 15 ]
pub enum ValEnum {
Foo,
Bar
}
"#;
//attempt to parse
let dut = MacroInput::from_str(dut).unwrap();
//check the attr
assert!( dut.attr.len() == 1);
assert_eq!( dut.attr[0].get_name().unwrap(), "DoTheThing");
assert_eq!( dut.attr[0].from_literal().unwrap(), FromLiteral::Int(15));
//check the id
assert_eq!( &dut.id, "ValEnum");
//check the body
assert!( dut.body.is_enum() );
let body = dut.body.get_enum().unwrap();
assert_eq!(body.len(), 2);
assert!( body[0].data.is_unit());
assert_eq!( body[0].get_name(), "Foo");
assert!( body[1].data.is_unit());
assert_eq!( body[1].get_name(), "Bar");
assert!(body.is_unit());
####Simple Struct Example
//the example string
let dut = r"
#[get]
pub struct Thing {
a: i64
}
";
//attempt to parse
let dut = MacroInput::from_str(dut).unwrap();
//check the attr
assert!( dut.attr.len() == 1);
assert!( dut.attr[0].is_word());
assert_eq!( dut.attr[0].get_name().unwrap(), "get");
//check the id
assert_eq!( dut.id, "Thing");
//check the body
assert!( dut.body.is_struct() );
let body = dut.body.get_struct().unwrap();
assert!( body.is_norm());
let fields = body.get_fields().unwrap();
assert_eq!( fields.len(), 1 );
assert_eq!( fields[0].get_name().unwrap(), "a");