Commit c3f9259 1 parent b8f8e4f commit c3f9259 Copy full SHA for c3f9259
File tree 1 file changed +23
-1
lines changed
1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -213,6 +213,14 @@ impl<S: PartialEq> Term<S> {
213
213
}
214
214
}
215
215
216
+ #[ cfg( feature = "std" ) ]
217
+ fn expand_prefix ( path : & Path , pre : & str , f : impl FnOnce ( ) -> Option < PathBuf > ) -> Option < PathBuf > {
218
+ let rest = path. strip_prefix ( pre) . ok ( ) ?;
219
+ let mut replace = f ( ) ?;
220
+ replace. push ( rest) ;
221
+ Some ( replace)
222
+ }
223
+
216
224
#[ cfg( feature = "std" ) ]
217
225
impl < ' a > Import < ' a , & ' a str > {
218
226
fn meta_paths ( & self ) -> Vec < PathBuf > {
@@ -240,10 +248,24 @@ impl<'a> Import<'a, &'a str> {
240
248
}
241
249
rel. set_extension ( ext) ;
242
250
251
+ #[ cfg( target_os = "windows" ) ]
252
+ let home = "USERPROFILE" ;
253
+ #[ cfg( not( target_os = "windows" ) ) ]
254
+ let home = "HOME" ;
255
+
256
+ use std:: env;
257
+ let home = || env:: var_os ( home) . map ( PathBuf :: from) ;
258
+ let origin = || env:: current_exe ( ) . ok ( ) ?. parent ( ) . map ( PathBuf :: from) ;
259
+
243
260
self . meta_paths ( )
244
261
. iter ( )
245
262
. chain ( paths)
246
- . map ( |path| parent. join ( path) . join ( & rel) )
263
+ . map ( |path| {
264
+ let home = expand_prefix ( path, "~" , home) ;
265
+ let orig = expand_prefix ( path, "$ORIGIN" , origin) ;
266
+ let path = home. as_ref ( ) . or ( orig. as_ref ( ) ) . unwrap_or ( path) ;
267
+ parent. join ( path) . join ( & rel)
268
+ } )
247
269
. filter_map ( |path| path. canonicalize ( ) . ok ( ) )
248
270
. find ( |path| path. is_file ( ) )
249
271
. ok_or_else ( || "file not found" . into ( ) )
You can’t perform that action at this time.
0 commit comments