@@ -4,9 +4,7 @@ use std::fs;
4
4
use std:: io;
5
5
use std:: path:: PathBuf ;
6
6
7
- use crate :: file:: {
8
- format:: all_extensions, source:: FileSourceResult , FileSource , FileStoredFormat , Format ,
9
- } ;
7
+ use crate :: file:: { source:: FileSourceResult , FileFormat , FileSource , FileStoredFormat , Format } ;
10
8
11
9
/// Describes a file sourced from a file
12
10
#[ derive( Clone , Debug ) ]
@@ -27,70 +25,61 @@ impl FileSourceFile {
27
25
where
28
26
F : FileStoredFormat + Format + ' static ,
29
27
{
30
- let filename = if self . name . is_absolute ( ) {
28
+ let path = if self . name . is_absolute ( ) {
31
29
self . name . clone ( )
32
30
} else {
33
31
env:: current_dir ( ) ?. as_path ( ) . join ( & self . name )
34
32
} ;
35
33
36
34
// First check for an _exact_ match
37
- if filename . is_file ( ) {
38
- return if let Some ( format) = format_hint {
39
- Ok ( ( filename , Box :: new ( format) ) )
35
+ if path . is_file ( ) {
36
+ if let Some ( format) = format_hint {
37
+ return Ok ( ( path , Box :: new ( format) ) ) ;
40
38
} else {
41
- for ( format, extensions) in all_extensions ( ) . iter ( ) {
42
- if extensions. contains (
43
- & filename
44
- . extension ( )
45
- . unwrap_or_default ( )
46
- . to_string_lossy ( )
47
- . as_ref ( ) ,
48
- ) {
49
- return Ok ( ( filename, Box :: new ( * format) ) ) ;
39
+ let ext = path. extension ( ) . unwrap_or_default ( ) . to_string_lossy ( ) ;
40
+ for format in FileFormat :: all ( ) {
41
+ if format. extensions ( ) . contains ( & ext. as_ref ( ) ) {
42
+ return Ok ( ( path, Box :: new ( * format) ) ) ;
50
43
}
51
44
}
52
-
53
- Err ( Box :: new ( io:: Error :: new (
45
+ return Err ( Box :: new ( io:: Error :: new (
54
46
io:: ErrorKind :: NotFound ,
55
47
format ! (
56
- "configuration file \" {}\" is not of a registered file format" ,
57
- filename . to_string_lossy( )
48
+ "configuration file \" {}\" is not of a supported file format" ,
49
+ path . to_string_lossy( )
58
50
) ,
59
- ) ) )
51
+ ) ) ) ;
60
52
} ;
61
53
}
62
54
63
- let mut filename = filename ;
55
+ let mut path = path ;
64
56
// Preserve any extension-like text within the provided file stem by appending a fake extension
65
57
// which will be replaced by `set_extension()` calls (e.g. `file.local.placeholder` => `file.local.json`)
66
- if filename . extension ( ) . is_some ( ) {
67
- filename . as_mut_os_string ( ) . push ( ".placeholder" ) ;
58
+ if path . extension ( ) . is_some ( ) {
59
+ path . as_mut_os_string ( ) . push ( ".placeholder" ) ;
68
60
}
69
-
70
61
match format_hint {
71
62
Some ( format) => {
72
63
for ext in format. file_extensions ( ) {
73
- filename . set_extension ( ext) ;
64
+ path . set_extension ( ext) ;
74
65
75
- if filename . is_file ( ) {
76
- return Ok ( ( filename , Box :: new ( format) ) ) ;
66
+ if path . is_file ( ) {
67
+ return Ok ( ( path , Box :: new ( format) ) ) ;
77
68
}
78
69
}
79
70
}
80
-
81
71
None => {
82
- for format in all_extensions ( ) . keys ( ) {
72
+ for format in FileFormat :: all ( ) {
83
73
for ext in format. extensions ( ) {
84
- filename . set_extension ( ext) ;
74
+ path . set_extension ( ext) ;
85
75
86
- if filename . is_file ( ) {
87
- return Ok ( ( filename , Box :: new ( * format) ) ) ;
76
+ if path . is_file ( ) {
77
+ return Ok ( ( path , Box :: new ( * format) ) ) ;
88
78
}
89
79
}
90
80
}
91
81
}
92
82
}
93
-
94
83
Err ( Box :: new ( io:: Error :: new (
95
84
io:: ErrorKind :: NotFound ,
96
85
format ! (
0 commit comments