3
3
#![ allow( dead_code, unused_imports) ]
4
4
5
5
use std:: {
6
+ fmt:: Write ,
6
7
fs,
7
8
path:: { Path , PathBuf } ,
8
9
} ;
@@ -159,7 +160,7 @@ struct Input {
159
160
///
160
161
/// If the subdirectory is omitted, we assume that the output goes
161
162
/// to "wgsl".
162
- subdirectory : Option < PathBuf > ,
163
+ subdirectory : PathBuf ,
163
164
164
165
/// The input filename name, without a directory.
165
166
file_name : PathBuf ,
@@ -182,9 +183,9 @@ impl Input {
182
183
/// The `input` path is interpreted relative to the `BASE_DIR_IN`
183
184
/// subdirectory of the directory given by the `CARGO_MANIFEST_DIR`
184
185
/// environment variable.
185
- fn new ( subdirectory : Option < & str > , name : & str , extension : & str ) -> Input {
186
+ fn new ( subdirectory : & str , name : & str , extension : & str ) -> Input {
186
187
Input {
187
- subdirectory : subdirectory . map ( PathBuf :: from) ,
188
+ subdirectory : PathBuf :: from ( subdirectory ) ,
188
189
// Don't wipe out any extensions on `name`, as
189
190
// `with_extension` would do.
190
191
file_name : PathBuf :: from ( format ! ( "{name}.{extension}" ) ) ,
@@ -194,13 +195,11 @@ impl Input {
194
195
195
196
/// Return an iterator that produces an `Input` for each entry in `subdirectory`.
196
197
fn files_in_dir (
197
- subdirectory : Option < & ' static str > ,
198
+ subdirectory : & ' static str ,
198
199
file_extensions : & ' static [ & ' static str ] ,
199
200
) -> impl Iterator < Item = Input > + ' static {
200
- let mut input_directory = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) . join ( BASE_DIR_IN ) ;
201
- if let Some ( ref subdirectory) = subdirectory {
202
- input_directory. push ( subdirectory) ;
203
- }
201
+ let input_directory = Path :: new ( CRATE_ROOT ) . join ( BASE_DIR_IN ) . join ( subdirectory) ;
202
+
204
203
let entries = match std:: fs:: read_dir ( & input_directory) {
205
204
Ok ( entries) => entries,
206
205
Err ( err) => panic ! (
@@ -237,14 +236,12 @@ impl Input {
237
236
/// Return the path to the input directory.
238
237
fn input_directory ( & self ) -> PathBuf {
239
238
let mut dir = Path :: new ( CRATE_ROOT ) . join ( BASE_DIR_IN ) ;
240
- if let Some ( ref subdirectory) = self . subdirectory {
241
- dir. push ( subdirectory) ;
242
- }
239
+ dir. push ( & self . subdirectory ) ;
243
240
dir
244
241
}
245
242
246
243
/// Return the path to the output directory.
247
- fn output_directory ( & self , subdirectory : & str ) -> PathBuf {
244
+ fn output_directory ( subdirectory : & str ) -> PathBuf {
248
245
let mut dir = Path :: new ( CRATE_ROOT ) . join ( BASE_DIR_OUT ) ;
249
246
dir. push ( subdirectory) ;
250
247
dir
@@ -258,14 +255,22 @@ impl Input {
258
255
}
259
256
260
257
fn output_path ( & self , subdirectory : & str , extension : & str ) -> PathBuf {
261
- let mut output = self . output_directory ( subdirectory) ;
258
+ let mut output = Self :: output_directory ( subdirectory) ;
262
259
if self . keep_input_extension {
263
- let mut file_name = self . file_name . as_os_str ( ) . to_owned ( ) ;
264
- file_name. push ( "." ) ;
265
- file_name. push ( extension) ;
260
+ let mut file_name = String :: new ( ) ;
261
+ write ! ( & mut file_name, "{}" , self . subdirectory. display( ) ) . unwrap ( ) ;
262
+ file_name. push_str ( "-" ) ;
263
+ write ! ( & mut file_name, "{}" , self . file_name. display( ) ) . unwrap ( ) ;
264
+ file_name. push ( '.' ) ;
265
+ file_name. push_str ( extension) ;
266
266
output. push ( & file_name) ;
267
267
} else {
268
- output. push ( & self . file_name ) ;
268
+ let mut file_name = String :: new ( ) ;
269
+ write ! ( & mut file_name, "{}" , self . subdirectory. display( ) ) . unwrap ( ) ;
270
+ file_name. push_str ( "-" ) ;
271
+ write ! ( & mut file_name, "{}" , self . file_name. display( ) ) . unwrap ( ) ;
272
+
273
+ output. push ( & file_name) ;
269
274
output. set_extension ( extension) ;
270
275
}
271
276
output
@@ -771,7 +776,7 @@ fn write_output_wgsl(
771
776
fn convert_snapshots_wgsl ( ) {
772
777
let _ = env_logger:: try_init ( ) ;
773
778
774
- for input in Input :: files_in_dir ( Some ( "wgsl" ) , & [ "wgsl" ] ) {
779
+ for input in Input :: files_in_dir ( "wgsl" , & [ "wgsl" ] ) {
775
780
let source = input. read_source ( ) ;
776
781
// crlf will make the large split output different on different platform
777
782
let source = source. replace ( '\r' , "" ) ;
@@ -792,7 +797,7 @@ fn convert_snapshots_spv() {
792
797
793
798
let _ = env_logger:: try_init ( ) ;
794
799
795
- for input in Input :: files_in_dir ( Some ( "spv" ) , & [ "spvasm" ] ) {
800
+ for input in Input :: files_in_dir ( "spv" , & [ "spvasm" ] ) {
796
801
println ! ( "Assembling '{}'" , input. file_name. display( ) ) ;
797
802
798
803
let command = Command :: new ( "spirv-as" )
@@ -840,7 +845,7 @@ fn convert_snapshots_spv() {
840
845
fn convert_snapshots_glsl ( ) {
841
846
let _ = env_logger:: try_init ( ) ;
842
847
843
- for input in Input :: files_in_dir ( Some ( "glsl" ) , & [ "vert" , "frag" , "comp" ] ) {
848
+ for input in Input :: files_in_dir ( "glsl" , & [ "vert" , "frag" , "comp" ] ) {
844
849
let input = Input {
845
850
keep_input_extension : true ,
846
851
..input
0 commit comments