I'm creating a procedural macro that auto-generates a library from some configuration file (it's a register layout, but that's not important for the question).
I would like the library to auto-generate the documentation accompanying the auto-library and include doc tests that should run with cargo test
. Now, I've implemented most of this, but there is one issue I can't see a solution to.
Say we have a library called my_lib
in which we invoke the macro to populate it:
use my_macro_lib::hello;
hello!();
which expands to something like:
/// `foo` will always return `true`
/// ```
/// use my_lib;
/// assert!(my_lib::foo());
/// ```
pub fn foo() -> bool {
true
}
This will run as expected - cargo doc
will do the right thing and cargo test
will run the doctests as expected.
The problem is that in this example, use my_lib
is hard-coded into the my_macro_lib
which is clearly undesirable.
How can I create a macro which infers the name of crate that is doing the calling?
I tried using a macro_rules!
inside the procedural macro to expand $crate
, but this breaks the hygiene rules.