blob: bfda755d956d509cdbf605e03be600c1119e6c40 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# proc-macro-crate
[](https://docs.rs/proc-macro-crate/) [](https://crates.io/crates/proc-macro-crate) [](https://crates.io/crates/proc-macro-crate) [](https://travis-ci.org/bkchr/proc-macro-crate)
Providing support for `$crate` in procedural macros.
* [Introduction](#introduction)
* [Example](#example)
* [License](#license)
### Introduction
In `macro_rules!` `$crate` is used to get the path of the crate where a macro is declared in. In
procedural macros there is currently no easy way to get this path. A common hack is to import the
desired crate with a know name and use this. However, with rust edition 2018 and dropping
`extern crate` declarations from `lib.rs`, people start to rename crates in `Cargo.toml` directly.
However, this breaks importing the crate, as the proc-macro developer does not know the renamed
name of the crate that should be imported.
This crate provides a way to get the name of a crate, even if it renamed in `Cargo.toml`. For this
purpose a single function `crate_name` is provided. This function needs to be called in the context
of a proc-macro with the name of the desired crate. `CARGO_MANIFEST_DIR` will be used to find the
current active `Cargo.toml` and this `Cargo.toml` is searched for the desired crate.
### Example
```rust
use quote::quote;
use syn::Ident;
use proc_macro2::Span;
use proc_macro_crate::{crate_name, FoundCrate};
fn import_my_crate() {
let found_crate = crate_name("my-crate").expect("my-crate is present in `Cargo.toml`");
match found_crate {
FoundCrate::Itself => quote!( crate::Something ),
FoundCrate::Name(name) => {
let ident = Ident::new(&name, Span::call_site());
quote!( #ident::Something )
}
};
}
```
### License
Licensed under either of
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
* [MIT license](http://opensource.org/licenses/MIT)
at your option.
License: Apache-2.0/MIT
|