Skip to content

Instantly share code, notes, and snippets.

@solidsnack
Last active August 28, 2016 05:17
Show Gist options
  • Save solidsnack/ecc99725ce0fa6db2087a0b328760e20 to your computer and use it in GitHub Desktop.
Save solidsnack/ecc99725ce0fa6db2087a0b328760e20 to your computer and use it in GitHub Desktop.
Aster for module generation
#[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";
}
#![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