Last active
August 28, 2016 05:17
-
-
Save solidsnack/ecc99725ce0fa6db2087a0b328760e20 to your computer and use it in GitHub Desktop.
Aster for module generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(dead_code, non_upper_case_globals)] | |
pub mod code { | |
const x: &'static str = "X string"; | |
const y: &'static str = "Y string"; | |
const z: &'static str = "another string"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(rustc_private)] | |
extern crate aster; | |
extern crate syntax; | |
fn main() { | |
let strings = | |
vec![("x", "X string"), ("y", "Y string"), ("z", "another string")]; | |
let items: Vec<_> = strings.into_iter() | |
.map(|(name, data)| { | |
let builder = aster::AstBuilder::new(); | |
builder.item() | |
.const_(name) | |
.expr() | |
.str(data) | |
.ty() | |
.ref_() | |
.lifetime("'static") | |
.ty() | |
.id("str") | |
}) | |
.collect(); | |
let module = syntax::ast::Mod { | |
inner: syntax::codemap::DUMMY_SP, | |
items: items, | |
}; | |
let builder = aster::AstBuilder::new(); | |
let item = builder.item() | |
.pub_() | |
.attr() | |
.allow(&["dead_code", "non_upper_case_globals"]) | |
.build_item_kind("code", syntax::ast::ItemKind::Mod(module)); | |
println!("{}", syntax::print::pprust::item_to_string(&item)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment