Struct find_crate::Manifest [−][src]
pub struct Manifest { pub dependencies: Dependencies, // some fields omitted }
The manifest of cargo.
Note that this function needs to be used in the context of proc-macro.
Fields
dependencies: Dependencies
The kind of dependencies to be searched.
Implementations
impl Manifest
[src]
impl Manifest
[src]pub fn new() -> Result<Self, Error>
[src]
Creates a new Manifest
from the current Cargo.toml
.
This function reads Cargo.toml
in CARGO_MANIFEST_DIR
as manifest.
pub fn from_toml(manifest: Table) -> Self
[src]
Creates a new Manifest
from a toml table.
pub fn find<P>(&self, predicate: P) -> Option<Package> where
P: FnMut(&str) -> bool,
[src]
P: FnMut(&str) -> bool,
Find the crate.
The argument of the closure is the original name of the package.
Examples
use find_crate::Manifest; use proc_macro2::{Ident, Span, TokenStream}; use quote::quote; fn import() -> TokenStream { let manifest = Manifest::new().unwrap(); let name = manifest.find(|s| s == "foo" || s == "foo-core").unwrap().name; 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;) }
pub fn find2<P>(&self, predicate: P) -> Option<Package> where
P: FnMut(&str, &str) -> bool,
[src]
P: FnMut(&str, &str) -> bool,
Find the crate.
The first argument of the closure is the original name of the package and the second argument is the version of the package.
Examples
use find_crate::Manifest; use proc_macro2::{Ident, Span, TokenStream}; use quote::quote; use semver::{Version, VersionReq}; fn check_version(req: &str, version: &Version) -> bool { VersionReq::parse(req).unwrap().matches(version) } fn import() -> TokenStream { let version = Version::parse("0.3.0").unwrap(); let manifest = Manifest::new().unwrap(); let name = manifest .find2(|name, req| name == "foo" && check_version(req, &version)) .unwrap() .name; 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;) }
pub fn crate_package(&self) -> Result<Package, Error>
[src]
The package for the crate that this manifest represents.
Examples
use find_crate::Manifest; use proc_macro2::{Ident, Span, TokenStream}; use quote::quote; fn current_crate_name() -> TokenStream { let manifest = Manifest::new().unwrap(); let current_crate_package = manifest.crate_package().unwrap(); let name = Ident::new(¤t_crate_package.name, Span::call_site()); quote!(#name) }