11use alloc:: { format, string:: String } ;
22use proc_macro2:: { Span , TokenStream as TokenStream2 } ;
3- use std:: io:: Error as IOError ;
3+ use std:: { borrow :: Cow , io:: Error as IOError , path :: Path } ;
44use syn:: Error as SynError ;
55
66/// Variants of errors when loading a file and presenting it as a set of compiler trees.
77#[ derive( Debug ) ]
88pub enum LoadFileAndAutoMakeTreeErr < ' a > {
99 /// The error type for I/O operations of the
1010 /// [Read], [Write], [Seek], and associated traits.
11- ReadToString { err : IOError , path : & ' a str } ,
11+ ReadToString { err : IOError , path : Cow < ' a , Path > } ,
1212
1313 /// Error returned when a Syn parser cannot parse the input tokens.
1414 ParseStr ( SynError ) ,
@@ -18,24 +18,24 @@ impl<'a> LoadFileAndAutoMakeTreeErr<'a> {
1818 /// The error type for I/O operations of the
1919 /// [Read], [Write], [Seek], and associated traits.
2020 #[ inline]
21- pub const fn read_to_string ( err : IOError , path : & ' a str ) -> Self {
21+ pub const fn read_to_string ( err : IOError , path : Cow < ' a , Path > ) -> Self {
2222 Self :: ReadToString { err, path }
2323 }
2424
2525 /// Convert an error to a syntax tree.
26- #[ inline]
2726 pub fn into_tt_err ( self , span : Span ) -> TokenStream2 {
2827 match self {
2928 Self :: ReadToString { err, path } => {
29+ let spath = format ! ( "{path:?}" ) ; // TODO REFACTORME
3030 let se = format ! ( "{err:?}" ) ;
3131 sg_err ! {
32- [ span] : "Error loading file, err: " , #se, ", path: " , #path , "."
32+ [ span] : "Error loading file, err: ' " , #se, "' , path: " , #spath , "."
3333 }
3434 }
3535 Self :: ParseStr ( e) => {
3636 let se = format ! ( "{e:?}" ) ;
3737 sg_err ! {
38- [ span] : "Failed to convert to tree `tt`: " , #se, "."
38+ [ span] : "Failed to convert to tree `tt`: ' " , #se, "' ."
3939 }
4040 }
4141 }
@@ -45,7 +45,7 @@ impl<'a> LoadFileAndAutoMakeTreeErr<'a> {
4545#[ allow( dead_code) ]
4646/// Load the file and present it as a compiler tree set.
4747pub fn load_file_and_automake_tree (
48- path : & str ,
48+ path : & Path ,
4949
5050 // Preprocessing a file loaded into a String before passing it directly to the parser.
5151 //
@@ -57,7 +57,7 @@ pub fn load_file_and_automake_tree(
5757
5858/// Load the file and present it as a compiler tree set.
5959pub fn load_file_and_automake_tree_with_fns < ' a , R > (
60- path : & ' a str ,
60+ path : & ' a Path ,
6161
6262 // Preprocessing a file loaded into a String before passing it directly to the parser.
6363 //
@@ -69,7 +69,12 @@ pub fn load_file_and_automake_tree_with_fns<'a, R>(
6969) -> R {
7070 let mut data = match std:: fs:: read_to_string ( path) {
7171 Ok ( a) => a,
72- Err ( e) => return err ( LoadFileAndAutoMakeTreeErr :: ReadToString { err : e, path : path } ) ,
72+ Err ( e) => {
73+ let path = path
74+ . canonicalize ( )
75+ . map_or_else ( |_| Cow :: Borrowed ( path) , Cow :: Owned ) ;
76+ return err ( LoadFileAndAutoMakeTreeErr :: read_to_string ( e, path) ) ;
77+ }
7378 } ;
7479
7580 if data. is_empty ( ) {
0 commit comments