wiggle-generate: paramaterize library on module path to runtime (#1574)

* wiggle-generate: paramaterize library on module path to runtime

This change makes no functional difference to users who only use the
wiggle crate.

Add a parameter to the `Names` constructor that determines the module
that runtime components (e.g. GuestPtr, GuestError etc) of wiggle come
from. For `wiggle` users this is just `quote!(wiggle)`, but other
libraries which consume wiggle-generate may wrap and re-export wiggle
under some other path, and not want their consumers to have to know
about the wiggle dependency, e.g. `quote!(my_crate::some_path::wiggle)`.

* wiggle-generate,macro: move more logic into macro

better for code reuse elsewhere
This commit is contained in:
Pat Hickey
2020-04-22 07:16:21 -07:00
committed by GitHub
parent 65ef26b989
commit 25cbd8b591
14 changed files with 118 additions and 93 deletions

View File

@@ -1,6 +1,7 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;
/// This macro expands to a set of `pub` Rust modules:
@@ -94,11 +95,15 @@ pub fn from_witx(args: TokenStream) -> TokenStream {
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var"),
);
#[cfg(feature = "wiggle_metadata")]
{
config.emit_metadata = true;
}
let doc = witx::load(&config.witx.paths).expect("loading witx");
TokenStream::from(wiggle_generate::generate(&doc, &config))
let names = wiggle_generate::Names::new(&config.ctx.name, quote!(wiggle));
let code = wiggle_generate::generate(&doc, &names);
let metadata = if cfg!(feature = "wiggle_metadata") {
wiggle_generate::generate_metadata(&doc, &names)
} else {
quote!()
};
TokenStream::from(quote! { #code #metadata })
}