this_crate/
lib.rs

1//! This crate provides utility macros to work with crate version information.
2//!
3//! The two main macros are `version_str!` and `version_tuple!`. These macros return the
4//! crate version in string format and as a tuple of three `u16` integers, respectively.
5//!
6//! # Examples
7//!
8//! ```rust
9//! use this_crate::{version_str, version_tuple};
10//! let version = version_str!();
11//! println!("The version is: {}", version);
12//!
13//! let (major, minor, patch) = version_tuple!();
14//! println!("The version is: {}.{}.{}", major, minor, patch);
15//! ```
16//!
17//! # no_std
18//! This crate is `no_std` compatible, so it can be used in environments without
19//! the Rust standard library.
20//! 
21#![no_std]
22
23/// A tuple representing the version of the crate as (major, minor, patch).
24pub type VersionTuple = (u16, u16, u16);
25
26
27/// `version_str!` is a macro that returns the version string of the crate.
28///
29/// # Examples
30///
31/// ```
32/// use this_crate::version_str;
33/// let version = version_str!();
34/// println!("The version is: {}", version);
35/// ```
36#[macro_export]
37macro_rules! version_str {
38    () => {
39        env!("CARGO_PKG_VERSION")
40    };
41}
42
43/// `version_tuple!` is a macro that returns the version of the crate as a tuple
44/// of three `u16` integers: (major, minor, patch).
45///
46/// # Examples
47///
48/// ```
49/// use this_crate::version_tuple;
50/// let (major, minor, patch) = version_tuple!();
51/// println!("The version is: {}.{}.{}", major, minor, patch);
52/// ```
53#[macro_export]
54macro_rules! version_tuple {
55    () => {{
56        let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u16>().unwrap_or(0);
57        let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u16>().unwrap_or(0);
58        let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap_or(0);
59        (major, minor, patch)
60    }};
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn it_works() {
69        assert_eq!(version_str!(), "0.1.0");
70        assert_eq!(version_tuple!(), (0, 1, 0));
71    }
72}