@@ -131,7 +131,7 @@ impl CompileTarget {
131
131
if name. is_empty ( ) {
132
132
anyhow:: bail!( "target was empty" ) ;
133
133
}
134
- if !name . ends_with ( ".json" ) {
134
+ if !Self :: name_ends_with_json ( name ) {
135
135
return Ok ( CompileTarget { name : name. into ( ) } ) ;
136
136
}
137
137
@@ -170,7 +170,7 @@ impl CompileTarget {
170
170
// name without ".json") as a short name for this target. Note that the
171
171
// `unwrap()` here should never trigger since we have a nonempty name
172
172
// and it starts as utf-8 so it's always utf-8
173
- if self . name . ends_with ( ".json" ) {
173
+ if self . is_json_file ( ) {
174
174
Path :: new ( & self . name ) . file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( )
175
175
} else {
176
176
& self . name
@@ -180,11 +180,7 @@ impl CompileTarget {
180
180
/// See [`CompileKind::fingerprint_hash`].
181
181
pub fn fingerprint_hash ( & self ) -> u64 {
182
182
let mut hasher = StableHasher :: new ( ) ;
183
- match self
184
- . name
185
- . ends_with ( ".json" )
186
- . then ( || fs:: read_to_string ( self . name ) )
187
- {
183
+ match self . is_json_file ( ) . then ( || fs:: read_to_string ( self . name ) ) {
188
184
Some ( Ok ( contents) ) => {
189
185
// This may have some performance concerns, since it is called
190
186
// fairly often. If that ever seems worth fixing, consider
@@ -197,4 +193,25 @@ impl CompileTarget {
197
193
}
198
194
hasher. finish ( )
199
195
}
196
+
197
+ /// Checks if name of `self` contains a filename with json ext.
198
+ /// Does not check if the path exists.
199
+ ///
200
+ /// Same as [`Self::name_ends_with_json`], use it to check strings if create `CompileTarget` isn't a way.
201
+ #[ inline]
202
+ pub fn is_json_file ( & self ) -> bool {
203
+ Self :: name_ends_with_json ( self . name )
204
+ }
205
+
206
+ /// Helper function to check if the `name` ends with ".json" (case-insensitive).
207
+ /// Does not check if the path exists.
208
+ pub fn name_ends_with_json < S : AsRef < str > > ( name : S ) -> bool {
209
+ const EXT : & ' static str = ".json" ;
210
+ name. as_ref ( ) . ends_with ( EXT ) || {
211
+ let s = name. as_ref ( ) ;
212
+ s. get ( ( s. len ( ) - 5 ) ..)
213
+ . map ( |ext| ext. eq_ignore_ascii_case ( EXT ) )
214
+ . unwrap_or_default ( )
215
+ }
216
+ }
200
217
}
0 commit comments