pkg_version_impl/
lib.rs

1//! Implementation details of the [`pkg-version`] crate.
2//!
3//! Do not use this crate directly. It does not provide a stable API and might
4//! break at any time. Use [`pkg-version`] instead.
5//!
6//! [`pkg-version`]: https://docs.rs/pkg-version
7
8#![doc(html_root_url = "https://docs.rs/pkg-version-impl/0.0.0")]
9#![warn(missing_debug_implementations, rust_2018_idioms)]
10
11extern crate proc_macro;
12
13use proc_macro::TokenStream;
14use proc_macro_hack::proc_macro_hack;
15use std::env;
16
17/// A type large enough to hold a version component.
18///
19/// This should match what the `semver` crate uses.
20type VersionNum = u64;
21
22#[proc_macro_hack]
23pub fn pkg_version_major(input: TokenStream) -> TokenStream {
24    if !input.is_empty() {
25        panic!("unexpected arguments for `pkg_version_major!` macro (expected no arguments)");
26    }
27
28    let version = env::var("CARGO_PKG_VERSION_MAJOR")
29        .unwrap()
30        .parse::<VersionNum>()
31        .unwrap();
32
33    version.to_string().parse().unwrap()
34}
35
36#[proc_macro_hack]
37pub fn pkg_version_minor(input: TokenStream) -> TokenStream {
38    if !input.is_empty() {
39        panic!("unexpected arguments for `pkg_version_minor!` macro (expected no arguments)");
40    }
41
42    let version = env::var("CARGO_PKG_VERSION_MINOR")
43        .unwrap()
44        .parse::<VersionNum>()
45        .unwrap();
46
47    version.to_string().parse().unwrap()
48}
49
50#[proc_macro_hack]
51pub fn pkg_version_patch(input: TokenStream) -> TokenStream {
52    if !input.is_empty() {
53        panic!("unexpected arguments for `pkg_version_patch!` macro (expected no arguments)");
54    }
55
56    let version = env::var("CARGO_PKG_VERSION_PATCH")
57        .unwrap()
58        .parse::<VersionNum>()
59        .unwrap();
60
61    version.to_string().parse().unwrap()
62}