apple_platforms/
platform.rs

1// https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/BinaryFormat/MachO.def#L123-L138
2
3// PLATFORM(platform, id, name, build_name, target, tapi_target, marketing)
4// PLATFORM(UNKNOWN, 0, unknown, unknown, unknown, unknown, unknown)
5// PLATFORM(MACOS, 1, macos, macos, macos, macos, macOS)
6// PLATFORM(IOS, 2, ios, ios, ios, ios, iOS)
7// PLATFORM(TVOS, 3, tvos, tvos, tvos, tvos, tvOS)
8// PLATFORM(WATCHOS, 4, watchos, watchos, watchos, watchos, watchOS)
9// PLATFORM(BRIDGEOS, 5, bridgeos, bridgeos, bridgeos, bridgeos, bridgeOS)
10// PLATFORM(MACCATALYST, 6, macCatalyst, macCatalyst, ios-macabi, maccatalyst, macCatalyst)
11// PLATFORM(IOSSIMULATOR, 7, iossimulator, iossimulator, ios-simulator, ios-simulator, iOS Simulator)
12// PLATFORM(TVOSSIMULATOR, 8, tvossimulator, tvossimulator, tvos-simulator, tvos-simulator, tvOS Simulator)
13// PLATFORM(WATCHOSSIMULATOR, 9, watchossimulator, watchossimulator, watchos-simulator, watchos-simulator, watchOS Simulator)
14// PLATFORM(DRIVERKIT, 10, driverkit, driverkit, driverkit, driverkit, DriverKit)
15// PLATFORM(XROS, 11, xros, xros, xros, xros, xrOS)
16// PLATFORM(XROS_SIMULATOR, 12, xrsimulator, xrsimulator, xrsimulator, xros-simulator, xrOS Simulator)
17
18use std::fmt;
19
20#[derive(Clone, Copy)]
21pub struct Platform<'a> {
22    platform: &'a str,
23    id: usize,
24    name: &'a str,
25    build_name: &'a str,
26    target: &'a str,
27    tapi_target: &'a str,
28    marketing: &'a str,
29}
30
31impl Default for Platform<'static> {
32    fn default() -> Self {
33        Self {
34            platform: "unknown",
35            id: 0,
36            name: "unknown",
37            build_name: "unknown",
38            target: "unknown",
39            tapi_target: "unknown",
40            marketing: "unknown",
41        }
42    }
43}
44
45impl fmt::Debug for Platform<'static> {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        f.debug_struct("Platform")
48            .field("platform", &self.platform)
49            .field("id", &self.id)
50            .field("name", &self.name)
51            .field("build_name", &self.build_name)
52            .field("target", &self.target)
53            .field("tapi_target", &self.tapi_target)
54            .field("marketing", &self.marketing)
55            .finish()
56    }
57}
58
59#[derive(Clone, Copy)]
60pub enum ApplePlatform {
61    UNKNOWN,
62    MACOS,
63    IOS,
64    TVOS,
65    WATCHOS,
66    BRIDGEOS,
67    MACCATALYST,
68    IOSSIMULATOR,
69    TVOSSIMULATOR,
70    WATCHOSSIMULATOR,
71    DRIVERKIT,
72    XROS,
73    #[allow(non_camel_case_types)]
74    XROS_SIMULATOR,
75}
76
77impl Default for ApplePlatform {
78    fn default() -> Self {
79        ApplePlatform::UNKNOWN
80    }
81}
82
83impl fmt::Debug for ApplePlatform {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        let platform = Platform::try_from(*self).unwrap();
86        write!(f, "{}", platform.platform)
87    }
88}
89
90impl ApplePlatform {
91    pub fn into_iter() -> std::array::IntoIter<ApplePlatform, 13> {
92        [
93            ApplePlatform::UNKNOWN,
94            ApplePlatform::MACOS,
95            ApplePlatform::IOS,
96            ApplePlatform::TVOS,
97            ApplePlatform::WATCHOS,
98            ApplePlatform::BRIDGEOS,
99            ApplePlatform::MACCATALYST,
100            ApplePlatform::IOSSIMULATOR,
101            ApplePlatform::TVOSSIMULATOR,
102            ApplePlatform::WATCHOSSIMULATOR,
103            ApplePlatform::DRIVERKIT,
104            ApplePlatform::XROS,
105            ApplePlatform::XROS_SIMULATOR,
106        ]
107        .into_iter()
108    }
109}
110
111impl TryFrom<ApplePlatform> for Platform<'_> {
112    type Error = ();
113
114    fn try_from(value: ApplePlatform) -> Result<Self, Self::Error> {
115        Ok(match value {
116            ApplePlatform::UNKNOWN => Platform {
117                platform: "UNKNOWN",
118                id: 0,
119                name: "unknown",
120                build_name: "unknown",
121                target: "unknown",
122                tapi_target: "unknown",
123                marketing: "unknown",
124            },
125            ApplePlatform::MACOS => Platform {
126                platform: "MACOS",
127                id: 1,
128                name: "macos",
129                build_name: "macos",
130                target: "macos",
131                tapi_target: "macos",
132                marketing: "macOS",
133            },
134            ApplePlatform::IOS => Platform {
135                platform: "IOS",
136                id: 2,
137                name: "ios",
138                build_name: "ios",
139                target: "ios",
140                tapi_target: "ios",
141                marketing: "iOS",
142            },
143            ApplePlatform::TVOS => Platform {
144                platform: "TVOS",
145                id: 3,
146                name: "tvos",
147                build_name: "tvos",
148                target: "tvos",
149                tapi_target: "tvos",
150                marketing: "tvOS",
151            },
152            ApplePlatform::WATCHOS => Platform {
153                platform: "WATCHOS",
154                id: 4,
155                name: "watchos",
156                build_name: "watchos",
157                target: "watchos",
158                tapi_target: "watchos",
159                marketing: "watchOS",
160            },
161            ApplePlatform::BRIDGEOS => Platform {
162                platform: "BRIDGEOS",
163                id: 5,
164                name: "bridgeos",
165                build_name: "bridgeos",
166                target: "bridgeos",
167                tapi_target: "bridgeos",
168                marketing: "bridgeOS",
169            },
170            ApplePlatform::MACCATALYST => Platform {
171                platform: "MACCATALYST",
172                id: 6,
173                name: "macCatalyst",
174                build_name: "macCatalyst",
175                target: "ios-macabi",
176                tapi_target: "ios-macabi",
177                marketing: "macCatalyst",
178            },
179            ApplePlatform::IOSSIMULATOR => Platform {
180                platform: "IOSSIMULATOR",
181                id: 7,
182                name: "iossimulator",
183                build_name: "iossimulator",
184                target: "ios-simulator",
185                tapi_target: "ios-simulator",
186                marketing: "iOS Simulator",
187            },
188            ApplePlatform::TVOSSIMULATOR => Platform {
189                platform: "TVOSSIMULATOR",
190                id: 8,
191                name: "tvossimulator",
192                build_name: "tvossimulator",
193                target: "tvos-simulator",
194                tapi_target: "tvos-simulator",
195                marketing: "tvOS Simulator",
196            },
197            ApplePlatform::WATCHOSSIMULATOR => Platform {
198                platform: "WATCHOSSIMULATOR",
199                id: 9,
200                name: "watchossimulator",
201                build_name: "watchossimulator",
202                target: "watchos-simulator",
203                tapi_target: "watchos-simulator",
204                marketing: "watchOS Simulator",
205            },
206            ApplePlatform::DRIVERKIT => Platform {
207                platform: "DRIVERKIT",
208                id: 10,
209                name: "driverkit",
210                build_name: "driverkit",
211                target: "driverkit",
212                tapi_target: "driverkit",
213                marketing: "DriverKit",
214            },
215            ApplePlatform::XROS => Platform {
216                platform: "XROS",
217                id: 11,
218                name: "xros",
219                build_name: "xros",
220                target: "xros",
221                tapi_target: "xros",
222                marketing: "xrOS",
223            },
224            ApplePlatform::XROS_SIMULATOR => Platform {
225                platform: "XROS_SIMULATOR",
226                id: 12,
227                name: "xrsimulator",
228                build_name: "xrsimulator",
229                target: "xrsimulator",
230                tapi_target: "xros-simulator",
231                marketing: "Simulator",
232            },
233        })
234    }
235}
236
237impl Into<ApplePlatform> for Platform<'static> {
238    fn into(self) -> ApplePlatform {
239        match self.platform {
240            "UNKNOWN" => ApplePlatform::UNKNOWN,
241            "MACOS" => ApplePlatform::MACOS,
242            "IOS" => ApplePlatform::IOS,
243            "TVOS" => ApplePlatform::TVOS,
244            "WATCHOS" => ApplePlatform::WATCHOS,
245            "BRIDGEOS" => ApplePlatform::BRIDGEOS,
246            "MACCATALYST" => ApplePlatform::MACCATALYST,
247            "IOSSIMULATOR" => ApplePlatform::IOSSIMULATOR,
248            "TVOSSIMULATOR" => ApplePlatform::TVOSSIMULATOR,
249            "WATCHOSSIMULATOR" => ApplePlatform::WATCHOSSIMULATOR,
250            "DRIVERKIT" => ApplePlatform::DRIVERKIT,
251            "XROS" => ApplePlatform::XROS,
252            "XROS_SIMULATOR" => ApplePlatform::XROS_SIMULATOR,
253            _ => ApplePlatform::UNKNOWN,
254        }
255    }
256}
257
258#[cfg(test)]
259mod tests {
260    use super::*;
261
262    #[test]
263    fn all_platforms() {
264        let platforms = ApplePlatform::into_iter()
265            .map(|platform| platform)
266            .collect::<Vec<_>>();
267        assert_eq!(
268            format!("{:?}", platforms),
269            "[UNKNOWN, MACOS, IOS, TVOS, WATCHOS, BRIDGEOS, MACCATALYST, IOSSIMULATOR, TVOSSIMULATOR, WATCHOSSIMULATOR, DRIVERKIT, XROS, XROS_SIMULATOR]"
270        )
271    }
272}