build_target/
family.rs

1use std::fmt;
2
3use crate::utils::{build_env_opt, define_target_enum};
4
5define_target_enum! {
6    /// A more generic description of a target, such as the family of
7    /// the operating systems or architectures that the target generally falls into.
8    #[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
9    #[non_exhaustive]
10    pub enum Family {
11        /// Unix based operating systems
12        Unix => "unix",
13        /// Microsoft's Windows operating system
14        Windows => "windows",
15        /// WebAssembly
16        Wasm => "wasm",
17    }
18
19    as_str_doc = "String representing this target family which matches `#[cfg(target_family)]`",
20    from_str_doc = "Tries to parse the given string as an [`Family`] falling back to [`Family::Other`] for unknown values.",
21}
22
23impl Family {
24    /// Gets the current target [`Family`]s.
25    pub fn target() -> Vec<Self> {
26        build_env_opt("CARGO_CFG_TARGET_FAMILY")
27            .unwrap_or_default()
28            .split(',')
29            .filter(|s| !s.is_empty())
30            .map(Self::from_str)
31            .collect()
32    }
33}
34
35impl fmt::Display for Family {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        f.write_str(self.as_str())
38    }
39}