Why does the specified file cannot be found in proc marcor

I want to implement my own include, the implementations is something like this:

pub fn my_include(v:TokenStream)->TokenStream{
   let path = v.to_string();
   let r = std::fs::read_to_string(path).unwrap();

Then, using the proc macro likes this way:

fn main(){

The program always panic with the error message:

message: called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }

However, if I change path to ./a.txt, it's ok? what's the wrong here?

PS: The issue still remains even though I change the my_include!("./a.txt"); to give a absolute path.

There's no filename called "./a.txt" (quotes included). Remember that you're dealing with tokens, not their typed post-compilation values.

Try using syn to do something like

let path = parse_macro_input!(v as LitStr).value();

Copy-pasted from here...

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.