aws_types/
build_metadata.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Metadata type used by all service SDKs to provide information about the environment the SDK was compiled for.
7
8include!(concat!(env!("OUT_DIR"), "/build_env.rs"));
9
10/// Metadata about the environment the SDK was compiled for.
11#[derive(Debug)]
12pub struct BuildMetadata {
13    /// Which version of Rust was used to compile the SDK.
14    pub rust_version: &'static str,
15    /// The version number of the `aws-types` crate compiled with the SDK.
16    pub core_pkg_version: &'static str,
17    /// The OS the SDK was compiled for.
18    pub os_family: OsFamily,
19}
20
21/// Operating system family that the SDK can be compiled for
22#[derive(PartialEq, Eq, Clone, Debug)]
23pub enum OsFamily {
24    /// Microsoft Windows
25    Windows,
26    /// Linux
27    Linux,
28    /// Apple MacOS
29    Macos,
30    /// Google Android
31    Android,
32    /// Apple iOS
33    Ios,
34    /// A different family of operating system
35    Other,
36}
37
38/// Check the current OS against a list of options
39///
40/// You can't directly inspect the value of a `cfg` param. Instead, you need to check if the param
41/// is set to a specific value. This macro simplifies checking the current OS family.
42///
43/// Usage:
44/// ```ignore
45/// let os = get_os_family!(target_os: ("linux", OsFamily::Windows), ("android", OsFamily::Android));
46/// ```
47macro_rules! get_os_family {
48    ($i:ident : $(($s:expr, $t:expr)),+) => (
49        { const fn __getcfg() -> OsFamily { $( if cfg!($i=$s) { return $t; } );+ OsFamily::Other } __getcfg()  }
50    )
51}
52
53impl OsFamily {
54    /// Returns the OS family this crate was compiled for.
55    pub const fn from_env() -> Self {
56        // values from https://doc.rust-lang.org/reference/conditional-compilation.html#target_os
57        get_os_family!(target_os:
58            ("windows", OsFamily::Windows),
59            ("macos", OsFamily::Macos),
60            ("ios", OsFamily::Ios),
61            ("linux", OsFamily::Linux),
62            ("android", OsFamily::Android)
63        )
64    }
65}
66
67impl From<&str> for OsFamily {
68    fn from(s: &str) -> Self {
69        match s {
70            "windows" => OsFamily::Windows,
71            "macos" => OsFamily::Macos,
72            "ios" => OsFamily::Ios,
73            "linux" => OsFamily::Linux,
74            "android" => OsFamily::Android,
75            _ => OsFamily::Other,
76        }
77    }
78}
79
80/// Constant build metadata for this crate.
81pub const BUILD_METADATA: BuildMetadata = BuildMetadata {
82    rust_version: RUST_VERSION,
83    core_pkg_version: env!("CARGO_PKG_VERSION"),
84    os_family: OsFamily::from_env(),
85};
86
87#[cfg(test)]
88mod test {
89    use crate::build_metadata::{OsFamily, BUILD_METADATA};
90
91    #[test]
92    fn valid_build_metadata() {
93        let meta = &BUILD_METADATA;
94        // obviously a slightly brittle test. Will be a small update for Rust 2.0 and GA :-)
95        assert!(meta.rust_version.starts_with("1."));
96        // In our release process towards GA, the package version could either be 0. or 1.
97        // so we need to make this `assert!` more flexible.
98        assert!(meta.core_pkg_version.starts_with("0.") || meta.core_pkg_version.starts_with("1."));
99        // quick sanity check that we're parsing common platforms correctly
100        if cfg!(target_os = "linux") {
101            assert_eq!(meta.os_family, OsFamily::Linux);
102        }
103        if cfg!(target_os = "macos") {
104            assert_eq!(meta.os_family, OsFamily::Macos);
105        }
106    }
107}