Skip to main content

substrait/
version.rs

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