pub fn expand_modules_into_inline_modules<R: Resolver>(
content: &mut File,
resolver: &mut R,
) -> Result<(), Error>
Expand description
Take a syn
representation of a Rust source file and turn into similar syn
representation,
but with mod something;
expanded into mod something { ... }
.
It is low-level IO-agnostic function: your callback is responsible for reading and parsing the data.
Use read_full_crate_source_code
for easy way to just load crate source code.
Example:
let mut ast: syn::File = syn::parse2(quote::quote! {
mod inner_module;
}).unwrap();
let cfg_evaluator = |_cfg: syn::Meta|Ok(false);
let code_loader = |_module:syn::Path, path:std::path::PathBuf|{
if path == std::path::Path::new("inner_module/mod.rs") {
Ok(Some(syn::parse2(quote::quote! {
trait Foo {
}
}).unwrap()))
} else {
Ok(None)
}
};
let mut resolver = syn_file_expand::ResolverHelper(code_loader, cfg_evaluator);
syn_file_expand::expand_modules_into_inline_modules(&mut ast, &mut resolver)?;
let expanded: syn::File = syn::parse2(quote::quote! {
mod inner_module {
trait Foo { }
}
}).unwrap();
assert_eq!(ast, expanded);