substrait/
version.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: Apache-2.0

//! Substrait version information.
//!
//! The contents of this module are auto-generated using `build.rs`. It is
//! included in the packaged crate, ignored by git, and automatically kept
//! in-sync.

use crate::proto::Version;

include!("../gen/version.in");

/// Returns the version of Substrait used to build this crate.
///
/// Note that this does not set [Version::producer]. See
/// [version_with_producer].
pub fn version() -> Version {
    Version {
        major_number: SUBSTRAIT_MAJOR_VERSION,
        minor_number: SUBSTRAIT_MINOR_VERSION,
        patch_number: SUBSTRAIT_PATCH_VERSION,
        git_hash: if SUBSTRAIT_GIT_DEPTH != 0 {
            String::from(SUBSTRAIT_GIT_SHA)
        } else {
            String::default()
        },
        ..Default::default()
    }
}

/// Returns the version of Substrait used to build this crate with
/// [Version::producer] set to the passed `producer`.
pub fn version_with_producer(producer: impl Into<String>) -> Version {
    Version {
        producer: producer.into(),
        ..version()
    }
}

/// Returns the semantic version of Substrait used to build this crate.
#[cfg(feature = "semver")]
pub fn semver() -> semver::Version {
    semver::Version {
        major: SUBSTRAIT_MAJOR_VERSION as _,
        minor: SUBSTRAIT_MINOR_VERSION as _,
        patch: SUBSTRAIT_PATCH_VERSION as _,
        pre: if SUBSTRAIT_GIT_DEPTH != 0 {
            semver::Prerelease::new(&SUBSTRAIT_GIT_DEPTH.to_string()).unwrap()
        } else {
            semver::Prerelease::EMPTY
        },
        build: semver::BuildMetadata::new(SUBSTRAIT_GIT_SHA).unwrap(),
    }
}

/// Returns the requirement of this crate for other Substrait versions.
#[cfg(feature = "semver")]
pub fn semver_req() -> semver::VersionReq {
    semver::VersionReq::parse(&format!("^{}", semver())).unwrap()
}

#[cfg(test)]
// These tests ensure this crate uses a tagged Substrait release.
mod tests {
    #[test]
    fn no_git_hash() {
        // An empty `git_hash` indicates that there are no additional commits
        // since the last tag.
        assert!(super::version().git_hash.is_empty());
    }

    #[test]
    #[allow(clippy::assertions_on_constants)]
    fn not_dirty() {
        // There should be no `dirty` in the describe output.
        assert!(!super::SUBSTRAIT_GIT_DESCRIBE.contains("dirty"));
        assert!(!super::SUBSTRAIT_GIT_DIRTY);
    }

    #[test]
    fn no_pre_release() {
        // The pre-release should be unset. If set it indicates additional
        // commits after the tag.
        #[cfg(feature = "semver")]
        assert!(super::semver().pre.is_empty());
        assert_eq!(super::SUBSTRAIT_GIT_DEPTH, 0);
    }
}