[][src]Function find_crate::find_crate

pub fn find_crate<P>(predicate: P) -> Result<String, Error> where
    P: FnMut(&str) -> bool

Find the crate name from the current Cargo.toml.

This function reads Cargo.toml in CARGO_MANIFEST_DIR as manifest.

Note that this function must be used in the context of proc-macro.

Examples

use find_crate::find_crate;
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;

fn import() -> TokenStream {
    let name = find_crate(|s| s == "foo" || s == "foo-core").unwrap();
    let name = Ident::new(&name, Span::call_site());
    // If your proc-macro crate is 2018 edition, use `quote!(use #name as _foo;)` instead.
    quote!(extern crate #name as _foo;)
}