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
59
60
61
62
//! Implementation details of the [`pkg-version`] crate.
//!
//! Do not use this crate directly. It does not provide a stable API and might
//! break at any time. Use [`pkg-version`] instead.
//!
//! [`pkg-version`]: https://docs.rs/pkg-version

#![doc(html_root_url = "https://docs.rs/pkg-version-impl/0.0.0")]
#![warn(missing_debug_implementations, rust_2018_idioms)]

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro_hack::proc_macro_hack;
use std::env;

/// A type large enough to hold a version component.
///
/// This should match what the `semver` crate uses.
type VersionNum = u64;

#[proc_macro_hack]
pub fn pkg_version_major(input: TokenStream) -> TokenStream {
    if !input.is_empty() {
        panic!("unexpected arguments for `pkg_version_major!` macro (expected no arguments)");
    }

    let version = env::var("CARGO_PKG_VERSION_MAJOR")
        .unwrap()
        .parse::<VersionNum>()
        .unwrap();

    version.to_string().parse().unwrap()
}

#[proc_macro_hack]
pub fn pkg_version_minor(input: TokenStream) -> TokenStream {
    if !input.is_empty() {
        panic!("unexpected arguments for `pkg_version_minor!` macro (expected no arguments)");
    }

    let version = env::var("CARGO_PKG_VERSION_MINOR")
        .unwrap()
        .parse::<VersionNum>()
        .unwrap();

    version.to_string().parse().unwrap()
}

#[proc_macro_hack]
pub fn pkg_version_patch(input: TokenStream) -> TokenStream {
    if !input.is_empty() {
        panic!("unexpected arguments for `pkg_version_patch!` macro (expected no arguments)");
    }

    let version = env::var("CARGO_PKG_VERSION_PATCH")
        .unwrap()
        .parse::<VersionNum>()
        .unwrap();

    version.to_string().parse().unwrap()
}