const_pkg_version/lib.rs
1//! Constant expressions for your package version.
2//!
3//! # Example
4//!
5//! ```
6//! use const_pkg_version::Version;
7//! const VERSION_FULL: Version = const_pkg_version::version!();
8//!
9//! const VERSION_MAJOR: u32 = const_pkg_version::major!();
10//! const VERSION_MINOR: u32 = const_pkg_version::minor!();
11//! const VERSION_PATCH: u32 = const_pkg_version::patch!();
12//! const VERSION_PRE: Option<&str> = const_pkg_version::pre_release!();
13//! const BUILD_METADATA: Option<&str> = const_pkg_version::build_metadata!();
14//! ```
15//!
16//! # Features
17//!
18//! * `debug`: Implements [`Debug`] for [`Version`] (enabled by default).
19//! * `defmt`: Implements [`defmt::Format`] for [`Version`].
20//! * `serde`: Implements [`serde::Deserialize`] and [`serde::Serialize`] for [`Version`].
21//! * `semver`: Implements [`TryFrom<crate::Version>`] for [`semver::Version`].
22#![no_std]
23#![warn(missing_docs)]
24
25#[doc(hidden)]
26pub mod __reexports {
27 pub use const_pkg_version_macros as macros;
28}
29
30/// A full package version (major, minor, patch, pre-release and build-metadata).
31#[derive(Copy, Clone, Eq, PartialEq)]
32#[cfg_attr(feature = "debug", derive(Debug))]
33#[cfg_attr(feature = "defmt", derive(defmt::Format))]
34#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
35pub struct Version<'a> {
36 /// The major version of the package.
37 pub major: u32,
38
39 /// The minor version of the package.
40 pub minor: u32,
41
42 /// The patch version of the package.
43 pub patch: u32,
44
45 /// The pre-release version of the package.
46 pub pre_release: Option<&'a str>,
47
48 /// The build metadata of the package.
49 pub build_metadata: Option<&'a str>,
50}
51
52/// Get the major version of your package in a constant expression.
53///
54/// # Example
55/// ```
56/// const VERSION_MAJOR: u32 = const_pkg_version::major!();
57/// ```
58///
59/// You can also limit the value to something lower than a `u32`.
60/// This will generate a compile error if the version is too high:
61/// ```
62/// const VERSION_MAJOR: u8 = const_pkg_version::major!();
63/// ```
64#[macro_export]
65macro_rules! major {
66 () => {
67 $crate::__reexports::macros::major!($crate)
68 }
69}
70
71/// Get the minor version of your package in a constant expression.
72///
73/// # Example
74/// ```
75/// const VERSION_MINOR: u32 = const_pkg_version::minor!();
76/// ```
77///
78/// You can also limit the value to something lower than a `u32`.
79/// This will generate a compile error if the version is too high:
80/// ```
81/// const VERSION_MINOR: u8 = const_pkg_version::minor!();
82/// ```
83#[macro_export]
84macro_rules! minor {
85 () => {
86 $crate::__reexports::macros::minor!($crate)
87 }
88}
89
90/// Get the patch version of your package in a constant expression.
91///
92/// # Example
93/// ```
94/// const VERSION_PATCH: u32 = const_pkg_version::patch!();
95/// ```
96///
97/// You can also limit the value to something lower than a `u32`.
98/// This will generate a compile error if the version is too high:
99/// ```
100/// const VERSION_PATCH: u8 = const_pkg_version::patch!();
101/// ```
102#[macro_export]
103macro_rules! patch {
104 () => {
105 $crate::__reexports::macros::patch!($crate)
106 }
107}
108
109#[macro_export]
110#[deprecated(note = "who are you calling a p'takh?")]
111#[doc(hidden)]
112macro_rules! ptach {
113 () => {
114 $crate::__reexports::macros::patch!($crate)
115 }
116}
117
118/// Get the pre-release version of your package in a constant expression.
119///
120/// Evaluates to `Some(str)` if your package has a pre-release version.
121/// Evaluates to `None` otherwise.
122///
123/// # Example
124/// ```
125/// const VERSION_PRE: Option<&str> = const_pkg_version::pre_release!();
126/// ```
127#[macro_export]
128macro_rules! pre_release {
129 () => {
130 $crate::__reexports::macros::pre_release!($crate)
131 }
132}
133
134/// Get the build metadata of your package in a constant expression.
135///
136/// Evaluates to `Some(str)` if your package has a build metadata string in the version.
137/// Evaluates to `None` otherwise.
138///
139/// # Example
140/// ```
141/// const VERSION_PRE: Option<&str> = const_pkg_version::pre_release!();
142/// ```
143#[macro_export]
144macro_rules! build_metadata {
145 () => {
146 $crate::__reexports::macros::build_metadata!($crate)
147 }
148}
149
150/// Get the full version information of your package in a constant expression.
151///
152/// Evaluates to a [`Version`] object.
153///
154/// # Example
155/// ```
156/// use const_pkg_version::Version;
157/// const VERSION_FULL: Version = const_pkg_version::version!();
158/// ```
159#[macro_export]
160macro_rules! version {
161 () => {
162 $crate::__reexports::macros::full!($crate)
163 }
164}
165
166#[cfg(feature = "semver")]
167impl<'a> TryFrom<Version<'a>> for semver::Version {
168 type Error = semver::Error;
169
170 fn try_from(input: Version<'a>) -> Result<Self, Self::Error> {
171 Self::try_from(&input)
172 }
173}
174
175#[cfg(feature = "semver")]
176impl TryFrom<&Version<'_>> for semver::Version {
177 type Error = semver::Error;
178
179 fn try_from(input: &Version<'_>) -> Result<Self, Self::Error> {
180 Ok(Self {
181 major: input.major.into(),
182 minor: input.minor.into(),
183 patch: input.patch.into(),
184 pre: input.pre_release.unwrap_or("").parse()?,
185 build: input.build_metadata.unwrap_or("").parse()?,
186 })
187 }
188}