[][src]Struct find_crate::Manifest

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]

pub fn new() -> Result<Self, Error>[src]

Constructs 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]

Constructs a new Manifest from a toml table.

pub fn find<P>(&self, predicate: P) -> Option<Package> where
    P: FnMut(&str) -> bool
[src]

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]

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(&current_crate_package.name, Span::call_site());
    quote!(#name)
}

Trait Implementations

impl Clone for Manifest[src]

impl Debug for Manifest[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.