pkg_version/lib.rs
1//! Provides macros for fetching the Cargo package version at compile time.
2//!
3//! All macros defined by this crate return constant expressions, so they can be used inside
4//! `const fn`s or to initialize the value of a `const` or `static` item.
5//!
6//! # Example
7//!
8//! ```
9//! use pkg_version::*;
10//!
11//! const MAJOR: u32 = pkg_version_major!();
12//! const MINOR: u32 = pkg_version_minor!();
13//! const PATCH: u32 = pkg_version_patch!();
14//!
15//! fn main() {
16//! let version = format!("{}.{}.{}", MAJOR, MINOR, PATCH);
17//! assert_eq!(version, "1.0.0");
18//!
19//! println!("I am version {}", version);
20//! }
21//! ```
22
23#![no_std]
24#![doc(html_root_url = "https://docs.rs/pkg-version/1.0.0")]
25#![warn(missing_debug_implementations, rust_2018_idioms)]
26
27use proc_macro_hack::proc_macro_hack;
28
29/// Expands to the major version number of the Cargo package, as an integer literal.
30///
31/// The resulting integer literal is *unsuffixed*, meaning that it does not use a type suffix like
32/// `1u32`. This means that it can be used to initialize a variable of any integer type, as long as
33/// the version number fits. If the number doesn't fit, the compiler will report an error.
34#[proc_macro_hack]
35pub use pkg_version_impl::pkg_version_major;
36
37/// Expands to the minor version number of the Cargo package, as an integer literal.
38///
39/// The resulting integer literal is *unsuffixed*, meaning that it does not use a type suffix like
40/// `1u32`. This means that it can be used to initialize a variable of any integer type, as long as
41/// the version number fits. If the number doesn't fit, the compiler will report an error.
42#[proc_macro_hack]
43pub use pkg_version_impl::pkg_version_minor;
44
45/// Expands to the patch version number of the Cargo package, as an integer literal.
46///
47/// The resulting integer literal is *unsuffixed*, meaning that it does not use a type suffix like
48/// `1u32`. This means that it can be used to initialize a variable of any integer type, as long as
49/// the version number fits. If the number doesn't fit, the compiler will report an error.
50#[proc_macro_hack]
51pub use pkg_version_impl::pkg_version_patch;
52
53/// Make sure passing any arguments results in an error.
54///
55/// ```compile_fail
56/// use pkg_version::*;
57/// pkg_version_major!(_);
58/// ```
59///
60/// ```compile_fail
61/// use pkg_version::*;
62/// pkg_version_minor!(_);
63/// ```
64///
65/// ```compile_fail
66/// use pkg_version::*;
67/// pkg_version_patch!(_);
68/// ```
69const _COMPILE_FAIL: () = ();