Debian Codegraph @

src/target/env.rs

raw
1 //! Rust target environments
2
3 use crate::error::Error;
4 use core::{fmt, str::FromStr};
5
6 #[cfg(feature = "serde")]
7 use serde::{de, de::Error as DeError, ser, Deserialize, Serialize};
8
9 /// `target_env`: target environment that disambiguates the target platform by ABI / libc.
10 ///
11 /// This value is closely related to the fourth element of the platform target triple,
12 /// though it is not identical. For example, embedded ABIs such as `gnueabihf` will simply
13 /// define `target_env` as `"gnu"` (i.e. `target::Env::GNU`)
14 #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
15 #[non_exhaustive]
16 pub enum Env {
17 /// ``: None
18 None,
19
20 /// `gnu`: The GNU C Library (glibc)
21 Gnu,
22
23 /// `macabi`
24 Macabi,
25
26 /// `mlibc`
27 Mlibc,
28
29 /// `msvc`: Microsoft Visual C(++)
30 Msvc,
31
32 /// `musl`: Clean, efficient, standards-conformant libc implementation.
33 Musl,
34
35 /// `newlib`
36 Newlib,
37
38 /// `nto70`
39 Nto70,
40
41 /// `nto71`
42 Nto71,
43
44 /// `nto71_iosock`
45 Nto71Iosock,
46
47 /// `nto80`
48 Nto80,
49
50 /// `ohos`
51 OhOS,
52
53 /// `p1`
54 P1,
55
56 /// `p2`
57 P2,
58
59 /// `p3`
60 P3,
61
62 /// `relibc`
63 Relibc,
64
65 /// `sgx`: Intel Software Guard Extensions (SGX) Enclave
66 Sgx,
67
68 /// `sim`
69 Sim,
70
71 /// `uclibc`: C library for developing embedded Linux systems
72 UClibc,
73
74 /// `v5`
75 V5,
76 }
77
78 impl Env {
79 /// String representing this `Env` which matches `#[cfg(target_env)]`
80 pub fn as_str(self) -> &'static str {
81 match self {
82 Env::None => "",
83 Env::Gnu => "gnu",
84 Env::Macabi => "macabi",
85 Env::Mlibc => "mlibc",
86 Env::Msvc => "msvc",
87 Env::Musl => "musl",
88 Env::Newlib => "newlib",
89 Env::Nto70 => "nto70",
90 Env::Nto71 => "nto71",
91 Env::Nto71Iosock => "nto71_iosock",
92 Env::Nto80 => "nto80",
93 Env::OhOS => "ohos",
94 Env::P1 => "p1",
95 Env::P2 => "p2",
96 Env::P3 => "p3",
97 Env::Relibc => "relibc",
98 Env::Sgx => "sgx",
99 Env::Sim => "sim",
100 Env::UClibc => "uclibc",
101 Env::V5 => "v5",
102 }
103 }
104 }
105
106 impl FromStr for Env {
107 type Err = Error;
108
109 /// Create a new `Env` from the given string
110 fn from_str(name: &str) -> Result<Self, Self::Err> {
111 let result = match name {
112 "" => Env::None,
113 "gnu" => Env::Gnu,
114 "macabi" => Env::Macabi,
115 "mlibc" => Env::Mlibc,
116 "msvc" => Env::Msvc,
117 "musl" => Env::Musl,
118 "newlib" => Env::Newlib,
119 "nto70" => Env::Nto70,
120 "nto71" => Env::Nto71,
121 "nto71_iosock" => Env::Nto71Iosock,
122 "nto80" => Env::Nto80,
123 "ohos" => Env::OhOS,
124 "p1" => Env::P1,
125 "p2" => Env::P2,
126 "p3" => Env::P3,
127 "relibc" => Env::Relibc,
128 "sgx" => Env::Sgx,
129 "sim" => Env::Sim,
130 "uclibc" => Env::UClibc,
131 "v5" => Env::V5,
132 _ => return Err(Error),
133 };
134
135 Ok(result)
136 }
137 }
138
139 impl fmt::Display for Env {
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 f.write_str(self.as_str())
142 }
143 }
144
145 #[cfg(feature = "serde")]
146 impl Serialize for Env {
147 fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
148 serializer.serialize_str(self.as_str())
149 }
150 }
151
152 #[cfg(all(feature = "serde", feature = "std"))]
153 impl<'de> Deserialize<'de> for Env {
154 fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
155 let string = std::string::String::deserialize(deserializer)?;
156 string.parse().map_err(|_| {
157 D::Error::custom(std::format!(
158 "Unrecognized value '{}' for target_env",
159 string
160 ))
161 })
162 }
163 }