guppy 0.18.0

Track and query Cargo dependency graphs.
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::{errors::TargetSpecError, platform::PlatformSpec};
use std::{error, fmt, sync::Arc};
pub use target_spec::summaries::{PlatformSummary, TargetFeaturesSummary};

/// A serializable version of [`PlatformSpec`].
///
/// Requires the `summaries` feature to be enabled.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub enum PlatformSpecSummary {
    /// The intersection of all platforms.
    ///
    /// This is converted to and from [`PlatformSpec::Always`], and is expressed as the string
    /// `"always"`, or as `spec = "always"`.
    ///
    /// # Examples
    ///
    /// Deserialize the string `"always"`.
    ///
    /// ```
    /// # use guppy::platform::PlatformSpecSummary;
    /// let spec: PlatformSpecSummary = serde_json::from_str(r#""always""#).unwrap();
    /// assert_eq!(spec, PlatformSpecSummary::Always);
    /// ```
    ///
    /// Deserialize `spec = "always"`.
    ///
    /// ```
    /// # use guppy::platform::PlatformSpecSummary;
    /// let spec: PlatformSpecSummary = toml::from_str(r#"spec = "always""#).unwrap();
    /// assert_eq!(spec, PlatformSpecSummary::Always);
    /// ```
    Always,

    /// A list of platforms.
    ///
    /// This is converted to and from [`PlatformSpec::Platforms`].
    ///
    /// * If the list has one platform, it is serialized as that platform's
    ///   table (for example `{ triple = "x86_64-unknown-linux-gnu",
    ///   target-features = "unknown" }`) rather than as a one-element array,
    ///   so files written by earlier versions of `guppy` round-trip unchanged.
    /// * Otherwise (zero, or two or more platforms), it is serialized as an
    ///   array of platform summaries.
    ///
    /// The deserializer accepts the following forms that the serializer does not produce:
    ///
    /// * A bare triple string, which is shorthand for a table with only `triple` set.
    /// * A one-element array, which is treated as a single platform.
    ///
    /// An empty array is the union of zero platforms, on which nothing is
    /// enabled. Note that this is different from omitting the key entirely,
    /// which is turned into `Any`.
    ///
    /// The strings `"always"` and `"any"` are only recognized as the whole
    /// spec: inside an array they are rejected, since they are not platforms.
    ///
    /// # Examples
    ///
    /// Deserialize a target triple.
    ///
    /// ```
    /// # use guppy::platform::{PlatformSummary, PlatformSpecSummary};
    /// # use target_spec::summaries::TargetFeaturesSummary;
    /// # use std::collections::BTreeSet;
    /// let spec: PlatformSpecSummary = serde_json::from_str(r#""x86_64-unknown-linux-gnu""#).unwrap();
    /// assert_eq!(
    ///     spec,
    ///     PlatformSpecSummary::Platforms(vec![PlatformSummary::new("x86_64-unknown-linux-gnu")]),
    /// );
    /// ```
    ///
    /// Deserialize a target map.
    ///
    /// ```
    /// # use guppy::platform::{PlatformSummary, PlatformSpecSummary};
    /// # use target_spec::summaries::TargetFeaturesSummary;
    /// # use std::collections::BTreeSet;
    /// let spec: PlatformSpecSummary = toml::from_str(r#"
    /// triple = "x86_64-unknown-linux-gnu"
    /// target-features = []
    /// flags = []
    /// "#).unwrap();
    /// assert_eq!(
    ///     spec,
    ///     PlatformSpecSummary::Platforms(vec![
    ///         PlatformSummary::new("x86_64-unknown-linux-gnu")
    ///             .with_target_features(TargetFeaturesSummary::Features(BTreeSet::new()))
    ///     ])
    /// );
    /// ```
    ///
    /// Deserialize a list of platforms.
    ///
    /// ```
    /// # use guppy::platform::{PlatformSummary, PlatformSpecSummary};
    /// let spec: PlatformSpecSummary = serde_json::from_str(r#"
    /// ["x86_64-unknown-linux-gnu", { "triple": "x86_64-pc-windows-msvc" }]
    /// "#).unwrap();
    /// assert_eq!(
    ///     spec,
    ///     PlatformSpecSummary::Platforms(vec![
    ///         PlatformSummary::new("x86_64-unknown-linux-gnu"),
    ///         PlatformSummary::new("x86_64-pc-windows-msvc"),
    ///     ]),
    /// );
    /// ```
    Platforms(Vec<PlatformSummary>),

    /// The union of all platforms.
    ///
    /// This is converted to and from [`PlatformSpec::Any`], and is serialized as the string
    /// `"any"`.
    ///
    /// This is also the default, since in many cases one desires to compute the union of enabled
    /// dependencies across all platforms.
    ///
    /// # Examples
    ///
    /// Deserialize the string `"any"`.
    ///
    /// ```
    /// # use guppy::platform::PlatformSpecSummary;
    /// let spec: PlatformSpecSummary = serde_json::from_str(r#""any""#).unwrap();
    /// assert_eq!(spec, PlatformSpecSummary::Any);
    /// ```
    ///
    /// Deserialize `spec = "any"`.
    ///
    /// ```
    /// # use guppy::platform::PlatformSpecSummary;
    /// let spec: PlatformSpecSummary = toml::from_str(r#"spec = "any""#).unwrap();
    /// assert_eq!(spec, PlatformSpecSummary::Any);
    /// ```
    #[default]
    Any,
}

impl PlatformSpecSummary {
    /// Creates a new `PlatformSpecSummary` from a [`PlatformSpec`].
    pub fn new(platform_spec: &PlatformSpec) -> Self {
        match platform_spec {
            PlatformSpec::Always => PlatformSpecSummary::Always,
            PlatformSpec::Platforms(platforms) => PlatformSpecSummary::Platforms(
                platforms
                    .iter()
                    .map(|platform| platform.to_summary())
                    .collect(),
            ),
            PlatformSpec::Any => PlatformSpecSummary::Any,
        }
    }

    /// Converts `self` to a `PlatformSpec`.
    ///
    /// Returns an error naming the platform if it could not be converted, for
    /// example because its triple was unknown.
    pub fn to_platform_spec(&self) -> Result<PlatformSpec, PlatformSpecSummaryError> {
        match self {
            PlatformSpecSummary::Always => Ok(PlatformSpec::Always),
            PlatformSpecSummary::Platforms(platforms) => {
                let count = platforms.len();
                let platforms = platforms
                    .iter()
                    .enumerate()
                    .map(|(index, platform)| {
                        let platform =
                            platform
                                .to_platform()
                                .map_err(|source| PlatformSpecSummaryError {
                                    triple: platform.triple.clone(),
                                    index,
                                    count,
                                    source: Box::new(source),
                                })?;
                        Ok(Arc::new(platform))
                    })
                    .collect::<Result<Vec<_>, PlatformSpecSummaryError>>()?;
                Ok(PlatformSpec::Platforms(platforms))
            }
            PlatformSpecSummary::Any => Ok(PlatformSpec::Any),
        }
    }

    /// Returns true if `self` is `PlatformSpecSummary::Any`.
    pub fn is_any(&self) -> bool {
        match self {
            PlatformSpecSummary::Any => true,
            PlatformSpecSummary::Always | PlatformSpecSummary::Platforms(_) => false,
        }
    }
}

/// An error returned by [`PlatformSpecSummary::to_platform_spec`].
#[derive(Debug)]
pub struct PlatformSpecSummaryError {
    triple: String,
    index: usize,
    count: usize,
    source: Box<TargetSpecError>,
}

impl PlatformSpecSummaryError {
    /// Returns the triple of the platform that failed.
    pub fn triple(&self) -> &str {
        &self.triple
    }

    /// Returns the underlying target-spec error.
    ///
    /// This error is also returned by `Error::source` for this error.
    pub fn target_spec_error(&self) -> &TargetSpecError {
        &self.source
    }

    /// Returns the zero-based position of the failed platform in the list.
    pub fn index(&self) -> usize {
        self.index
    }

    /// Returns the total number of platforms in the list.
    ///
    /// Returns 1 for a bare platform.
    pub fn count(&self) -> usize {
        self.count
    }

    pub(crate) fn fmt_position(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.count > 1 {
            write!(f, " (element {} of {})", self.index + 1, self.count)?;
        }
        Ok(())
    }
}

impl fmt::Display for PlatformSpecSummaryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "invalid platform `{}`", self.triple)?;
        self.fmt_position(f)
    }
}

impl error::Error for PlatformSpecSummaryError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        Some(&*self.source)
    }
}

mod serde_impl {
    use super::*;
    use serde::{
        Deserialize, Deserializer, Serialize, Serializer,
        de::{
            self, DeserializeSeed, IgnoredAny, IntoDeserializer, MapAccess, SeqAccess, Visitor,
            value::{MapAccessDeserializer, StrDeserializer, StringDeserializer},
        },
    };
    use std::fmt;

    impl Serialize for PlatformSpecSummary {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            match self {
                PlatformSpecSummary::Always => Spec { spec: "always" }.serialize(serializer),
                PlatformSpecSummary::Any => Spec { spec: "any" }.serialize(serializer),
                PlatformSpecSummary::Platforms(platforms) => match platforms.as_slice() {
                    // A single platform is serialized as-is for compatibility
                    // with existing files.
                    [platform] => platform.serialize(serializer),
                    _ => platforms.serialize(serializer),
                },
            }
        }
    }

    // `always` and `any` are serialized as `spec = "always"` tables rather
    // than bare strings. This was originally a workaround for ValueAfterTable
    // errors in older `toml` versions, and is now the on-disk format.
    #[derive(Serialize)]
    struct Spec {
        spec: &'static str,
    }

    impl<'de> Deserialize<'de> for PlatformSpecSummary {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_any(PlatformSpecSummaryVisitor)
        }
    }

    // This is a hand-written visitor rather than an untagged enum for better
    // error reporting.
    struct PlatformSpecSummaryVisitor;

    impl<'de> Visitor<'de> for PlatformSpecSummaryVisitor {
        type Value = PlatformSpecSummary;

        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str(
                "\"always\", \"any\", a platform (a triple string or a table \
                 with a `triple` key), or an array of platforms",
            )
        }

        fn visit_str<E>(self, spec: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            match spec {
                "always" => Ok(PlatformSpecSummary::Always),
                "any" => Ok(PlatformSpecSummary::Any),
                // TODO: expression parsing would go here
                triple => {
                    let platform = platform_from_str(triple)?;
                    Ok(PlatformSpecSummary::Platforms(vec![platform]))
                }
            }
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut platforms = Vec::new();
            while let Some(PlatformElement(platform)) = seq.next_element()? {
                platforms.push(platform);
            }
            Ok(PlatformSpecSummary::Platforms(platforms))
        }

        fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
        where
            A: MapAccess<'de>,
        {
            let first_key = map.next_key::<String>()?;
            if first_key.as_deref() != Some("spec") {
                let platform =
                    platform_from_map(PlatformMap::new(first_key, map, SpecContext::WholeSpec))?;
                return Ok(PlatformSpecSummary::Platforms(vec![platform]));
            }

            let spec: String = map.next_value()?;
            let mut platform_keys = Vec::new();
            while let Some(key) = map.next_key::<String>()? {
                map.next_value::<IgnoredAny>()?;
                platform_keys.push(key);
            }
            if !platform_keys.is_empty() {
                return Err(de::Error::custom(format!(
                    "{} cannot appear alongside `spec = \"{spec}\"`: a spec table has \
                     only the `spec` key",
                    format_keys(&platform_keys),
                )));
            }
            match spec.as_str() {
                "always" => Ok(PlatformSpecSummary::Always),
                "any" => Ok(PlatformSpecSummary::Any),
                other => Err(de::Error::custom(format!(
                    "unknown spec `{other}`: expected \"always\" or \"any\" \
                     (a platform is written as `triple = \"...\"`)",
                ))),
            }
        }
    }

    /// One element of a platform array: a platform, but never `"always"`,
    /// `"any"`, or a `spec` table, since those are only meaningful as the
    /// whole spec.
    struct PlatformElement(PlatformSummary);

    impl<'de> Deserialize<'de> for PlatformElement {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_any(PlatformElementVisitor)
        }
    }

    struct PlatformElementVisitor;

    impl<'de> Visitor<'de> for PlatformElementVisitor {
        type Value = PlatformElement;

        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("a platform (a triple string or a table with a `triple` key)")
        }

        fn visit_str<E>(self, triple: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            platform_from_str(triple).map(PlatformElement)
        }

        fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
        where
            A: MapAccess<'de>,
        {
            let first_key = map.next_key::<String>()?;
            platform_from_map(PlatformMap::new(first_key, map, SpecContext::ListElement))
                .map(PlatformElement)
        }
    }

    #[derive(Clone, Copy)]
    enum SpecContext {
        WholeSpec,
        ListElement,
    }

    struct PlatformMap<A> {
        first_key: Option<String>,
        seen_keys: Vec<String>,
        map: A,
        context: SpecContext,
    }

    impl<A> PlatformMap<A> {
        fn new(first_key: Option<String>, map: A, context: SpecContext) -> Self {
            Self {
                first_key,
                seen_keys: Vec::new(),
                map,
                context,
            }
        }
    }

    impl<'de, A: MapAccess<'de>> MapAccess<'de> for PlatformMap<A> {
        type Error = A::Error;

        fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, A::Error>
        where
            K: DeserializeSeed<'de>,
        {
            let key = match self.first_key.take() {
                Some(key) => key,
                None => match self.map.next_key::<String>()? {
                    Some(key) => key,
                    None => return Ok(None),
                },
            };
            if key == "spec" {
                return Err(match self.context {
                    SpecContext::WholeSpec => de::Error::custom(format!(
                        "`spec` cannot appear alongside {}: a spec table has only \
                         the `spec` key",
                        format_keys(&self.seen_keys),
                    )),
                    SpecContext::ListElement => de::Error::custom(
                        "`spec` is not allowed inside a list of platforms: \
                         \"always\" and \"any\" are only valid as the whole spec",
                    ),
                });
            }
            self.seen_keys.push(key.clone());
            let deserializer: StringDeserializer<A::Error> = key.into_deserializer();
            seed.deserialize(deserializer).map(Some)
        }

        fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, A::Error>
        where
            V: DeserializeSeed<'de>,
        {
            self.map.next_value_seed(seed)
        }

        fn size_hint(&self) -> Option<usize> {
            self.map
                .size_hint()
                .map(|remaining| remaining + usize::from(self.first_key.is_some()))
        }
    }

    fn format_keys(keys: &[String]) -> String {
        keys.iter()
            .map(|key| format!("`{key}`"))
            .collect::<Vec<_>>()
            .join(", ")
    }

    fn platform_from_str<E: de::Error>(triple: &str) -> Result<PlatformSummary, E> {
        let deserializer: StrDeserializer<'_, E> = triple.into_deserializer();
        let platform = PlatformSummary::deserialize(deserializer)?;
        reject_spec_keyword(&platform.triple)?;
        Ok(platform)
    }

    fn platform_from_map<'de, A: MapAccess<'de>>(
        map: PlatformMap<A>,
    ) -> Result<PlatformSummary, A::Error> {
        let platform = PlatformSummary::deserialize(MapAccessDeserializer::new(map))?;
        reject_spec_keyword(&platform.triple)?;
        Ok(platform)
    }

    fn reject_spec_keyword<E: de::Error>(triple: &str) -> Result<(), E> {
        match triple {
            "always" | "any" => Err(E::custom(format!(
                "`{triple}` is not a platform: \"always\" and \"any\" are only \
                 valid as the whole spec, as a bare string or `spec = \"...\"`",
            ))),
            _ => Ok(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};
    use std::{collections::BTreeSet, error::Error as _};

    // Wrap in a struct: TOML needs a table at the top level.
    #[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
    #[serde(rename_all = "kebab-case")]
    pub(super) struct Wrapper {
        #[serde(default)]
        pub(super) target_platform: PlatformSpecSummary,
    }

    const LINUX: &str = "x86_64-unknown-linux-gnu";
    const WINDOWS: &str = "x86_64-pc-windows-msvc";

    fn single_platform(triple: &str) -> PlatformSpecSummary {
        PlatformSpecSummary::Platforms(vec![PlatformSummary::new(triple)])
    }

    fn sse2() -> TargetFeaturesSummary {
        TargetFeaturesSummary::Features(["sse2".to_owned()].into_iter().collect::<BTreeSet<_>>())
    }

    fn mixed_platforms() -> PlatformSpecSummary {
        PlatformSpecSummary::Platforms(vec![
            PlatformSummary::new(LINUX),
            PlatformSummary::new(WINDOWS).with_target_features(sse2()),
        ])
    }

    #[test]
    fn deserialize_platform_lists() {
        let mixed_toml = r#"
        target-platform = [
            "x86_64-unknown-linux-gnu",
            { triple = "x86_64-pc-windows-msvc", target-features = ["sse2"] },
        ]
        "#;
        let wrapper: Wrapper = toml::from_str(mixed_toml).expect("mixed TOML array deserialized");
        assert_eq!(
            wrapper.target_platform,
            mixed_platforms(),
            "string and table elements both deserialize as platforms",
        );

        let mixed_json = r#"{
            "target-platform": [
                "x86_64-unknown-linux-gnu",
                { "triple": "x86_64-pc-windows-msvc", "target-features": ["sse2"] }
            ]
        }"#;
        let wrapper: Wrapper =
            serde_json::from_str(mixed_json).expect("mixed JSON array deserialized");
        assert_eq!(
            wrapper.target_platform,
            mixed_platforms(),
            "the same visitor handles JSON",
        );

        let wrapper: Wrapper =
            toml::from_str("target-platform = []").expect("empty array deserialized");
        assert_eq!(
            wrapper.target_platform,
            PlatformSpecSummary::Platforms(vec![]),
            "an empty array is Platforms over no platforms",
        );

        let wrapper: Wrapper = toml::from_str("").expect("missing key deserialized");
        assert_eq!(
            wrapper.target_platform,
            PlatformSpecSummary::Any,
            "a missing key is Any, not Platforms over no platforms",
        );
    }

    #[test]
    fn deserialize_other_variants_still_win() {
        for (input, expected) in [
            (r#"target-platform = "any""#, PlatformSpecSummary::Any),
            (r#"target-platform = "always""#, PlatformSpecSummary::Always),
            (
                r#"target-platform = { spec = "always" }"#,
                PlatformSpecSummary::Always,
            ),
            (
                r#"target-platform = "x86_64-unknown-linux-gnu""#,
                single_platform(LINUX),
            ),
            (
                r#"target-platform = { triple = "x86_64-unknown-linux-gnu" }"#,
                single_platform(LINUX),
            ),
            (
                r#"target-platform = ["x86_64-unknown-linux-gnu"]"#,
                single_platform(LINUX),
            ),
        ] {
            let wrapper: Wrapper = toml::from_str(input).expect("input deserialized");
            assert_eq!(wrapper.target_platform, expected, "for input {input}");
        }
    }

    #[test]
    fn deserialize_errors_name_the_problem() {
        fn toml_error(input: &str) -> String {
            toml::from_str::<Wrapper>(input)
                .expect_err("input rejected")
                .to_string()
        }

        for (input, expected) in [
            (r#"target-platform = ["any"]"#, "`any` is not a platform"),
            (
                r#"target-platform = ["x86_64-unknown-linux-gnu", "always"]"#,
                "`always` is not a platform",
            ),
            (
                r#"target-platform = [{ triple = "any" }]"#,
                "`any` is not a platform",
            ),
            (
                r#"target-platform = [{ spec = "always" }]"#,
                "`spec` is not allowed inside a list",
            ),
            (
                r#"target-platform = [{ tripel = "x86_64-unknown-linux-gnu" }]"#,
                "unknown field `tripel`",
            ),
            (
                r#"target-platform = [{ target-features = [] }]"#,
                "missing field `triple`",
            ),
            (
                r#"target-platform = [""]"#,
                "a platform triple cannot be empty",
            ),
            (
                r#"target-platform = [
                    "x86_64-unknown-linux-gnu",
                    { triple = "x86_64-pc-windows-msvc", target-features = "bogus" },
                ]"#,
                "unknown string for target features: bogus",
            ),
            (
                r#"target-platform = [["x86_64-unknown-linux-gnu"]]"#,
                "expected a platform (a triple string or a table with a `triple` key)",
            ),
            (
                "target-platform = 5",
                "expected \"always\", \"any\", a platform",
            ),
            (
                r#"target-platform = """#,
                "a platform triple cannot be empty",
            ),
            (
                r#"target-platform = { triple = "" }"#,
                "a platform triple cannot be empty",
            ),
            (
                r#"target-platform = { spec = "always", triple = "x86_64-unknown-linux-gnu" }"#,
                "`triple` cannot appear alongside `spec = \"always\"`",
            ),
            (
                r#"target-platform = { spec = "always", flags = [] }"#,
                "`flags` cannot appear alongside",
            ),
            (
                r#"target-platform = { spec = "always", custom-cfg = "x", target-features = [] }"#,
                "`custom-cfg`, `target-features` cannot appear alongside",
            ),
            (
                r#"target-platform = { triple = "x86_64-unknown-linux-gnu", spec = "always" }"#,
                "`spec` cannot appear alongside `triple`:",
            ),
            (
                r#"target-platform = { triple = "x86_64-unknown-linux-gnu", flags = [], spec = "always" }"#,
                "`spec` cannot appear alongside `triple`, `flags`:",
            ),
            (
                r#"target-platform = [{ triple = "x86_64-unknown-linux-gnu", spec = "always" }]"#,
                "`spec` is not allowed inside a list",
            ),
            (
                r#"target-platform = { spec = "sometimes" }"#,
                "unknown spec `sometimes`",
            ),
            (
                r#"target-platform = { spec = "x86_64-unknown-linux-gnu" }"#,
                "unknown spec `x86_64-unknown-linux-gnu`",
            ),
            (
                r#"target-platform = { target-features = [] }"#,
                "missing field `triple`",
            ),
            (
                r#"target-platform = { triple = "x86_64-unknown-linux-gnu", bogus = 1 }"#,
                "unknown field `bogus`",
            ),
        ] {
            let message = toml_error(input);
            assert!(
                message.contains(expected),
                "for input {input}: error `{message}` contains `{expected}`",
            );
        }

        // The same visitor is used for JSON.
        for (input, expected) in [
            (
                r#"{ "target-platform": ["any"] }"#,
                "`any` is not a platform",
            ),
            (
                r#"{ "target-platform": { "triple": "x86_64-unknown-linux-gnu", "spec": "always" } }"#,
                "`spec` cannot appear alongside `triple`:",
            ),
        ] {
            let message = serde_json::from_str::<Wrapper>(input)
                .expect_err("input rejected")
                .to_string();
            assert!(
                message.contains(expected),
                "for JSON input {input}: error `{message}` contains `{expected}`",
            );
        }
    }

    #[test]
    fn serialize_one_platform_bare_and_others_as_arrays() {
        let render = |target_platform: PlatformSpecSummary| {
            toml::to_string(&Wrapper { target_platform }).expect("summary serialized to TOML")
        };

        assert_eq!(
            render(single_platform(LINUX)),
            "[target-platform]\ntriple = \"x86_64-unknown-linux-gnu\"\n\
             target-features = \"unknown\"\n",
            "one platform serializes bare, not as a one-element array",
        );
        assert_eq!(
            render(PlatformSpecSummary::Platforms(vec![])),
            "target-platform = []\n",
            "no platforms serialize as an empty array",
        );
        assert_eq!(
            render(mixed_platforms()),
            "[[target-platform]]\ntriple = \"x86_64-unknown-linux-gnu\"\n\
             target-features = \"unknown\"\n\n\
             [[target-platform]]\ntriple = \"x86_64-pc-windows-msvc\"\n\
             target-features = [\"sse2\"]\n",
            "several platforms serialize as an array",
        );
    }

    #[test]
    fn platform_summary_fields_round_trip() {
        #[derive(Serialize)]
        #[serde(rename_all = "kebab-case")]
        struct PlatformWrapper<T> {
            target_platform: T,
        }

        let full = PlatformSummary::new(LINUX)
            .with_custom_json("{}")
            .with_custom_cfg("target_os=\"linux\"")
            .with_target_features(sse2())
            .with_added_flags(["abc", "def"]);
        let plain = PlatformSummary::new(WINDOWS);

        let bare = toml::to_string(&PlatformWrapper {
            target_platform: &full,
        })
        .expect("platform serialized to TOML");
        let wrapper: Wrapper = toml::from_str(&bare).expect("bare platform deserialized");
        assert_eq!(
            wrapper.target_platform,
            PlatformSpecSummary::Platforms(vec![full.clone()]),
            "every PlatformSummary field survives the bare form:\n{bare}",
        );

        let list = toml::to_string(&PlatformWrapper {
            target_platform: vec![&full, &plain],
        })
        .expect("platform list serialized to TOML");
        let wrapper: Wrapper = toml::from_str(&list).expect("platform list deserialized");
        assert_eq!(
            wrapper.target_platform,
            PlatformSpecSummary::Platforms(vec![full, plain]),
            "every PlatformSummary field survives the list form:\n{list}",
        );
    }

    #[test]
    fn to_platform_spec_names_invalid_platform() {
        let err = single_platform("x86_64-unknown-foo")
            .to_platform_spec()
            .expect_err("unknown triple rejected");
        assert_eq!(err.triple(), "x86_64-unknown-foo", "error names the triple");
        assert_eq!(
            err.to_string(),
            "invalid platform `x86_64-unknown-foo`",
            "display names the triple"
        );
        assert!(
            err.source().is_some(),
            "the target-spec error is the source"
        );
        assert!(
            matches!(
                err.target_spec_error(),
                TargetSpecError::UnknownPlatformTriple(_)
            ),
            "underlying error is an unknown triple, found {:?}",
            err.target_spec_error(),
        );

        let err = PlatformSpecSummary::Platforms(vec![
            PlatformSummary::new(LINUX),
            PlatformSummary::new("x86_64-unknown-foo"),
        ])
        .to_platform_spec()
        .expect_err("unknown triple in a list rejected");
        assert_eq!(
            err.to_string(),
            "invalid platform `x86_64-unknown-foo` (element 2 of 2)",
            "display names the position within a list"
        );
    }

    #[test]
    fn to_platform_spec_accepts_non_platform_variants() {
        for summary in [PlatformSpecSummary::Always, PlatformSpecSummary::Any] {
            summary
                .to_platform_spec()
                .unwrap_or_else(|err| panic!("{summary:?} converted: {err}"));
        }
    }
}

#[cfg(all(test, feature = "proptest1"))]
mod proptests {
    use super::{tests::Wrapper, *};
    use crate::platform::Platform;
    use proptest::prelude::*;
    use std::collections::HashSet;

    fn assert_platforms_match(platform: &Platform, platform2: &Platform) {
        assert_eq!(
            platform.triple_str(),
            platform2.triple_str(),
            "triples match"
        );
        assert_eq!(
            platform.target_features(),
            platform2.target_features(),
            "target features match"
        );
        assert_eq!(
            platform.flags().collect::<HashSet<_>>(),
            platform2.flags().collect::<HashSet<_>>(),
            "flags match"
        );
    }

    proptest! {
        #[test]
        fn summary_roundtrip(platform_spec in any::<PlatformSpec>()) {
            let summary = PlatformSpecSummary::new(&platform_spec);
            let wrapper = Wrapper { target_platform: summary.clone() };
            let serialized = toml::ser::to_string(&wrapper).expect("serialization succeeded");

            let deserialized: Wrapper = toml::from_str(&serialized).expect("deserialization succeeded");
            assert_eq!(wrapper, deserialized, "summary and deserialized should match");
            let platform_spec2 = deserialized
                .target_platform
                .to_platform_spec()
                .expect("conversion to PlatformSpec succeeded");

            match (platform_spec, platform_spec2) {
                (PlatformSpec::Any, PlatformSpec::Any)
                | (PlatformSpec::Always, PlatformSpec::Always) => {},
                (PlatformSpec::Platforms(platforms), PlatformSpec::Platforms(platforms2)) => {
                    assert_eq!(platforms.len(), platforms2.len(), "platform counts match");
                    for (platform, platform2) in platforms.iter().zip(&platforms2) {
                        assert_platforms_match(platform, platform2);
                    }
                }
                (other, other2) => panic!("platform specs do not match: original: {other:?}, roundtrip: {other2:?}"),
            }
        }
    }
}