1use itertools::Itertools;
2
3use std::{cmp::Ordering, collections::HashMap};
4use strum::IntoEnumIterator;
5use strum_macros::EnumIter;
6use thiserror::Error;
7
8use serde::{
9 de::{self, DeserializeOwned, IntoDeserializer, Visitor},
10 Deserialize, Deserializer,
11};
12use serde_enum_str::{Deserialize_enum_str, Serialize_enum_str};
13
14use super::{normalize_lua_value, DisplayAsLuaKV, DisplayLuaKV, DisplayLuaValue, LuaValueSeed};
15
16#[derive(Deserialize_enum_str, Serialize_enum_str, PartialEq, Eq, Hash, Debug, Clone, EnumIter)]
19#[serde(rename_all = "lowercase")]
20#[strum(serialize_all = "lowercase")]
21pub enum PlatformIdentifier {
22 Unix,
24 Windows,
25 Win32,
26 Cygwin,
27 MacOSX,
28 Linux,
29 FreeBSD,
30 #[serde(other)]
31 Unknown(String),
32}
33
34impl Default for PlatformIdentifier {
35 fn default() -> Self {
36 target_identifier()
37 }
38}
39
40impl PartialOrd for PlatformIdentifier {
42 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
43 match (self, other) {
44 (PlatformIdentifier::Unix, PlatformIdentifier::Cygwin) => Some(Ordering::Less),
45 (PlatformIdentifier::Unix, PlatformIdentifier::MacOSX) => Some(Ordering::Less),
46 (PlatformIdentifier::Unix, PlatformIdentifier::Linux) => Some(Ordering::Less),
47 (PlatformIdentifier::Unix, PlatformIdentifier::FreeBSD) => Some(Ordering::Less),
48 (PlatformIdentifier::Windows, PlatformIdentifier::Win32) => Some(Ordering::Greater),
49 (PlatformIdentifier::Win32, PlatformIdentifier::Windows) => Some(Ordering::Less),
50 (PlatformIdentifier::Cygwin, PlatformIdentifier::Unix) => Some(Ordering::Greater),
51 (PlatformIdentifier::MacOSX, PlatformIdentifier::Unix) => Some(Ordering::Greater),
52 (PlatformIdentifier::Linux, PlatformIdentifier::Unix) => Some(Ordering::Greater),
53 (PlatformIdentifier::FreeBSD, PlatformIdentifier::Unix) => Some(Ordering::Greater),
54 _ if self == other => Some(Ordering::Equal),
55 _ => None,
56 }
57 }
58}
59
60fn target_identifier() -> PlatformIdentifier {
67 if cfg!(target_env = "msvc") {
68 PlatformIdentifier::Windows
69 } else if cfg!(target_os = "linux") {
70 PlatformIdentifier::Linux
71 } else if cfg!(target_os = "macos") || cfg!(target_vendor = "apple") {
72 PlatformIdentifier::MacOSX
73 } else if cfg!(target_os = "freebsd") {
74 PlatformIdentifier::FreeBSD
75 } else if which::which("cygpath").is_ok() {
76 PlatformIdentifier::Cygwin
77 } else {
78 PlatformIdentifier::Unix
79 }
80}
81
82impl PlatformIdentifier {
83 pub fn get_subsets(&self) -> Vec<Self> {
86 PlatformIdentifier::iter()
87 .filter(|identifier| identifier.is_subset_of(self))
88 .collect()
89 }
90
91 pub fn get_extended_platforms(&self) -> Vec<Self> {
94 PlatformIdentifier::iter()
95 .filter(|identifier| identifier.is_extension_of(self))
96 .collect()
97 }
98
99 fn is_subset_of(&self, other: &PlatformIdentifier) -> bool {
101 self.partial_cmp(other) == Some(Ordering::Less)
102 }
103
104 fn is_extension_of(&self, other: &PlatformIdentifier) -> bool {
106 self.partial_cmp(other) == Some(Ordering::Greater)
107 }
108}
109
110#[derive(Clone, Debug, PartialEq)]
112pub struct PlatformSupport {
113 platform_map: HashMap<PlatformIdentifier, bool>,
115}
116
117impl Default for PlatformSupport {
118 fn default() -> Self {
119 Self {
120 platform_map: PlatformIdentifier::iter()
121 .filter(|identifier| !matches!(identifier, PlatformIdentifier::Unknown(_)))
122 .map(|identifier| (identifier, true))
123 .collect(),
124 }
125 }
126}
127
128impl<'de> Deserialize<'de> for PlatformSupport {
129 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
130 where
131 D: Deserializer<'de>,
132 {
133 let platforms: Vec<String> = Vec::deserialize(deserializer)?;
134 Self::parse(&platforms).map_err(de::Error::custom)
135 }
136}
137
138impl DisplayAsLuaKV for PlatformSupport {
139 fn display_lua(&self) -> DisplayLuaKV {
140 DisplayLuaKV {
141 key: "supported_platforms".to_string(),
142 value: DisplayLuaValue::List(
143 self.platforms()
144 .iter()
145 .map(|(platform, supported)| {
146 DisplayLuaValue::String(format!(
147 "{}{}",
148 if *supported { "" } else { "!" },
149 platform,
150 ))
151 })
152 .collect(),
153 ),
154 }
155 }
156}
157
158#[derive(Error, Debug)]
159pub enum PlatformValidationError {
160 #[error("error when parsing platform identifier: {0}")]
161 ParseError(String),
162
163 #[error("conflicting supported platform entries")]
164 ConflictingEntries,
165}
166
167impl PlatformSupport {
168 fn validate_platforms(
169 platforms: &[String],
170 ) -> Result<HashMap<PlatformIdentifier, bool>, PlatformValidationError> {
171 platforms
172 .iter()
173 .try_fold(HashMap::new(), |mut platforms, platform| {
174 let (is_positive_assertion, platform) = platform
178 .strip_prefix('!')
179 .map(|str| (false, str))
180 .unwrap_or((true, platform));
181
182 let platform_identifier = platform
183 .parse::<PlatformIdentifier>()
184 .map_err(|err| PlatformValidationError::ParseError(err.to_string()))?;
185
186 if platforms
190 .get(&platform_identifier)
191 .unwrap_or(&is_positive_assertion)
192 != &is_positive_assertion
193 {
194 return Err(PlatformValidationError::ConflictingEntries);
195 }
196
197 platforms.insert(platform_identifier.clone(), is_positive_assertion);
198
199 let subset_or_extended_platforms = if is_positive_assertion {
200 platform_identifier.get_extended_platforms()
201 } else {
202 platform_identifier.get_subsets()
203 };
204
205 for sub_platform in subset_or_extended_platforms {
206 if platforms
207 .get(&sub_platform)
208 .unwrap_or(&is_positive_assertion)
209 != &is_positive_assertion
210 {
211 return Err(PlatformValidationError::ConflictingEntries);
213 }
214
215 platforms.insert(sub_platform, is_positive_assertion);
216 }
217
218 Ok(platforms)
219 })
220 }
221
222 pub fn parse(platforms: &[String]) -> Result<Self, PlatformValidationError> {
223 match platforms {
227 [] => Ok(Self::default()),
228 platforms if platforms.iter().any(|platform| platform.starts_with('!')) => {
229 let mut platform_map = Self::validate_platforms(platforms)?;
230
231 for identifier in PlatformIdentifier::iter() {
234 if !matches!(identifier, PlatformIdentifier::Unknown(_)) {
235 platform_map.entry(identifier).or_insert(true);
236 }
237 }
238
239 Ok(Self { platform_map })
240 }
241 platforms => Ok(Self {
243 platform_map: Self::validate_platforms(platforms)?,
244 }),
245 }
246 }
247
248 pub fn is_supported(&self, platform: &PlatformIdentifier) -> bool {
249 self.platform_map.get(platform).cloned().unwrap_or(false)
250 }
251
252 pub(crate) fn platforms(&self) -> &HashMap<PlatformIdentifier, bool> {
253 &self.platform_map
254 }
255}
256
257pub trait PartialOverride: Sized {
258 type Err: std::error::Error;
259
260 fn apply_overrides(&self, override_val: &Self) -> Result<Self, Self::Err>;
261}
262
263pub trait PlatformOverridable: PartialOverride {
264 type Err: std::error::Error;
265
266 fn on_nil<T>() -> Result<PerPlatform<T>, <Self as PlatformOverridable>::Err>
267 where
268 T: PlatformOverridable,
269 T: Default;
270}
271
272#[derive(Clone, Debug, PartialEq)]
274pub struct PerPlatform<T> {
275 pub(crate) default: T,
277 pub(crate) per_platform: HashMap<PlatformIdentifier, T>,
279}
280
281impl<T> PerPlatform<T> {
282 pub(crate) fn new(default: T) -> Self {
283 Self {
284 default,
285 per_platform: HashMap::default(),
286 }
287 }
288
289 pub fn current_platform(&self) -> &T {
292 self.for_platform_identifier(&target_identifier())
293 }
294
295 fn for_platform_identifier(&self, identifier: &PlatformIdentifier) -> &T {
296 self.get(identifier)
297 }
298
299 pub fn get(&self, platform: &PlatformIdentifier) -> &T {
300 self.per_platform.get(platform).unwrap_or(
301 platform
302 .get_subsets()
303 .into_iter()
304 .sorted_by(|a, b| b.partial_cmp(a).unwrap_or(Ordering::Equal))
308 .find(|identifier| self.per_platform.contains_key(identifier))
309 .and_then(|identifier| self.per_platform.get(&identifier))
310 .unwrap_or(&self.default),
311 )
312 }
313
314 pub(crate) fn map<U, F>(&self, cb: F) -> PerPlatform<U>
315 where
316 F: Fn(&T) -> U,
317 {
318 PerPlatform {
319 default: cb(&self.default),
320 per_platform: self
321 .per_platform
322 .iter()
323 .map(|(identifier, value)| (identifier.clone(), cb(value)))
324 .collect(),
325 }
326 }
327}
328
329impl<U, E> PerPlatform<Result<U, E>>
330where
331 E: std::error::Error,
332{
333 pub fn transpose(self) -> Result<PerPlatform<U>, E> {
334 Ok(PerPlatform {
335 default: self.default?,
336 per_platform: self
337 .per_platform
338 .into_iter()
339 .map(|(identifier, value)| Ok((identifier, value?)))
340 .try_collect()?,
341 })
342 }
343}
344
345impl<T: Default> Default for PerPlatform<T> {
346 fn default() -> Self {
347 Self {
348 default: T::default(),
349 per_platform: HashMap::default(),
350 }
351 }
352}
353
354struct PerPlatformVisitor<T>(std::marker::PhantomData<T>);
355
356impl<'de, T> Visitor<'de> for PerPlatformVisitor<T>
357where
358 T: DeserializeOwned,
359 T: PlatformOverridable,
360 T: Default,
361 T: Clone,
362{
363 type Value = PerPlatform<T>;
364
365 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
366 formatter.write_str("a table or nil")
367 }
368
369 fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
370 where
371 A: de::MapAccess<'de>,
372 {
373 use serde_value::Value;
374
375 let mut platforms_val: Option<Value> = None;
376 let mut other_entries: Vec<(Value, Value)> = Vec::new();
377
378 while let Some(key) = map.next_key_seed(LuaValueSeed)? {
379 if key == Value::String("platforms".to_string()) {
380 platforms_val = Some(map.next_value_seed(LuaValueSeed)?);
381 } else {
382 other_entries.push((key, map.next_value_seed(LuaValueSeed)?));
383 }
384 }
385
386 let mut per_platform = match platforms_val {
387 Some(val) => match val {
388 Value::Map(_) => val
389 .deserialize_into::<HashMap<PlatformIdentifier, T>>()
390 .map_err(de::Error::custom)?,
391 Value::Unit => HashMap::default(),
392 val => {
393 return Err(de::Error::custom(format!(
394 "Expected platforms to be a table or nil, but got {val:?}",
395 )))
396 }
397 },
398 None => HashMap::default(),
399 };
400
401 let obj = normalize_lua_value(Value::Map(other_entries.into_iter().collect()));
405 let default = T::deserialize(obj.into_deserializer()).map_err(de::Error::custom)?;
406 apply_per_platform_overrides(&mut per_platform, &default)
407 .map_err(|err: <T as PartialOverride>::Err| de::Error::custom(err.to_string()))?;
408 Ok(PerPlatform {
409 default,
410 per_platform,
411 })
412 }
413
414 fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
415 where
416 A: de::SeqAccess<'de>,
417 {
418 let default = T::deserialize(de::value::SeqAccessDeserializer::new(seq))?;
422 Ok(PerPlatform::new(default))
423 }
424
425 fn visit_unit<E>(self) -> Result<Self::Value, E>
426 where
427 E: de::Error,
428 {
429 T::on_nil().map_err(de::Error::custom)
430 }
431
432 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
433 where
434 E: de::Error,
435 {
436 let s = std::str::from_utf8(v).map_err(de::Error::custom)?;
437 self.visit_str(s)
438 }
439
440 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
441 where
442 E: de::Error,
443 {
444 self.visit_bytes(&v)
445 }
446
447 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
448 where
449 E: de::Error,
450 {
451 let default = T::deserialize(v.into_deserializer())?;
452 Ok(PerPlatform::new(default))
453 }
454}
455
456impl<'de, T> Deserialize<'de> for PerPlatform<T>
457where
458 T: DeserializeOwned,
459 T: PlatformOverridable,
460 T: Default,
461 T: Clone,
462{
463 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
464 deserializer.deserialize_any(PerPlatformVisitor(std::marker::PhantomData))
465 }
466}
467
468pub(crate) fn per_platform_from_intermediate<'de, D, I, T>(
472 deserializer: D,
473) -> Result<PerPlatform<T>, D::Error>
474where
475 D: Deserializer<'de>,
476 I: PlatformOverridable<Err: ToString>,
477 I: DeserializeOwned,
478 I: Default,
479 I: Clone,
480 T: TryFrom<I, Error: ToString>,
481{
482 PerPlatform::<I>::deserialize(deserializer)?
483 .map(|internal| {
484 T::try_from(internal.clone()).map_err(|err| serde::de::Error::custom(err.to_string()))
485 })
486 .transpose()
487}
488
489fn apply_per_platform_overrides<T>(
490 per_platform: &mut HashMap<PlatformIdentifier, T>,
491 base: &T,
492) -> Result<(), T::Err>
493where
494 T: PartialOverride,
495 T: Clone,
496{
497 let per_platform_raw = per_platform.clone();
498 for (platform, overrides) in per_platform.clone() {
499 let overridden = base.apply_overrides(&overrides)?;
501 per_platform.insert(platform, overridden);
502 }
503 for (platform, overrides) in per_platform_raw {
504 for extended_platform in &platform.get_extended_platforms() {
506 if let Some(extended_overrides) = per_platform.get(extended_platform) {
507 per_platform.insert(
508 extended_platform.to_owned(),
509 extended_overrides.apply_overrides(&overrides)?,
510 );
511 }
512 }
513 }
514 Ok(())
515}
516
517#[cfg(test)]
518mod tests {
519
520 use super::*;
521 use proptest::prelude::*;
522
523 fn platform_identifier_strategy() -> impl Strategy<Value = PlatformIdentifier> {
524 prop_oneof![
525 Just(PlatformIdentifier::Unix),
526 Just(PlatformIdentifier::Windows),
527 Just(PlatformIdentifier::Win32),
528 Just(PlatformIdentifier::Cygwin),
529 Just(PlatformIdentifier::MacOSX),
530 Just(PlatformIdentifier::Linux),
531 Just(PlatformIdentifier::FreeBSD),
532 ]
533 }
534
535 #[tokio::test]
536 async fn sort_platform_identifier_more_specific_last() {
537 let mut platforms = vec![
538 PlatformIdentifier::Cygwin,
539 PlatformIdentifier::Linux,
540 PlatformIdentifier::Unix,
541 ];
542 platforms.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
543 assert_eq!(
544 platforms,
545 vec![
546 PlatformIdentifier::Unix,
547 PlatformIdentifier::Cygwin,
548 PlatformIdentifier::Linux
549 ]
550 );
551 let mut platforms = vec![PlatformIdentifier::Windows, PlatformIdentifier::Win32];
552 platforms.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
553 assert_eq!(
554 platforms,
555 vec![PlatformIdentifier::Win32, PlatformIdentifier::Windows]
556 )
557 }
558
559 #[tokio::test]
560 async fn test_is_subset_of() {
561 assert!(PlatformIdentifier::Unix.is_subset_of(&PlatformIdentifier::Linux));
562 assert!(PlatformIdentifier::Unix.is_subset_of(&PlatformIdentifier::MacOSX));
563 assert!(!PlatformIdentifier::Linux.is_subset_of(&PlatformIdentifier::Unix));
564 }
565
566 #[tokio::test]
567 async fn test_is_extension_of() {
568 assert!(PlatformIdentifier::Linux.is_extension_of(&PlatformIdentifier::Unix));
569 assert!(PlatformIdentifier::MacOSX.is_extension_of(&PlatformIdentifier::Unix));
570 assert!(!PlatformIdentifier::Unix.is_extension_of(&PlatformIdentifier::Linux));
571 }
572
573 #[tokio::test]
574 async fn per_platform() {
575 let foo = PerPlatform {
576 default: "default",
577 per_platform: vec![
578 (PlatformIdentifier::Unix, "unix"),
579 (PlatformIdentifier::FreeBSD, "freebsd"),
580 (PlatformIdentifier::Cygwin, "cygwin"),
581 (PlatformIdentifier::Linux, "linux"),
582 ]
583 .into_iter()
584 .collect(),
585 };
586 assert_eq!(*foo.get(&PlatformIdentifier::MacOSX), "unix");
587 assert_eq!(*foo.get(&PlatformIdentifier::Linux), "linux");
588 assert_eq!(*foo.get(&PlatformIdentifier::FreeBSD), "freebsd");
589 assert_eq!(*foo.get(&PlatformIdentifier::Cygwin), "cygwin");
590 assert_eq!(*foo.get(&PlatformIdentifier::Windows), "default");
591 }
592
593 #[cfg(target_os = "linux")]
594 #[tokio::test]
595 async fn test_target_identifier() {
596 run_test_target_identifier(PlatformIdentifier::Linux)
597 }
598
599 #[cfg(target_os = "macos")]
600 #[tokio::test]
601 async fn test_target_identifier() {
602 run_test_target_identifier(PlatformIdentifier::MacOSX)
603 }
604
605 #[cfg(target_env = "msvc")]
606 #[tokio::test]
607 async fn test_target_identifier() {
608 run_test_target_identifier(PlatformIdentifier::Windows)
609 }
610
611 #[cfg(target_os = "android")]
612 #[tokio::test]
613 async fn test_target_identifier() {
614 run_test_target_identifier(PlatformIdentifier::Unix)
615 }
616
617 fn run_test_target_identifier(expected: PlatformIdentifier) {
618 assert_eq!(expected, target_identifier());
619 }
620
621 proptest! {
622 #[test]
623 fn supported_platforms(identifier in platform_identifier_strategy()) {
624 let identifier_str = identifier.to_string();
625 let platforms = vec![identifier_str];
626 let platform_support = PlatformSupport::parse(&platforms).unwrap();
627 prop_assert!(platform_support.is_supported(&identifier))
628 }
629
630 #[test]
631 fn unsupported_platforms_only(unsupported in platform_identifier_strategy(), supported in platform_identifier_strategy()) {
632 if supported == unsupported
633 || unsupported.is_extension_of(&supported) {
634 return Ok(());
635 }
636 let identifier_str = format!("!{unsupported}");
637 let platforms = vec![identifier_str];
638 let platform_support = PlatformSupport::parse(&platforms).unwrap();
639 prop_assert!(!platform_support.is_supported(&unsupported));
640 prop_assert!(platform_support.is_supported(&supported))
641 }
642
643 #[test]
644 fn supported_and_unsupported_platforms(unsupported in platform_identifier_strategy(), unspecified in platform_identifier_strategy()) {
645 if unspecified == unsupported
646 || unsupported.is_extension_of(&unspecified) {
647 return Ok(());
648 }
649 let supported_str = unspecified.to_string();
650 let unsupported_str = format!("!{unsupported}");
651 let platforms = vec![supported_str, unsupported_str];
652 let platform_support = PlatformSupport::parse(&platforms).unwrap();
653 prop_assert!(platform_support.is_supported(&unspecified));
654 prop_assert!(!platform_support.is_supported(&unsupported));
655 }
656
657 #[test]
658 fn all_platforms_supported_if_none_are_specified(identifier in platform_identifier_strategy()) {
659 let platforms = vec![];
660 let platform_support = PlatformSupport::parse(&platforms).unwrap();
661 prop_assert!(platform_support.is_supported(&identifier))
662 }
663
664 #[test]
665 fn conflicting_platforms(identifier in platform_identifier_strategy()) {
666 let identifier_str = identifier.to_string();
667 let identifier_str_negated = format!("!{identifier}");
668 let platforms = vec![identifier_str, identifier_str_negated];
669 let _ = PlatformSupport::parse(&platforms).unwrap_err();
670 }
671
672 #[test]
673 fn extended_platforms_supported_if_supported(identifier in platform_identifier_strategy()) {
674 let identifier_str = identifier.to_string();
675 let platforms = vec![identifier_str];
676 let platform_support = PlatformSupport::parse(&platforms).unwrap();
677 for identifier in identifier.get_extended_platforms() {
678 prop_assert!(platform_support.is_supported(&identifier))
679 }
680 }
681
682 #[test]
683 fn sub_platforms_unsupported_if_unsupported(identifier in platform_identifier_strategy()) {
684 let identifier_str = format!("!{identifier}");
685 let platforms = vec![identifier_str];
686 let platform_support = PlatformSupport::parse(&platforms).unwrap();
687 for identifier in identifier.get_subsets() {
688 prop_assert!(!platform_support.is_supported(&identifier))
689 }
690 }
691
692 #[test]
693 fn conflicting_extended_platform_definitions(identifier in platform_identifier_strategy()) {
694 let extended_platforms = identifier.get_extended_platforms();
695 if extended_platforms.is_empty() {
696 return Ok(());
697 }
698 let supported_str = identifier.to_string();
699 let mut platforms: Vec<String> = extended_platforms.into_iter().map(|ident| format!("!{ident}")).collect();
700 platforms.push(supported_str);
701 let _ = PlatformSupport::parse(&platforms).unwrap_err();
702 }
703 }
704}