use quote::quote; use crate::*; use syn::*; const FEATURE_NAME: &str = "only-types"; pub fn stub_function>(f: F) -> impl Into { let f: utils::SomeFunction = f.into(); let name = &f.sig.ident; let ret = &f.sig.output; let inputs = &f.sig.inputs; let body = &f.block; let attrs = &f.attrs; let vis = &f.vis; // Signatures let abi = &f.sig.abi; let asyncness = &f.sig.asyncness; let constness = &f.sig.constness; let unsafety = &f.sig.unsafety; let generics = &f.sig.generics; let where_clause = &f.sig.generics.where_clause; let (body, stubbed) = utils::stub_func_body(body) .expect("Failed to stub function body"); let result = quote! { #(#attrs)* #[allow(unused)] #[cfg_attr(feature = #FEATURE_NAME, doc = "STUBBED BY ONLYTYPES.")] #vis #constness #asyncness #unsafety #abi fn #name #generics(#inputs) #ret #where_clause { #[cfg(not(feature = #FEATURE_NAME))] #body #[cfg(feature = #FEATURE_NAME)] #stubbed } }; // let result = quote! { // #(#attrs)* // #[cfg(not(feature = #FEATURE_NAME))] // #vis #constness #asyncness #unsafety #abi fn #name #generics(#inputs) #ret #where_clause { // #body // } // #(#attrs)* // #[allow(unused)] // #[cfg(feature = #FEATURE_NAME)] // #[doc = "THIS WAS STUBBED BY ONLYTYPES."] // #vis #constness #asyncness #unsafety #abi fn #name #generics(#inputs) #ret #where_clause { // #stubbed // } // }; // panic!("{}", result.to_string()); result } pub fn stub_impl_items<'a>(items: &'a Vec) -> Vec { let mut stubbed_items = Vec::new(); for item in items { let stubbed = match item { ImplItem::Fn(f) => { let stubbed = utils::SomeFunction::from(f.clone()); let stubbed = stub_function(stubbed); let stubbed = parse2::(stubbed.into()) .expect("Failed to parse stubbed function"); ImplItem::Fn(stubbed) }, other => other.clone() }; stubbed_items.push(stubbed); }; stubbed_items } pub fn stub_impl(i: ItemImpl) -> impl Into { let attrs = &i.attrs; let unsafety = &i.unsafety; let generics = &i.generics; let trait_ = &i.trait_; let self_ty = &i.self_ty; let items = &i.items; let where_clause = &i.generics.where_clause; let items = stub_impl_items(items); let result = if trait_.is_some() { let trait_ = trait_.clone().unwrap().1; quote! { #(#attrs)* #unsafety impl #generics #trait_ for #self_ty #where_clause { #(#items)* } } } else { quote! { #(#attrs)* #unsafety impl #generics #self_ty #where_clause { #(#items)* } } }; result }