2020-02-15 11:43:47 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use devise::{syn::{self, Ident, LitStr}, Result};
|
|
|
|
|
|
|
|
use crate::syn_ext::syn_to_diag;
|
|
|
|
use crate::proc_macro2::TokenStream as TokenStream2;
|
|
|
|
|
|
|
|
pub fn _macro(input: TokenStream) -> Result<TokenStream> {
|
2020-06-15 01:56:45 +00:00
|
|
|
let root_glob = syn::parse::<LitStr>(input.into()).map_err(syn_to_diag)?;
|
|
|
|
let modules = entry_to_modules(&root_glob)
|
|
|
|
.map_err(|e| root_glob.span().unstable().error(format!("failed to read: {}", e)))?;
|
2020-02-15 11:43:47 +00:00
|
|
|
|
2020-06-15 01:56:45 +00:00
|
|
|
Ok(quote_spanned!(root_glob.span() =>
|
2020-02-15 11:43:47 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
mod test_site_guide { #(#modules)* }
|
|
|
|
).into())
|
|
|
|
}
|
|
|
|
|
2020-06-15 01:56:45 +00:00
|
|
|
fn entry_to_modules(root_glob: &LitStr) -> std::result::Result<Vec<TokenStream2>, Box<dyn Error>> {
|
2020-02-15 11:43:47 +00:00
|
|
|
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("MANIFEST_DIR");
|
2020-06-15 01:56:45 +00:00
|
|
|
let full_glob = Path::new(&manifest_dir).join(&root_glob.value()).display().to_string();
|
2020-02-15 11:43:47 +00:00
|
|
|
|
|
|
|
let mut modules = vec![];
|
2020-06-15 01:56:45 +00:00
|
|
|
for path in glob::glob(&full_glob).map_err(Box::new)? {
|
|
|
|
let path = path.map_err(Box::new)?;
|
2020-02-15 11:43:47 +00:00
|
|
|
let name = path.file_name()
|
|
|
|
.and_then(|f| f.to_str())
|
|
|
|
.map(|name| name.trim_matches(|c| char::is_numeric(c) || c == '-')
|
2020-06-15 01:56:45 +00:00
|
|
|
.replace(|c| c == '-' || c == '.', "_"))
|
|
|
|
.ok_or_else(|| "invalid file name")?;
|
2020-02-15 11:43:47 +00:00
|
|
|
|
2020-06-15 01:56:45 +00:00
|
|
|
let ident = Ident::new(&name, root_glob.span());
|
2020-02-15 11:43:47 +00:00
|
|
|
let full_path = Path::new(&manifest_dir).join(&path).display().to_string();
|
2020-06-15 01:56:45 +00:00
|
|
|
modules.push(quote_spanned!(root_glob.span() =>
|
2020-02-15 11:43:47 +00:00
|
|
|
#[doc(include = #full_path)]
|
|
|
|
struct #ident;
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(modules)
|
|
|
|
}
|