Skip to content

Commit

Permalink
Make zarrs::version::version_{major,minor,patch} const
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Nov 16, 2024
1 parent 72864e4 commit 01aaafb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- Make `zarrs::version::version_{major,minor,patch}` const

## [0.18.0-beta.0] - 2024-11-15

### Highlights
Expand Down
24 changes: 24 additions & 0 deletions zarrs/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![allow(missing_docs)]

use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;

fn main() {
let major_version = env::var("CARGO_PKG_VERSION_MAJOR").unwrap();
let minor_version = env::var("CARGO_PKG_VERSION_MINOR").unwrap();
let patch_version = env::var("CARGO_PKG_VERSION_PATCH").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("version.rs");

let mut file = fs::File::create(dest_path).unwrap();
file.write_fmt(format_args!(
r"
pub(crate) const VERSION_MAJOR: u32 = {major_version};
pub(crate) const VERSION_MINOR: u32 = {minor_version};
pub(crate) const VERSION_PATCH: u32 = {patch_version};
"
))
.unwrap();
}
20 changes: 8 additions & 12 deletions zarrs/src/version.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
//! `zarrs` version information.

include!(concat!(env!("OUT_DIR"), "/version.rs"));

/// The `zarrs` major version.
#[must_use]
pub fn version_major() -> u32 {
const VERSION_MAJOR: &str = env!("CARGO_PKG_VERSION_MAJOR");
let major: u32 = VERSION_MAJOR.parse::<u32>().unwrap_or_default();
major
pub const fn version_major() -> u32 {
VERSION_MAJOR
}

/// The `zarrs` minor version.
#[must_use]
pub fn version_minor() -> u32 {
const VERSION_MINOR: &str = env!("CARGO_PKG_VERSION_MINOR");
let minor: u32 = VERSION_MINOR.parse::<u32>().unwrap_or_default();
minor
pub const fn version_minor() -> u32 {
VERSION_MINOR
}

/// The `zarrs` patch version.
#[must_use]
pub fn version_patch() -> u32 {
const VERSION_PATCH: &str = env!("CARGO_PKG_VERSION_PATCH");
let patch: u32 = VERSION_PATCH.parse::<u32>().unwrap_or_default();
patch
pub const fn version_patch() -> u32 {
VERSION_PATCH
}

/// A [`u32`] representation of the `zarrs` version.
Expand Down

0 comments on commit 01aaafb

Please sign in to comment.