use proc_macro2::TokenStream;
use quote::quote;
use std::collections::HashMap;
use syn::Attribute;
pub struct Scope {
locals: HashMap<String, String>,
exports: Vec<Export>,
}
struct Export {
attrs: Vec<Attribute>,
name: String,
value: String,
}
impl Scope {
pub fn new() -> Self {
Scope {
locals: HashMap::new(),
exports: Vec::new(),
}
}
pub fn add_local(&mut self, name: String, value: String) {
self.locals.insert(name, value);
}
pub fn get_local(&self, name: &str) -> Option<&String> {
self.locals.get(name)
}
pub fn add_export(&mut self, attrs: Vec<Attribute>, name: String, value: String) {
self.exports.push(Export { attrs, name, value });
}
pub fn generate_output(&self) -> TokenStream {
let mut output = TokenStream::new();
for export in &self.exports {
let name = syn::Ident::new(&export.name, proc_macro2::Span::call_site());
let value = &export.value;
let attrs = &export.attrs;
output.extend(quote! {
#(#attrs)*
const #name: &str = #value;
});
}
output
}
}