Wiggle: tweaks to internal interfaces (#1469)
* [wiggle]: wiggle-generate internal interface for Names simplified im using it in other libraries where I don't want to construct a wiggle_generate::Config just to use the ctx_type out of it. * wiggle: define_func can get trait name as argument this flexibility needed for some customization over in lucet
This commit is contained in:
@@ -5,7 +5,11 @@ use crate::lifetimes::anon_lifetime;
|
||||
use crate::module_trait::passed_by_reference;
|
||||
use crate::names::Names;
|
||||
|
||||
pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
|
||||
pub fn define_func(
|
||||
names: &Names,
|
||||
func: &witx::InterfaceFunc,
|
||||
trait_name: TokenStream,
|
||||
) -> TokenStream {
|
||||
let funcname = func.name.as_str();
|
||||
|
||||
let ident = names.func(&func.name);
|
||||
@@ -166,7 +170,7 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
|
||||
{
|
||||
log::trace!(#trace_fmt, #(#args),*);
|
||||
}
|
||||
let #trait_bindings = match ctx.#ident(#(#trait_args),*) {
|
||||
let #trait_bindings = match #trait_name::#ident(ctx, #(#trait_args),*) {
|
||||
Ok(#trait_bindings) => { #trait_rets },
|
||||
Err(e) => { #ret_err },
|
||||
};
|
||||
|
||||
@@ -15,13 +15,18 @@ pub use names::Names;
|
||||
pub use types::define_datatype;
|
||||
|
||||
pub fn generate(doc: &witx::Document, config: &Config) -> TokenStream {
|
||||
let names = Names::new(config); // TODO parse the names from the invocation of the macro, or from a file?
|
||||
// TODO at some point config should grow more ability to configure name
|
||||
// overrides.
|
||||
let names = Names::new(&config.ctx.name);
|
||||
|
||||
let types = doc.typenames().map(|t| define_datatype(&names, &t));
|
||||
|
||||
let modules = doc.modules().map(|module| {
|
||||
let modname = names.module(&module.name);
|
||||
let fs = module.funcs().map(|f| define_func(&names, &f));
|
||||
let trait_name = names.trait_name(&module.name);
|
||||
let fs = module
|
||||
.funcs()
|
||||
.map(|f| define_func(&names, &f, quote!(#trait_name)));
|
||||
let modtrait = define_module_trait(&names, &module);
|
||||
let ctx_type = names.ctx_type();
|
||||
quote!(
|
||||
|
||||
@@ -4,21 +4,19 @@ use quote::{format_ident, quote};
|
||||
use witx::{AtomType, BuiltinType, Id, TypeRef};
|
||||
|
||||
use crate::lifetimes::LifetimeExt;
|
||||
use crate::Config;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Names {
|
||||
config: Config,
|
||||
ctx_type: Ident,
|
||||
}
|
||||
|
||||
impl Names {
|
||||
pub fn new(config: &Config) -> Names {
|
||||
pub fn new(ctx_type: &Ident) -> Names {
|
||||
Names {
|
||||
config: config.clone(),
|
||||
ctx_type: ctx_type.clone(),
|
||||
}
|
||||
}
|
||||
pub fn ctx_type(&self) -> Ident {
|
||||
self.config.ctx.name.clone()
|
||||
self.ctx_type.clone()
|
||||
}
|
||||
pub fn type_(&self, id: &Id) -> TokenStream {
|
||||
let ident = format_ident!("{}", id.as_str().to_camel_case());
|
||||
|
||||
Reference in New Issue
Block a user