substrait/
version.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Substrait version information.
4//!
5//! The contents of this module are auto-generated using `build.rs`. It is
6//! included in the packaged crate, ignored by git, and automatically kept
7//! in-sync.
8
9use crate::proto::Version;
10
11include!("../gen/version.in");
12
13/// Returns the version of Substrait used to build this crate.
14///
15/// Note that this does not set [Version::producer]. See
16/// [version_with_producer].
17pub fn version() -> Version {
18    Version {
19        major_number: SUBSTRAIT_MAJOR_VERSION,
20        minor_number: SUBSTRAIT_MINOR_VERSION,
21        patch_number: SUBSTRAIT_PATCH_VERSION,
22        git_hash: if SUBSTRAIT_GIT_DEPTH != 0 {
23            String::from(SUBSTRAIT_GIT_SHA)
24        } else {
25            String::default()
26        },
27        ..Default::default()
28    }
29}
30
31/// Returns the version of Substrait used to build this crate with
32/// [Version::producer] set to the passed `producer`.
33pub fn version_with_producer(producer: impl Into<String>) -> Version {
34    Version {
35        producer: producer.into(),
36        ..version()
37    }
38}
39
40/// Returns the semantic version of Substrait used to build this crate.
41#[cfg(feature = "semver")]
42pub fn semver() -> semver::Version {
43    semver::Version {
44        major: SUBSTRAIT_MAJOR_VERSION as _,
45        minor: SUBSTRAIT_MINOR_VERSION as _,
46        patch: SUBSTRAIT_PATCH_VERSION as _,
47        pre: if SUBSTRAIT_GIT_DEPTH != 0 {
48            semver::Prerelease::new(&SUBSTRAIT_GIT_DEPTH.to_string()).unwrap()
49        } else {
50            semver::Prerelease::EMPTY
51        },
52        build: semver::BuildMetadata::new(SUBSTRAIT_GIT_SHA).unwrap(),
53    }
54}
55
56/// Returns the requirement of this crate for other Substrait versions.
57#[cfg(feature = "semver")]
58pub fn semver_req() -> semver::VersionReq {
59    semver::VersionReq::parse(&format!("^{}", semver())).unwrap()
60}
61
62#[cfg(test)]
63// These tests ensure this crate uses a tagged Substrait release.
64mod tests {
65    #[test]
66    fn no_git_hash() {
67        // An empty `git_hash` indicates that there are no additional commits
68        // since the last tag.
69        assert!(super::version().git_hash.is_empty());
70    }
71
72    #[test]
73    #[allow(clippy::assertions_on_constants)]
74    fn not_dirty() {
75        // There should be no `dirty` in the describe output.
76        assert!(!super::SUBSTRAIT_GIT_DESCRIBE.contains("dirty"));
77        assert!(!super::SUBSTRAIT_GIT_DIRTY);
78    }
79
80    #[test]
81    fn no_pre_release() {
82        // The pre-release should be unset. If set it indicates additional
83        // commits after the tag.
84        #[cfg(feature = "semver")]
85        assert!(super::semver().pre.is_empty());
86        assert_eq!(super::SUBSTRAIT_GIT_DEPTH, 0);
87    }
88}