1
1
//! A simple proc macro to generate a const list of filenames and optionally apply a macro to each.
2
- //!
2
+ //!
3
3
//! ```rust
4
4
//! use std::fs::canonicalize;
5
- //!
5
+ //!
6
6
//! use list_files_macro::list_files;
7
- //!
7
+ //!
8
8
//! fn get_full_path(path: &str) -> String {
9
9
//! canonicalize(path).unwrap().into_os_string().into_string().unwrap()
10
10
//! }
11
- //!
11
+ //!
12
12
//! const FILENAMES: [&'static str; 3] = list_files!("../tests/files/*.rs");
13
- //!
13
+ //!
14
14
//! assert_eq!(FILENAMES, [
15
15
//! "tests/files/a.rs",
16
16
//! "tests/files/b.rs",
17
17
//! "tests/files/c.rs",
18
18
//! ].map(get_full_path));
19
- //!
19
+ //!
20
20
//! const CONTENTS: [&'static str; 3] = list_files!(include_str, "../tests/files/*.rs");
21
- //!
21
+ //!
22
22
//! assert_eq!(CONTENTS[0], r#"
23
23
//! pub fn run() -> &'static str {
24
24
//! "A"
25
25
//! }
26
26
//! "#);
27
- //!
27
+ //!
28
28
//! macro_rules! run_file {
29
29
//! ($x:expr) => {
30
30
//! {
34
34
//! }
35
35
//! };
36
36
//! }
37
- //!
37
+ //!
38
38
//! let results = list_files!(run_file, "../tests/files/*.rs");
39
- //!
39
+ //!
40
40
//! assert_eq!(results, [
41
41
//! "A",
42
42
//! "B",
43
43
//! "C",
44
44
//! ]);
45
45
//! ```
46
- //!
46
+ //!
47
47
//! To use this, add it as a dependency to your Cargo.toml:
48
48
//! ```toml
49
49
//! [dependencies]
50
50
//! list_files_macro = "^0.1.0"
51
51
//! ```
52
52
53
- #![ feature( let_else) ]
54
53
#![ feature( proc_macro_span) ]
55
54
56
55
#![ doc( html_root_url = "https://docs.rs/list_files_macro/0.1.0" ) ]
@@ -73,23 +72,23 @@ pub fn list_files(input: TokenStream) -> TokenStream {
73
72
[ Expr :: Lit ( ExprLit { lit : Lit :: Str ( path) , .. } ) ] => ( path. value ( ) , None ) ,
74
73
_ => panic ! ( "Usage: list_files!(\" path/to/dir\" ) | list_files!(handler_macro, \" path/to/dir\" )" ) ,
75
74
} ;
76
-
75
+
77
76
// Resolve directory
78
77
let absolute_path =
79
78
if path. starts_with ( "." ) {
80
- let source_path = Span :: call_site ( ) . source_file ( ) . path ( ) ;
79
+ let source_path: std :: path :: PathBuf = Span :: call_site ( ) . file ( ) . into ( ) ;
81
80
source_path. parent ( ) . unwrap ( ) . join ( & path) . into_os_string ( ) . into_string ( ) . unwrap ( )
82
81
} else {
83
82
path
84
83
} ;
85
-
84
+
86
85
// Generate list of literal string paths
87
86
let files = glob ( & absolute_path) . unwrap ( ) ;
88
87
let file_literals = files. map ( |file| {
89
88
let file = file. unwrap ( ) . canonicalize ( ) . unwrap ( ) . into_os_string ( ) . into_string ( ) . unwrap ( ) ;
90
89
quote ! ( #file)
91
90
} ) ;
92
-
91
+
93
92
// Run handlers if required
94
93
if let Some ( handler) = handler {
95
94
quote ! ( [ #( #handler!( #file_literals) ) , * ] )
0 commit comments