lux-lib 0.12.0

Library for the lux package manager for Lua
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use itertools::Itertools;
use mlua::{FromLua, IntoLuaMulti, Lua, LuaSerdeExt, UserData, Value};
use std::{cmp::Ordering, collections::HashMap, marker::PhantomData};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use thiserror::Error;

use serde::{
    de::{self, DeserializeOwned},
    Deserialize, Deserializer,
};
use serde_enum_str::{Deserialize_enum_str, Serialize_enum_str};

use super::{DisplayAsLuaKV, DisplayLuaKV, DisplayLuaValue};

/// Identifier by a platform.
/// The `PartialOrd` instance views more specific platforms as `Greater`
#[derive(Deserialize_enum_str, Serialize_enum_str, PartialEq, Eq, Hash, Debug, Clone, EnumIter)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum PlatformIdentifier {
    // TODO: Add undocumented platform identifiers from luarocks codebase?
    Unix,
    Windows,
    Win32,
    Cygwin,
    MacOSX,
    Linux,
    FreeBSD,
    #[serde(other)]
    Unknown(String),
}

impl Default for PlatformIdentifier {
    fn default() -> Self {
        target_identifier()
    }
}

// Order by specificity -> less specific = `Less`
impl PartialOrd for PlatformIdentifier {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (PlatformIdentifier::Unix, PlatformIdentifier::Cygwin) => Some(Ordering::Less),
            (PlatformIdentifier::Unix, PlatformIdentifier::MacOSX) => Some(Ordering::Less),
            (PlatformIdentifier::Unix, PlatformIdentifier::Linux) => Some(Ordering::Less),
            (PlatformIdentifier::Unix, PlatformIdentifier::FreeBSD) => Some(Ordering::Less),
            (PlatformIdentifier::Windows, PlatformIdentifier::Win32) => Some(Ordering::Greater),
            (PlatformIdentifier::Win32, PlatformIdentifier::Windows) => Some(Ordering::Less),
            (PlatformIdentifier::Cygwin, PlatformIdentifier::Unix) => Some(Ordering::Greater),
            (PlatformIdentifier::MacOSX, PlatformIdentifier::Unix) => Some(Ordering::Greater),
            (PlatformIdentifier::Linux, PlatformIdentifier::Unix) => Some(Ordering::Greater),
            (PlatformIdentifier::FreeBSD, PlatformIdentifier::Unix) => Some(Ordering::Greater),
            _ if self == other => Some(Ordering::Equal),
            _ => None,
        }
    }
}

impl FromLua for PlatformIdentifier {
    fn from_lua(value: Value, lua: &Lua) -> mlua::Result<Self> {
        let string = String::from_lua(value, lua)?;
        Ok(string
            .parse()
            .unwrap_or(PlatformIdentifier::Unknown(string)))
    }
}

/// Retrieves the platform identifier for the target platform
///
/// NOTE: This is the platform lux was built with.
/// As we don't support cross-compilation, we currently expect
/// users to use a version of lux that was built with the same platform
/// as the one they are targeting
fn target_identifier() -> PlatformIdentifier {
    if cfg!(target_env = "msvc") {
        PlatformIdentifier::Windows
    } else if cfg!(target_os = "linux") {
        PlatformIdentifier::Linux
    } else if cfg!(target_os = "macos") || cfg!(target_vendor = "apple") {
        PlatformIdentifier::MacOSX
    } else if cfg!(target_os = "freebsd") {
        PlatformIdentifier::FreeBSD
    } else if which::which("cygpath").is_ok() {
        PlatformIdentifier::Cygwin
    } else {
        PlatformIdentifier::Unix
    }
}

impl PlatformIdentifier {
    /// Get identifiers that are a subset of this identifier.
    /// For example, Unix is a subset of Linux
    pub fn get_subsets(&self) -> Vec<Self> {
        PlatformIdentifier::iter()
            .filter(|identifier| identifier.is_subset_of(self))
            .collect()
    }

    /// Get identifiers that are an extension of this identifier.
    /// For example, Linux is an extension of Unix
    pub fn get_extended_platforms(&self) -> Vec<Self> {
        PlatformIdentifier::iter()
            .filter(|identifier| identifier.is_extension_of(self))
            .collect()
    }

    /// e.g. Unix is a subset of Linux
    fn is_subset_of(&self, other: &PlatformIdentifier) -> bool {
        self.partial_cmp(other) == Some(Ordering::Less)
    }

    /// e.g. Linux is an extension of Unix
    fn is_extension_of(&self, other: &PlatformIdentifier) -> bool {
        self.partial_cmp(other) == Some(Ordering::Greater)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PlatformSupport {
    /// Do not match this platform
    platform_map: HashMap<PlatformIdentifier, bool>,
}

impl Default for PlatformSupport {
    fn default() -> Self {
        Self {
            platform_map: PlatformIdentifier::iter()
                .filter(|identifier| !matches!(identifier, PlatformIdentifier::Unknown(_)))
                .map(|identifier| (identifier, true))
                .collect(),
        }
    }
}

impl UserData for PlatformSupport {
    fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
        methods.add_method("is_supported", |_, this, platform: PlatformIdentifier| {
            Ok(this.is_supported(&platform))
        });
    }
}

impl<'de> Deserialize<'de> for PlatformSupport {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let platforms: Vec<String> = Vec::deserialize(deserializer)?;
        Self::parse(&platforms).map_err(de::Error::custom)
    }
}

impl DisplayAsLuaKV for PlatformSupport {
    fn display_lua(&self) -> DisplayLuaKV {
        DisplayLuaKV {
            key: "supported_platforms".to_string(),
            value: DisplayLuaValue::List(
                self.platforms()
                    .iter()
                    .map(|(platform, supported)| {
                        DisplayLuaValue::String(format!(
                            "{}{}",
                            if *supported { "" } else { "!" },
                            platform,
                        ))
                    })
                    .collect(),
            ),
        }
    }
}

#[derive(Error, Debug)]
pub enum PlatformValidationError {
    #[error("error when parsing platform identifier: {0}")]
    ParseError(String),

    #[error("conflicting supported platform entries")]
    ConflictingEntries,
}

impl PlatformSupport {
    fn validate_platforms(
        platforms: &[String],
    ) -> Result<HashMap<PlatformIdentifier, bool>, PlatformValidationError> {
        platforms
            .iter()
            .try_fold(HashMap::new(), |mut platforms, platform| {
                // Platform assertions can exist in one of the following forms:
                // - `platform` - a positive assertion for the platform (the platform must be present)
                // - `!platform` - a negative assertion for the platform (any platform *but* this one must be present)
                let (is_positive_assertion, platform) = platform
                    .strip_prefix('!')
                    .map(|str| (false, str))
                    .unwrap_or((true, platform));

                let platform_identifier = platform
                    .parse::<PlatformIdentifier>()
                    .map_err(|err| PlatformValidationError::ParseError(err.to_string()))?;

                // If a platform with the same name exists already and is contradictory
                // then throw an error. An example of such a contradiction is e.g.:
                // [`win32`, `!win32`]
                if platforms
                    .get(&platform_identifier)
                    .unwrap_or(&is_positive_assertion)
                    != &is_positive_assertion
                {
                    return Err(PlatformValidationError::ConflictingEntries);
                }

                platforms.insert(platform_identifier.clone(), is_positive_assertion);

                let subset_or_extended_platforms = if is_positive_assertion {
                    platform_identifier.get_extended_platforms()
                } else {
                    platform_identifier.get_subsets()
                };

                for sub_platform in subset_or_extended_platforms {
                    if platforms
                        .get(&sub_platform)
                        .unwrap_or(&is_positive_assertion)
                        != &is_positive_assertion
                    {
                        // TODO(vhyrro): More detailed errors
                        return Err(PlatformValidationError::ConflictingEntries);
                    }

                    platforms.insert(sub_platform, is_positive_assertion);
                }

                Ok(platforms)
            })
    }

    pub fn parse(platforms: &[String]) -> Result<Self, PlatformValidationError> {
        // Platforms are matched in one of two ways: exclusively or inclusively.
        // If only positive matches are present, then the platforms are matched inclusively (as you only support the matches that you specified).
        // If any negative matches are present, then the platforms are matched exclusively (as you want to support any operating system *other* than the ones you negated).
        match platforms {
            [] => Ok(Self::default()),
            platforms if platforms.iter().any(|platform| platform.starts_with('!')) => {
                let mut platform_map = Self::validate_platforms(platforms)?;

                // Loop through all identifiers and set them to true if they are not present in
                // the map (exclusive matching).
                for identifier in PlatformIdentifier::iter() {
                    if !matches!(identifier, PlatformIdentifier::Unknown(_)) {
                        platform_map.entry(identifier).or_insert(true);
                    }
                }

                Ok(Self { platform_map })
            }
            // Only validate positive matches (inclusive matching)
            platforms => Ok(Self {
                platform_map: Self::validate_platforms(platforms)?,
            }),
        }
    }

    pub fn is_supported(&self, platform: &PlatformIdentifier) -> bool {
        self.platform_map.get(platform).cloned().unwrap_or(false)
    }

    pub(crate) fn platforms(&self) -> &HashMap<PlatformIdentifier, bool> {
        &self.platform_map
    }
}

pub trait PartialOverride: Sized {
    type Err: std::error::Error;

    fn apply_overrides(&self, override_val: &Self) -> Result<Self, Self::Err>;
}

pub trait PlatformOverridable: PartialOverride {
    type Err: std::error::Error;

    fn on_nil<T>() -> Result<PerPlatform<T>, <Self as PlatformOverridable>::Err>
    where
        T: PlatformOverridable,
        T: Default;
}

pub trait FromPlatformOverridable<T: PlatformOverridable, G: FromPlatformOverridable<T, G>> {
    type Err: std::error::Error;

    fn from_platform_overridable(internal: T) -> Result<G, Self::Err>;
}

/// Data that that can vary per platform
#[derive(Clone, Debug, PartialEq)]
pub struct PerPlatform<T> {
    /// The base data, applicable if no platform is specified
    pub(crate) default: T,
    /// The per-platform override, if present.
    pub(crate) per_platform: HashMap<PlatformIdentifier, T>,
}

impl<T> PerPlatform<T> {
    pub(crate) fn new(default: T) -> Self {
        Self {
            default,
            per_platform: HashMap::default(),
        }
    }

    /// Merge per-platform overrides for the configured build target platform,
    /// with more specific platform overrides having higher priority.
    pub fn current_platform(&self) -> &T {
        self.for_platform_identifier(&target_identifier())
    }

    fn for_platform_identifier(&self, identifier: &PlatformIdentifier) -> &T {
        self.get(identifier)
    }

    pub fn get(&self, platform: &PlatformIdentifier) -> &T {
        self.per_platform.get(platform).unwrap_or(
            platform
                .get_subsets()
                .into_iter()
                // More specific platforms first.
                // This is safe because a platform's subsets
                // can be totally ordered among each other.
                .sorted_by(|a, b| b.partial_cmp(a).unwrap_or(Ordering::Equal))
                .find(|identifier| self.per_platform.contains_key(identifier))
                .and_then(|identifier| self.per_platform.get(&identifier))
                .unwrap_or(&self.default),
        )
    }

    pub(crate) fn map<U, F>(&self, cb: F) -> PerPlatform<U>
    where
        F: Fn(&T) -> U,
    {
        PerPlatform {
            default: cb(&self.default),
            per_platform: self
                .per_platform
                .iter()
                .map(|(identifier, value)| (identifier.clone(), cb(value)))
                .collect(),
        }
    }
}

impl<U, E> PerPlatform<Result<U, E>>
where
    E: std::error::Error,
{
    pub fn transpose(self) -> Result<PerPlatform<U>, E> {
        Ok(PerPlatform {
            default: self.default?,
            per_platform: self
                .per_platform
                .into_iter()
                .map(|(identifier, value)| Ok((identifier, value?)))
                .try_collect()?,
        })
    }
}

impl<T: Default> Default for PerPlatform<T> {
    fn default() -> Self {
        Self {
            default: T::default(),
            per_platform: HashMap::default(),
        }
    }
}

impl<'de, T> Deserialize<'de> for PerPlatform<T>
where
    T: Deserialize<'de>,
    T: Clone,
    T: PartialOverride,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let mut map = toml::map::Map::deserialize(deserializer)?;

        let mut per_platform: HashMap<PlatformIdentifier, T> = map
            .remove("platforms")
            .map_or(Ok(HashMap::default()), |platforms| platforms.try_into())
            .map_err(serde::de::Error::custom)?;

        let default: T = map.try_into().map_err(serde::de::Error::custom)?;

        apply_per_platform_overrides(&mut per_platform, &default)
            .map_err(serde::de::Error::custom)?;

        Ok(PerPlatform {
            default,
            per_platform,
        })
    }
}

impl<T> FromLua for PerPlatform<T>
where
    T: PlatformOverridable,
    T: PartialOverride,
    T: DeserializeOwned,
    T: Default,
    T: Clone,
{
    fn from_lua(value: Value, lua: &Lua) -> mlua::Result<Self> {
        match &value {
            list @ Value::Table(tbl) => {
                let mut per_platform = match tbl.get("platforms")? {
                    val @ Value::Table(_) => Ok(lua.from_value(val)?),
                    Value::Nil => Ok(HashMap::default()),
                    val => Err(mlua::Error::DeserializeError(format!(
                        "Expected platforms to be a table or nil, but got {}",
                        val.type_name()
                    ))),
                }?;
                let _ = tbl.raw_remove("platforms");
                let default = lua.from_value(list.to_owned())?;
                apply_per_platform_overrides(&mut per_platform, &default).map_err(
                    |err: <T as PartialOverride>::Err| {
                        mlua::Error::DeserializeError(err.to_string())
                    },
                )?;
                Ok(PerPlatform {
                    default,
                    per_platform,
                })
            }
            Value::Nil => T::on_nil().map_err(|err| mlua::Error::DeserializeError(err.to_string())),
            val => Err(mlua::Error::DeserializeError(format!(
                "Expected rockspec external dependencies to be a table or nil, but got {}",
                val.type_name()
            ))),
        }
    }
}

impl<T> UserData for PerPlatform<T>
where
    T: IntoLuaMulti + Clone,
{
    fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
        // TODO(mrcjkb): YAGNI?
        // methods.add_method("current_platform", |_, this, _: ()| {
        //     Ok(this.for_target_platform().clone())
        // });
        methods.add_method("get", |_, this, platform: PlatformIdentifier| {
            Ok(this.get(&platform).clone())
        });
    }
}

/// Newtype wrapper used to implement a `FromLua` instance for `FromPlatformOverridable`
/// This is necessary, because Rust doesn't yet support specialization.
pub struct PerPlatformWrapper<T, G> {
    pub un_per_platform: PerPlatform<T>,
    phantom: PhantomData<G>,
}

impl<T, G> FromLua for PerPlatformWrapper<T, G>
where
    T: FromPlatformOverridable<G, T, Err: ToString>,
    G: PlatformOverridable<Err: ToString>,
    G: DeserializeOwned,
    G: Default,
    G: Clone,
{
    fn from_lua(value: Value, lua: &Lua) -> mlua::Result<Self> {
        let internal = PerPlatform::from_lua(value, lua)?;
        let per_platform: HashMap<_, _> = internal
            .per_platform
            .into_iter()
            .map(|(platform, internal_override)| {
                let override_spec = T::from_platform_overridable(internal_override)
                    .map_err(|err| mlua::Error::DeserializeError(err.to_string()))?;

                Ok((platform, override_spec))
            })
            .try_collect::<_, _, mlua::Error>()?;
        let un_per_platform = PerPlatform {
            default: T::from_platform_overridable(internal.default)
                .map_err(|err| mlua::Error::DeserializeError(err.to_string()))?,
            per_platform,
        };
        Ok(PerPlatformWrapper {
            un_per_platform,
            phantom: PhantomData,
        })
    }
}

impl<'de, T, G> Deserialize<'de> for PerPlatformWrapper<T, G>
where
    T: FromPlatformOverridable<G, T, Err: ToString>,
    G: PlatformOverridable<Err: ToString>,
    G: DeserializeOwned,
    G: Default,
    G: Clone,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let internal = PerPlatform::deserialize(deserializer)?;
        let per_platform: HashMap<_, _> = internal
            .per_platform
            .into_iter()
            .map(|(platform, internal_override)| {
                let override_spec = T::from_platform_overridable(internal_override)
                    .map_err(serde::de::Error::custom)?;

                Ok((platform, override_spec))
            })
            .try_collect::<_, _, D::Error>()?;
        let un_per_platform = PerPlatform {
            default: T::from_platform_overridable(internal.default)
                .map_err(serde::de::Error::custom)?,
            per_platform,
        };
        Ok(PerPlatformWrapper {
            un_per_platform,
            phantom: PhantomData,
        })
    }
}

fn apply_per_platform_overrides<T>(
    per_platform: &mut HashMap<PlatformIdentifier, T>,
    base: &T,
) -> Result<(), T::Err>
where
    T: PartialOverride,
    T: Clone,
{
    let per_platform_raw = per_platform.clone();
    for (platform, overrides) in per_platform.clone() {
        // Add base values for each platform
        let overridden = base.apply_overrides(&overrides)?;
        per_platform.insert(platform, overridden);
    }
    for (platform, overrides) in per_platform_raw {
        // Add extended platform dependencies (without base deps) for each platform
        for extended_platform in &platform.get_extended_platforms() {
            if let Some(extended_overrides) = per_platform.get(extended_platform) {
                per_platform.insert(
                    extended_platform.to_owned(),
                    extended_overrides.apply_overrides(&overrides)?,
                );
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {

    use super::*;
    use proptest::prelude::*;

    fn platform_identifier_strategy() -> impl Strategy<Value = PlatformIdentifier> {
        prop_oneof![
            Just(PlatformIdentifier::Unix),
            Just(PlatformIdentifier::Windows),
            Just(PlatformIdentifier::Win32),
            Just(PlatformIdentifier::Cygwin),
            Just(PlatformIdentifier::MacOSX),
            Just(PlatformIdentifier::Linux),
            Just(PlatformIdentifier::FreeBSD),
        ]
    }

    #[tokio::test]
    async fn sort_platform_identifier_more_specific_last() {
        let mut platforms = vec![
            PlatformIdentifier::Cygwin,
            PlatformIdentifier::Linux,
            PlatformIdentifier::Unix,
        ];
        platforms.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
        assert_eq!(
            platforms,
            vec![
                PlatformIdentifier::Unix,
                PlatformIdentifier::Cygwin,
                PlatformIdentifier::Linux
            ]
        );
        let mut platforms = vec![PlatformIdentifier::Windows, PlatformIdentifier::Win32];
        platforms.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
        assert_eq!(
            platforms,
            vec![PlatformIdentifier::Win32, PlatformIdentifier::Windows]
        )
    }

    #[tokio::test]
    async fn test_is_subset_of() {
        assert!(PlatformIdentifier::Unix.is_subset_of(&PlatformIdentifier::Linux));
        assert!(PlatformIdentifier::Unix.is_subset_of(&PlatformIdentifier::MacOSX));
        assert!(!PlatformIdentifier::Linux.is_subset_of(&PlatformIdentifier::Unix));
    }

    #[tokio::test]
    async fn test_is_extension_of() {
        assert!(PlatformIdentifier::Linux.is_extension_of(&PlatformIdentifier::Unix));
        assert!(PlatformIdentifier::MacOSX.is_extension_of(&PlatformIdentifier::Unix));
        assert!(!PlatformIdentifier::Unix.is_extension_of(&PlatformIdentifier::Linux));
    }

    #[tokio::test]
    async fn per_platform() {
        let foo = PerPlatform {
            default: "default",
            per_platform: vec![
                (PlatformIdentifier::Unix, "unix"),
                (PlatformIdentifier::FreeBSD, "freebsd"),
                (PlatformIdentifier::Cygwin, "cygwin"),
                (PlatformIdentifier::Linux, "linux"),
            ]
            .into_iter()
            .collect(),
        };
        assert_eq!(*foo.get(&PlatformIdentifier::MacOSX), "unix");
        assert_eq!(*foo.get(&PlatformIdentifier::Linux), "linux");
        assert_eq!(*foo.get(&PlatformIdentifier::FreeBSD), "freebsd");
        assert_eq!(*foo.get(&PlatformIdentifier::Cygwin), "cygwin");
        assert_eq!(*foo.get(&PlatformIdentifier::Windows), "default");
    }

    #[cfg(target_os = "linux")]
    #[tokio::test]
    async fn test_target_identifier() {
        run_test_target_identifier(PlatformIdentifier::Linux)
    }

    #[cfg(target_os = "macos")]
    #[tokio::test]
    async fn test_target_identifier() {
        run_test_target_identifier(PlatformIdentifier::MacOSX)
    }

    #[cfg(target_env = "msvc")]
    #[tokio::test]
    async fn test_target_identifier() {
        run_test_target_identifier(PlatformIdentifier::Windows)
    }

    fn run_test_target_identifier(expected: PlatformIdentifier) {
        assert_eq!(expected, target_identifier());
    }

    proptest! {
        #[test]
        fn supported_platforms(identifier in platform_identifier_strategy()) {
            let identifier_str = identifier.to_string();
            let platforms = vec![identifier_str];
            let platform_support = PlatformSupport::parse(&platforms).unwrap();
            prop_assert!(platform_support.is_supported(&identifier))
        }

        #[test]
        fn unsupported_platforms_only(unsupported in platform_identifier_strategy(), supported in platform_identifier_strategy()) {
            if supported == unsupported
                || unsupported.is_extension_of(&supported) {
                return Ok(());
            }
            let identifier_str = format!("!{}", unsupported);
            let platforms = vec![identifier_str];
            let platform_support = PlatformSupport::parse(&platforms).unwrap();
            prop_assert!(!platform_support.is_supported(&unsupported));
            prop_assert!(platform_support.is_supported(&supported))
        }

        #[test]
        fn supported_and_unsupported_platforms(unsupported in platform_identifier_strategy(), unspecified in platform_identifier_strategy()) {
            if unspecified == unsupported
                || unsupported.is_extension_of(&unspecified) {
                return Ok(());
            }
            let supported_str = unspecified.to_string();
            let unsupported_str = format!("!{}", unsupported);
            let platforms = vec![supported_str, unsupported_str];
            let platform_support = PlatformSupport::parse(&platforms).unwrap();
            prop_assert!(platform_support.is_supported(&unspecified));
            prop_assert!(!platform_support.is_supported(&unsupported));
        }

        #[test]
        fn all_platforms_supported_if_none_are_specified(identifier in platform_identifier_strategy()) {
            let platforms = vec![];
            let platform_support = PlatformSupport::parse(&platforms).unwrap();
            prop_assert!(platform_support.is_supported(&identifier))
        }

        #[test]
        fn conflicting_platforms(identifier in platform_identifier_strategy()) {
            let identifier_str = identifier.to_string();
            let identifier_str_negated = format!("!{}", identifier);
            let platforms = vec![identifier_str, identifier_str_negated];
            let _ = PlatformSupport::parse(&platforms).unwrap_err();
        }

        #[test]
        fn extended_platforms_supported_if_supported(identifier in platform_identifier_strategy()) {
            let identifier_str = identifier.to_string();
            let platforms = vec![identifier_str];
            let platform_support = PlatformSupport::parse(&platforms).unwrap();
            for identifier in identifier.get_extended_platforms() {
                prop_assert!(platform_support.is_supported(&identifier))
            }
        }

        #[test]
        fn sub_platforms_unsupported_if_unsupported(identifier in platform_identifier_strategy()) {
            let identifier_str = format!("!{}", identifier);
            let platforms = vec![identifier_str];
            let platform_support = PlatformSupport::parse(&platforms).unwrap();
            for identifier in identifier.get_subsets() {
                prop_assert!(!platform_support.is_supported(&identifier))
            }
        }

        #[test]
        fn conflicting_extended_platform_definitions(identifier in platform_identifier_strategy()) {
            let extended_platforms = identifier.get_extended_platforms();
            if extended_platforms.is_empty() {
                return Ok(());
            }
            let supported_str = identifier.to_string();
            let mut platforms: Vec<String> = extended_platforms.into_iter().map(|ident| format!("!{}", ident)).collect();
            platforms.push(supported_str);
            let _ = PlatformSupport::parse(&platforms).unwrap_err();
        }
    }
}