Skip to content

Commit 00a0ce5

Browse files
committed
feat: improve error output details
1 parent ac7e19c commit 00a0ce5

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/macros/include.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ use alloc::string::String;
1111
use proc_macro2::{
1212
Delimiter, Group, Literal, Span, TokenStream as TokenStream2, TokenTree as TokenTree2,
1313
};
14-
use std::{fs::File, io::Read};
14+
use std::{
15+
borrow::Cow,
16+
fs::File,
17+
io::Read,
18+
path::Path,
19+
};
1520

1621
/// A trait that specifies the final behavior for the `include` macro.
1722
pub trait BehMacroInclude {
@@ -92,8 +97,9 @@ impl BehMacroInclude for IncludeTT {
9297
) -> TreeResult<Self::Result> {
9398
arg0.get_str_with_fns(
9499
|sspath| {
100+
let path = Path::new(sspath);
95101
load_file_and_automake_tree_with_fns(
96-
sspath,
102+
path,
97103
|_| {}, /* skip_prepare */
98104
|fs_tt| {
99105
let ett = match fs_tt {
@@ -138,6 +144,7 @@ impl BehMacroInclude for IncludeTTAndFixUnkStartToken {
138144
) -> TreeResult<Self::Result> {
139145
arg0.get_str_with_fns(
140146
|sspath| {
147+
let sspath = Path::new(sspath);
141148
load_file_and_automake_tree_with_fns(
142149
sspath,
143150
|p_string| {
@@ -238,10 +245,14 @@ impl BehMacroInclude for IncludeStr {
238245
) -> TreeResult<Self::Result> {
239246
arg0.get_str_with_fns(
240247
|sspath| {
241-
let data = match std::fs::read_to_string(sspath) {
248+
let path = Path::new(sspath);
249+
let data = match std::fs::read_to_string(path) {
242250
Ok(a) => a,
243251
Err(e) => {
244-
return LoadFileAndAutoMakeTreeErr::read_to_string(e, sspath)
252+
let path = path
253+
.canonicalize()
254+
.map_or_else(|_| Cow::Borrowed(path), Cow::Owned);
255+
return LoadFileAndAutoMakeTreeErr::read_to_string(e, path)
245256
.into_tt_err(literal_span)
246257
.into();
247258
}
@@ -280,18 +291,25 @@ impl BehMacroInclude for IncludeArr {
280291
arg0.get_str_with_fns(
281292
|sspath| {
282293
let vec = {
283-
let mut file = match File::open(sspath) {
294+
let path = Path::new(sspath);
295+
let mut file = match File::open(path) {
284296
Ok(a) => a,
285297
Err(e) => {
286-
return LoadFileAndAutoMakeTreeErr::read_to_string(e, sspath)
298+
let path = path
299+
.canonicalize()
300+
.map_or_else(|_| Cow::Borrowed(path), Cow::Owned);
301+
return LoadFileAndAutoMakeTreeErr::read_to_string(e, path)
287302
.into_tt_err(literal_span)
288303
.into();
289304
}
290305
};
291306

292307
let mut vec = Vec::new(); // capacity is not required.
293308
if let Err(e) = file.read_to_end(&mut vec) {
294-
return LoadFileAndAutoMakeTreeErr::read_to_string(e, sspath)
309+
let path = path
310+
.canonicalize()
311+
.map_or_else(|_| Cow::Borrowed(path), Cow::Owned);
312+
return LoadFileAndAutoMakeTreeErr::read_to_string(e, path)
295313
.into_tt_err(literal_span)
296314
.into();
297315
};

src/trees/loader.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use alloc::{format, string::String};
22
use proc_macro2::{Span, TokenStream as TokenStream2};
3-
use std::io::Error as IOError;
3+
use std::{borrow::Cow, io::Error as IOError, path::Path};
44
use 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)]
88
pub 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.
4747
pub 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.
5959
pub 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

Comments
 (0)