rocket_codegen/bang/
test_guide.rs1use std::path::Path;
2use std::error::Error;
3
4use syn::{self, Ident, LitStr};
5use devise::ext::SpanDiagnosticExt;
6use proc_macro2::TokenStream;
7
8pub fn _macro(input: proc_macro::TokenStream) -> devise::Result<TokenStream> {
9 let root_glob = syn::parse::<LitStr>(input)?;
10 let tests = entry_to_tests(&root_glob)
11 .map_err(|e| root_glob.span().error(format!("failed to read: {}", e)))?;
12
13 Ok(quote!(#(#tests)*))
14}
15
16fn entry_to_tests(root_glob: &LitStr) -> Result<Vec<TokenStream>, Box<dyn Error>> {
17 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("MANIFEST_DIR");
18 let full_glob = Path::new(&manifest_dir).join(&root_glob.value()).display().to_string();
19
20 let mut tests = vec![];
21 for path in glob::glob(&full_glob).map_err(Box::new)? {
22 let path = path.map_err(Box::new)?;
23 let name = path.file_name()
24 .and_then(|f| f.to_str())
25 .map(|name| name.trim_matches(|c| char::is_numeric(c) || c == '-')
26 .replace(|c| c == '-' || c == '.', "_"))
27 .ok_or("invalid file name")?;
28
29 let ident = Ident::new(&name.to_lowercase(), root_glob.span());
30 let full_path = Path::new(&manifest_dir).join(&path).display().to_string();
31 tests.push(quote_spanned!(root_glob.span() =>
32 #[allow(unused_doc_comments)]
33 mod #ident {
34 macro_rules! doc_comment { ($x:expr) => (#[doc = $x] extern {}); }
35 doc_comment!(include_str!(#full_path));
36 }
37 ));
38 }
39
40 if tests.is_empty() {
41 return Err(format!("glob '{}' evaluates to 0 files", full_glob).into());
42 }
43
44 Ok(tests)
45}