cabinpkg-core 0.16.0

Stable internal data model for Cabin
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
//! Typed C/C++ language standards.
//!
//! Owns the standard enums, the manifest declaration shape shared by
//! `[package]` and `[target.<name>]`, effective-standard resolution
//! (target â–¶ package â–¶ built-in default), interface-requirement
//! relevance and fallback, the escape-hatch conflict detector, and
//! the per-package summary that feeds `BuildConfiguration`
//! fingerprinting and the metadata view. Pure data and logic only;
//! no I/O. See `docs/language-standards.md` for the user-facing
//! contract.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{ResolvedProfileFlags, SourceLanguage, Target, classify_source};

/// C language standards Cabin can request, oldest to newest. The
/// `Ord` derive follows declaration order, which the interface
/// compatibility check relies on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum CStandard {
    #[serde(rename = "c89")]
    C89,
    #[serde(rename = "c99")]
    C99,
    #[serde(rename = "c11")]
    C11,
    #[serde(rename = "c17")]
    C17,
    #[serde(rename = "c23")]
    C23,
}

impl CStandard {
    pub const ALL: [Self; 5] = [Self::C89, Self::C99, Self::C11, Self::C17, Self::C23];

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::C89 => "c89",
            Self::C99 => "c99",
            Self::C11 => "c11",
            Self::C17 => "c17",
            Self::C23 => "c23",
        }
    }

    /// # Errors
    /// Returns [`LanguageStandardParseError`] listing the valid
    /// spellings when `value` is not a recognized C standard.
    pub fn parse(value: &str) -> Result<Self, LanguageStandardParseError> {
        Self::ALL
            .into_iter()
            .find(|s| s.as_str() == value)
            .ok_or_else(|| LanguageStandardParseError {
                language: SourceLanguage::C,
                value: value.to_owned(),
            })
    }
}

impl std::fmt::Display for CStandard {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for CStandard {
    type Err = LanguageStandardParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

/// C++ language standards Cabin can request, oldest to newest.
/// `c++26` is deferred until its capability thresholds are audited.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum CxxStandard {
    #[serde(rename = "c++98")]
    Cxx98,
    #[serde(rename = "c++03")]
    Cxx03,
    #[serde(rename = "c++11")]
    Cxx11,
    #[serde(rename = "c++14")]
    Cxx14,
    #[serde(rename = "c++17")]
    Cxx17,
    #[serde(rename = "c++20")]
    Cxx20,
    #[serde(rename = "c++23")]
    Cxx23,
}

impl CxxStandard {
    pub const ALL: [Self; 7] = [
        Self::Cxx98,
        Self::Cxx03,
        Self::Cxx11,
        Self::Cxx14,
        Self::Cxx17,
        Self::Cxx20,
        Self::Cxx23,
    ];

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Cxx98 => "c++98",
            Self::Cxx03 => "c++03",
            Self::Cxx11 => "c++11",
            Self::Cxx14 => "c++14",
            Self::Cxx17 => "c++17",
            Self::Cxx20 => "c++20",
            Self::Cxx23 => "c++23",
        }
    }

    /// # Errors
    /// Returns [`LanguageStandardParseError`] listing the valid
    /// spellings when `value` is not a recognized C++ standard.
    pub fn parse(value: &str) -> Result<Self, LanguageStandardParseError> {
        Self::ALL
            .into_iter()
            .find(|s| s.as_str() == value)
            .ok_or_else(|| LanguageStandardParseError {
                language: SourceLanguage::Cxx,
                value: value.to_owned(),
            })
    }
}

impl std::fmt::Display for CxxStandard {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for CxxStandard {
    type Err = LanguageStandardParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

/// Built-in defaults: what every package compiles with when no
/// standard is declared anywhere. Changing either constant changes
/// every undeclared project's compile commands.
pub const DEFAULT_C_STANDARD: CStandard = CStandard::C11;
pub const DEFAULT_CXX_STANDARD: CxxStandard = CxxStandard::Cxx17;

/// An invalid manifest standard value, with the valid spellings for
/// the diagnostic.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[error(
    "unknown {} standard `{value}`: expected one of {}",
    .language.human_label(),
    valid_standard_values(*.language)
)]
pub struct LanguageStandardParseError {
    pub language: SourceLanguage,
    pub value: String,
}

fn valid_standard_values(language: SourceLanguage) -> String {
    match language {
        SourceLanguage::C => CStandard::ALL.map(CStandard::as_str).join(", "),
        SourceLanguage::Cxx => CxxStandard::ALL.map(CxxStandard::as_str).join(", "),
    }
}

/// One per-compile standard value, carried by the build IR. Encodes
/// the source language, so the dialect lowering derives both the
/// rule kind and the standard flag from this single field.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LanguageStandard {
    C(CStandard),
    Cxx(CxxStandard),
}

impl LanguageStandard {
    pub const fn language(self) -> SourceLanguage {
        match self {
            Self::C(_) => SourceLanguage::C,
            Self::Cxx(_) => SourceLanguage::Cxx,
        }
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::C(s) => s.as_str(),
            Self::Cxx(s) => s.as_str(),
        }
    }

    /// The `/std:` value `cl.exe` accepts for this standard, when a
    /// stable one exists. `None` marks the MSVC-dialect gaps
    /// (C89/C99/C23, C++98/03/11/23); the planner rejects those
    /// before lowering on the MSVC dialect.
    pub const fn msvc_spelling(self) -> Option<&'static str> {
        match self {
            Self::C(CStandard::C11) => Some("/std:c11"),
            Self::C(CStandard::C17) => Some("/std:c17"),
            Self::Cxx(CxxStandard::Cxx14) => Some("/std:c++14"),
            Self::Cxx(CxxStandard::Cxx17) => Some("/std:c++17"),
            Self::Cxx(CxxStandard::Cxx20) => Some("/std:c++20"),
            _ => None,
        }
    }
}

impl std::fmt::Display for LanguageStandard {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// One declared standard-field value as it travels from the
/// manifest to the resolved package model. Mirrors the
/// `DependencySource::Workspace` contract: `cabin-manifest`
/// constructs `Declared` (literal) or `Workspace` (the
/// `{ workspace = true }` opt-in marker), `cabin-workspace`
/// rewrites every marker into `Inherited(value)` before any
/// consumer sees the `Package`, and a marker that survives past
/// the loader is a workspace invariant violation. Marker
/// semantics deliberately split by consumer: `.is_some()`-based
/// relevance checks (`imposes_requirement`,
/// `find_standard_flag_conflicts`, `is_empty`) count an
/// unresolved marker as a declaration, while the `*_value()`
/// accessors treat it as absent — both cases are unreachable
/// post-loader under the rewrite invariant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StandardDeclaration<S> {
    /// Literal value written in this manifest → source `package`
    /// (or `target` for a target-level field).
    Declared(S),
    /// Unresolved `{ workspace = true }` opt-in marker.
    Workspace,
    /// Value resolved from the workspace root's `[workspace]`
    /// declaration → source `workspace`.
    Inherited(S),
}

impl<S> StandardDeclaration<S> {
    /// The resolved standard value. `None` only for an unresolved
    /// marker, which must not reach consumers (debug-asserted).
    #[must_use]
    pub fn value(self) -> Option<S> {
        match self {
            Self::Declared(s) | Self::Inherited(s) => Some(s),
            Self::Workspace => {
                debug_assert!(
                    false,
                    "unresolved `{{ workspace = true }}` standard marker reached a consumer"
                );
                None
            }
        }
    }
}

// `Declared` and `Inherited` serialize as the bare standard string
// so the canonical-metadata / index wire format is identical to a
// literal declaration (publish bakes inherited values). An
// unresolved marker must never reach a serialization boundary.
impl<S: Serialize> Serialize for StandardDeclaration<S> {
    fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        Ser: serde::Serializer,
    {
        match self {
            Self::Declared(s) | Self::Inherited(s) => s.serialize(serializer),
            Self::Workspace => Err(serde::ser::Error::custom(
                "unresolved `{ workspace = true }` standard marker cannot be serialized",
            )),
        }
    }
}

// A bare string deserializes as `Declared`: a consumer re-parsing
// published metadata sees a plain declaration.
impl<'de, S: Deserialize<'de>> Deserialize<'de> for StandardDeclaration<S> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        S::deserialize(deserializer).map(Self::Declared)
    }
}

/// The four manifest fields, shared by `[package]` and
/// `[target.<name>]` (`c-standard` / `cxx-standard` /
/// `interface-c-standard` / `interface-cxx-standard`). At
/// `[package]` level each field may also be the
/// `{ workspace = true }` opt-in marker; target-level fields are
/// always `Declared` (the parser rejects markers there).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct LanguageStandardSettings {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub c_standard: Option<StandardDeclaration<CStandard>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cxx_standard: Option<StandardDeclaration<CxxStandard>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub interface_c_standard: Option<StandardDeclaration<CStandard>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub interface_cxx_standard: Option<StandardDeclaration<CxxStandard>>,
}

impl LanguageStandardSettings {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.c_standard.is_none()
            && self.cxx_standard.is_none()
            && self.interface_c_standard.is_none()
            && self.interface_cxx_standard.is_none()
    }

    /// Resolved C implementation standard, when declared or
    /// inherited.
    #[must_use]
    pub fn c_standard_value(&self) -> Option<CStandard> {
        self.c_standard.and_then(StandardDeclaration::value)
    }

    /// Resolved C++ implementation standard, when declared or
    /// inherited.
    #[must_use]
    pub fn cxx_standard_value(&self) -> Option<CxxStandard> {
        self.cxx_standard.and_then(StandardDeclaration::value)
    }

    /// Resolved C interface standard, when declared or inherited.
    #[must_use]
    pub fn interface_c_standard_value(&self) -> Option<CStandard> {
        self.interface_c_standard
            .and_then(StandardDeclaration::value)
    }

    /// Resolved C++ interface standard, when declared or
    /// inherited.
    #[must_use]
    pub fn interface_cxx_standard_value(&self) -> Option<CxxStandard> {
        self.interface_cxx_standard
            .and_then(StandardDeclaration::value)
    }

    /// First field carrying the unresolved `{ workspace = true }`
    /// marker, for error reporting.
    #[must_use]
    pub fn workspace_marker_field(&self) -> Option<&'static str> {
        if self.c_standard == Some(StandardDeclaration::Workspace) {
            return Some("c-standard");
        }
        if self.cxx_standard == Some(StandardDeclaration::Workspace) {
            return Some("cxx-standard");
        }
        if self.interface_c_standard == Some(StandardDeclaration::Workspace) {
            return Some("interface-c-standard");
        }
        if self.interface_cxx_standard == Some(StandardDeclaration::Workspace) {
            return Some("interface-cxx-standard");
        }
        None
    }
}

/// Literal `[workspace]`-level standard default values that member
/// packages opt into per field with `<field> = { workspace = true }`
/// on `[package]`. Plain values only — the opt-in marker is not
/// accepted on the `[workspace]` table itself.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub struct WorkspaceStandardDefaults {
    #[serde(rename = "c-standard", skip_serializing_if = "Option::is_none")]
    pub c_standard: Option<CStandard>,
    #[serde(rename = "cxx-standard", skip_serializing_if = "Option::is_none")]
    pub cxx_standard: Option<CxxStandard>,
    #[serde(
        rename = "interface-c-standard",
        skip_serializing_if = "Option::is_none"
    )]
    pub interface_c_standard: Option<CStandard>,
    #[serde(
        rename = "interface-cxx-standard",
        skip_serializing_if = "Option::is_none"
    )]
    pub interface_cxx_standard: Option<CxxStandard>,
}

impl WorkspaceStandardDefaults {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.c_standard.is_none()
            && self.cxx_standard.is_none()
            && self.interface_c_standard.is_none()
            && self.interface_cxx_standard.is_none()
    }
}

/// Provenance of an effective implementation standard.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum LanguageStandardSource {
    BuiltinDefault,
    Package,
    Target,
    Workspace,
}

impl LanguageStandardSource {
    pub const fn as_key(self) -> &'static str {
        match self {
            Self::BuiltinDefault => "builtin-default",
            Self::Package => "package",
            Self::Target => "target",
            Self::Workspace => "workspace",
        }
    }
}

/// Provenance of an effective interface standard.
/// `CompileStandard` marks the documented default: no interface
/// field was declared, so the requirement equals the target's
/// effective implementation standard.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum InterfaceStandardSource {
    Target,
    Package,
    CompileStandard,
    Workspace,
}

impl InterfaceStandardSource {
    pub const fn as_key(self) -> &'static str {
        match self {
            Self::Target => "target",
            Self::Package => "package",
            Self::CompileStandard => "compile-standard",
            Self::Workspace => "workspace",
        }
    }
}

/// A resolved implementation standard plus where it came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ResolvedStandard<S> {
    pub standard: S,
    pub source: LanguageStandardSource,
}

/// A resolved interface standard plus where it came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct InterfaceStandard<S> {
    pub standard: S,
    pub source: InterfaceStandardSource,
}

/// Package-level effective implementation standards.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ResolvedLanguageStandards {
    pub c: ResolvedStandard<CStandard>,
    pub cxx: ResolvedStandard<CxxStandard>,
}

impl Default for ResolvedLanguageStandards {
    fn default() -> Self {
        resolve_language_standards(&LanguageStandardSettings::default())
    }
}

/// Map a package-level declaration to its resolved standard and
/// provenance: literal → `package`, workspace-inherited →
/// `workspace`, absent (or an unresolved marker, debug-asserted
/// here) → built-in default.
fn package_resolution<S: Copy>(
    declaration: Option<StandardDeclaration<S>>,
    default: S,
) -> ResolvedStandard<S> {
    match declaration {
        Some(StandardDeclaration::Declared(standard)) => ResolvedStandard {
            standard,
            source: LanguageStandardSource::Package,
        },
        Some(StandardDeclaration::Inherited(standard)) => ResolvedStandard {
            standard,
            source: LanguageStandardSource::Workspace,
        },
        Some(StandardDeclaration::Workspace) => {
            debug_assert!(
                false,
                "unresolved `{{ workspace = true }}` standard marker reached resolution"
            );
            ResolvedStandard {
                standard: default,
                source: LanguageStandardSource::BuiltinDefault,
            }
        }
        None => ResolvedStandard {
            standard: default,
            source: LanguageStandardSource::BuiltinDefault,
        },
    }
}

/// Resolve the package-level effective standards:
/// `[package]` declaration (literal or workspace-inherited) â–¶
/// built-in default.
#[must_use]
pub fn resolve_language_standards(package: &LanguageStandardSettings) -> ResolvedLanguageStandards {
    ResolvedLanguageStandards {
        c: package_resolution(package.c_standard, DEFAULT_C_STANDARD),
        cxx: package_resolution(package.cxx_standard, DEFAULT_CXX_STANDARD),
    }
}

/// Effective C implementation standard for one target:
/// target override â–¶ package (literal or workspace-inherited) â–¶
/// built-in default.
#[must_use]
pub fn effective_c(
    package: &ResolvedLanguageStandards,
    target: &Target,
) -> ResolvedStandard<CStandard> {
    target
        .language
        .c_standard_value()
        .map_or(package.c, |standard| ResolvedStandard {
            standard,
            source: LanguageStandardSource::Target,
        })
}

/// Effective C++ implementation standard for one target.
#[must_use]
pub fn effective_cxx(
    package: &ResolvedLanguageStandards,
    target: &Target,
) -> ResolvedStandard<CxxStandard> {
    target
        .language
        .cxx_standard_value()
        .map_or(package.cxx, |standard| ResolvedStandard {
            standard,
            source: LanguageStandardSource::Target,
        })
}

/// Map a package-level *interface* declaration to its provenance:
/// literal → `package`, workspace-inherited → `workspace`.
fn interface_resolution<S: Copy>(
    declaration: Option<StandardDeclaration<S>>,
) -> Option<InterfaceStandard<S>> {
    match declaration {
        Some(StandardDeclaration::Declared(standard)) => Some(InterfaceStandard {
            standard,
            source: InterfaceStandardSource::Package,
        }),
        Some(StandardDeclaration::Inherited(standard)) => Some(InterfaceStandard {
            standard,
            source: InterfaceStandardSource::Workspace,
        }),
        Some(StandardDeclaration::Workspace) => {
            debug_assert!(
                false,
                "unresolved `{{ workspace = true }}` standard marker reached resolution"
            );
            None
        }
        None => None,
    }
}

/// Effective C interface standard for a library-like target:
/// target interface â–¶ package interface (literal or
/// workspace-inherited) â–¶ the target's effective implementation
/// standard (literal defaulting — built-in default included).
#[must_use]
pub fn interface_c(
    package: &ResolvedLanguageStandards,
    package_settings: &LanguageStandardSettings,
    target: &Target,
) -> InterfaceStandard<CStandard> {
    if let Some(standard) = target.language.interface_c_standard_value() {
        return InterfaceStandard {
            standard,
            source: InterfaceStandardSource::Target,
        };
    }
    if let Some(interface) = interface_resolution(package_settings.interface_c_standard) {
        return interface;
    }
    InterfaceStandard {
        standard: effective_c(package, target).standard,
        source: InterfaceStandardSource::CompileStandard,
    }
}

/// Effective C++ interface standard for a library-like target.
#[must_use]
pub fn interface_cxx(
    package: &ResolvedLanguageStandards,
    package_settings: &LanguageStandardSettings,
    target: &Target,
) -> InterfaceStandard<CxxStandard> {
    if let Some(standard) = target.language.interface_cxx_standard_value() {
        return InterfaceStandard {
            standard,
            source: InterfaceStandardSource::Target,
        };
    }
    if let Some(interface) = interface_resolution(package_settings.interface_cxx_standard) {
        return interface;
    }
    InterfaceStandard {
        standard: effective_cxx(package, target).standard,
        source: InterfaceStandardSource::CompileStandard,
    }
}

/// Whether a dependency target imposes an interface requirement for
/// `language` on its consumers. A language is relevant when the
/// target has sources of that language, declares a target-level
/// field for it (implementation or interface), or is header-only
/// while the package declares a package-level *interface* standard
/// for it. Package-level implementation defaults never create
/// relevance by themselves.
#[must_use]
pub fn imposes_requirement(
    target: &Target,
    package_settings: &LanguageStandardSettings,
    language: SourceLanguage,
) -> bool {
    let has_sources = target
        .sources
        .iter()
        .any(|s| classify_source(s) == Some(language));
    let target_declares = match language {
        SourceLanguage::C => {
            target.language.c_standard.is_some() || target.language.interface_c_standard.is_some()
        }
        SourceLanguage::Cxx => {
            target.language.cxx_standard.is_some()
                || target.language.interface_cxx_standard.is_some()
        }
    };
    let header_only_package_interface = target.kind.is_header_only()
        && match language {
            SourceLanguage::C => package_settings.interface_c_standard.is_some(),
            SourceLanguage::Cxx => package_settings.interface_cxx_standard.is_some(),
        };
    has_sources || target_declares || header_only_package_interface
}

/// Token prefixes that select a language standard inside an
/// escape-hatch flag list.
pub const STANDARD_FLAG_PREFIXES: [&str; 3] = ["-std=", "--std=", "/std:"];

/// A first-class standard declaration conflicting with an explicit
/// standard flag in the same package's manifest-derived flags.
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[error(
    "package `{package}` declares a first-class {} standard (`{field}`) but its `{flag_list}` also select one via `{flag}`; remove the flag, or drop the `{field}` declaration and keep the raw flag",
    .language.human_label()
)]
pub struct StandardFlagConflict {
    pub package: String,
    pub language: SourceLanguage,
    /// The manifest field family that was declared (`c-standard` or
    /// `cxx-standard`, at package or target level).
    pub field: &'static str,
    /// The flag list carrying the conflicting token (`cflags` or
    /// `cxxflags`).
    pub flag_list: &'static str,
    pub flag: String,
    /// Scope of the conflicting declaration: `Some(target)` when a
    /// target-level field created it (the ambiguity exists only on
    /// that target's compiles), `None` when the package-level field
    /// did (every compile of the language is ambiguous). The build
    /// planner uses the scope to surface a conflict only when a
    /// matching compile is actually planned.
    pub target: Option<String>,
}

fn first_standard_token(flags: &[String]) -> Option<String> {
    flags
        .iter()
        .find(|f| STANDARD_FLAG_PREFIXES.iter().any(|p| f.starts_with(p)))
        .cloned()
}

/// Detect the documented conflict candidates: an explicit
/// first-class implementation standard declaration (package or
/// target level) for a language whose manifest-derived flag list
/// also pins a standard. Runs on resolved flags *before* env /
/// pkg-config augmentation so `CFLAGS` / `CXXFLAGS` remain exempt.
///
/// These are *candidates*, scoped per declaration: the build
/// planner surfaces one only when a compile its scope covers is
/// actually planned, so an unbuilt sibling target's declaration
/// never gates a command that does not compile it.
#[must_use]
pub fn find_standard_flag_conflicts(
    package: &str,
    settings: &LanguageStandardSettings,
    targets: &[Target],
    flags: &ResolvedProfileFlags,
) -> Vec<StandardFlagConflict> {
    let mut out = Vec::new();
    let mut push = |language: SourceLanguage,
                    field: &'static str,
                    flag_list: &'static str,
                    flag: String,
                    target: Option<String>| {
        out.push(StandardFlagConflict {
            package: package.to_owned(),
            language,
            field,
            flag_list,
            flag,
            target,
        });
    };
    if let Some(flag) = first_standard_token(&flags.cflags) {
        if settings.c_standard.is_some() {
            push(
                SourceLanguage::C,
                "c-standard",
                "cflags",
                flag.clone(),
                None,
            );
        } else {
            for target in targets {
                if target.language.c_standard.is_some() {
                    push(
                        SourceLanguage::C,
                        "c-standard",
                        "cflags",
                        flag.clone(),
                        Some(target.name.as_str().to_owned()),
                    );
                }
            }
        }
    }
    if let Some(flag) = first_standard_token(&flags.cxxflags) {
        if settings.cxx_standard.is_some() {
            push(
                SourceLanguage::Cxx,
                "cxx-standard",
                "cxxflags",
                flag.clone(),
                None,
            );
        } else {
            for target in targets {
                if target.language.cxx_standard.is_some() {
                    push(
                        SourceLanguage::Cxx,
                        "cxx-standard",
                        "cxxflags",
                        flag.clone(),
                        Some(target.name.as_str().to_owned()),
                    );
                }
            }
        }
    }
    out
}

/// Per-package language-standard summary carried by
/// `BuildConfiguration`: package-level effective standards plus the
/// effective values for every target. Values (not provenance) feed
/// the fingerprint; the whole struct feeds `cabin metadata` /
/// `cabin explain build-config`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LanguageStandardsSummary {
    pub c: ResolvedStandard<CStandard>,
    pub cxx: ResolvedStandard<CxxStandard>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub targets: BTreeMap<String, TargetStandardsSummary>,
}

/// Effective standards for one target. Interface entries are
/// present only for `library` / `header-only` kinds.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TargetStandardsSummary {
    pub c: ResolvedStandard<CStandard>,
    pub cxx: ResolvedStandard<CxxStandard>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub interface_c: Option<InterfaceStandard<CStandard>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub interface_cxx: Option<InterfaceStandard<CxxStandard>>,
}

impl Default for LanguageStandardsSummary {
    fn default() -> Self {
        let resolved = ResolvedLanguageStandards::default();
        Self {
            c: resolved.c,
            cxx: resolved.cxx,
            targets: BTreeMap::new(),
        }
    }
}

impl LanguageStandardsSummary {
    /// Compute the summary from a package's declarations.
    #[must_use]
    pub fn from_package(package: &crate::Package) -> Self {
        let resolved = resolve_language_standards(&package.language);
        let targets = package
            .targets
            .iter()
            .map(|target| {
                let library_like = target.kind.produces_archive() || target.kind.is_header_only();
                let summary = TargetStandardsSummary {
                    c: effective_c(&resolved, target),
                    cxx: effective_cxx(&resolved, target),
                    interface_c: library_like
                        .then(|| interface_c(&resolved, &package.language, target)),
                    interface_cxx: library_like
                        .then(|| interface_cxx(&resolved, &package.language, target)),
                };
                (target.name.as_str().to_owned(), summary)
            })
            .collect();
        Self {
            c: resolved.c,
            cxx: resolved.cxx,
            targets,
        }
    }

    /// Stable line serialization for the build-configuration
    /// fingerprint. Values only — provenance must not move the
    /// fingerprint.
    #[must_use]
    pub fn fingerprint_lines(&self) -> Vec<String> {
        let mut lines = vec![
            format!("c={}", self.c.standard),
            format!("cxx={}", self.cxx.standard),
        ];
        for (name, target) in &self.targets {
            lines.push(format!("target={name}"));
            lines.push(format!("c={}", target.c.standard));
            lines.push(format!("cxx={}", target.cxx.standard));
            if let Some(interface) = &target.interface_c {
                lines.push(format!("interface-c={}", interface.standard));
            }
            if let Some(interface) = &target.interface_cxx {
                lines.push(format!("interface-cxx={}", interface.standard));
            }
        }
        lines
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{TargetKind, TargetName};
    use camino::Utf8PathBuf;

    fn target(kind: TargetKind, sources: &[&str], language: LanguageStandardSettings) -> Target {
        Target {
            name: TargetName::new("t").unwrap(),
            kind,
            sources: sources.iter().map(Utf8PathBuf::from).collect(),
            include_dirs: Vec::new(),
            defines: Vec::new(),
            deps: Vec::new(),
            language,
        }
    }

    #[test]
    fn standards_parse_round_trip_and_reject_unknown_values() {
        for s in CStandard::ALL {
            assert_eq!(CStandard::parse(s.as_str()).unwrap(), s);
        }
        for s in CxxStandard::ALL {
            assert_eq!(CxxStandard::parse(s.as_str()).unwrap(), s);
        }
        let err = CStandard::parse("c++17").unwrap_err();
        assert!(
            err.to_string()
                .contains("expected one of c89, c99, c11, c17, c23"),
            "unexpected message: {err}"
        );
        let err = CxxStandard::parse("c++26").unwrap_err();
        assert!(
            err.to_string().contains("c++23"),
            "unexpected message: {err}"
        );
        assert!(
            err.to_string().contains("unknown C++ standard `c++26`"),
            "unexpected message: {err}"
        );
    }

    #[test]
    fn standards_order_chronologically() {
        assert!(CStandard::C99 < CStandard::C23);
        assert!(CStandard::C11 < CStandard::C17);
        assert!(CxxStandard::Cxx14 < CxxStandard::Cxx20);
        assert!(CxxStandard::Cxx98 < CxxStandard::Cxx03);
    }

    #[test]
    fn msvc_spellings_cover_exactly_the_stable_flags() {
        assert_eq!(
            LanguageStandard::Cxx(CxxStandard::Cxx20).msvc_spelling(),
            Some("/std:c++20")
        );
        assert_eq!(
            LanguageStandard::C(CStandard::C17).msvc_spelling(),
            Some("/std:c17")
        );
        assert_eq!(LanguageStandard::C(CStandard::C99).msvc_spelling(), None);
        assert_eq!(
            LanguageStandard::Cxx(CxxStandard::Cxx23).msvc_spelling(),
            None
        );
        assert_eq!(
            LanguageStandard::Cxx(CxxStandard::Cxx11).msvc_spelling(),
            None
        );
    }

    #[test]
    fn effective_standard_prefers_target_then_package_then_builtin() {
        let undeclared = resolve_language_standards(&LanguageStandardSettings::default());
        let plain = target(
            TargetKind::Executable,
            &["a.cc"],
            LanguageStandardSettings::default(),
        );
        let effective = effective_cxx(&undeclared, &plain);
        assert_eq!(effective.standard, CxxStandard::Cxx17);
        assert_eq!(effective.source, LanguageStandardSource::BuiltinDefault);

        let package = resolve_language_standards(&LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx14)),
            ..Default::default()
        });
        let effective = effective_cxx(&package, &plain);
        assert_eq!(effective.standard, CxxStandard::Cxx14);
        assert_eq!(effective.source, LanguageStandardSource::Package);

        let overridden = target(
            TargetKind::Executable,
            &["a.cc"],
            LanguageStandardSettings {
                cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
                ..Default::default()
            },
        );
        let effective = effective_cxx(&package, &overridden);
        assert_eq!(effective.standard, CxxStandard::Cxx20);
        assert_eq!(effective.source, LanguageStandardSource::Target);
    }

    #[test]
    fn interface_standard_falls_back_to_effective_compile_standard() {
        let package_settings = LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
            ..Default::default()
        };
        let resolved = resolve_language_standards(&package_settings);
        let lib = target(
            TargetKind::Library,
            &["a.cc"],
            LanguageStandardSettings::default(),
        );
        let interface = interface_cxx(&resolved, &package_settings, &lib);
        assert_eq!(interface.standard, CxxStandard::Cxx20);
        assert_eq!(interface.source, InterfaceStandardSource::CompileStandard);

        let package_interface = LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
            interface_cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx17)),
            ..Default::default()
        };
        let resolved = resolve_language_standards(&package_interface);
        let interface = interface_cxx(&resolved, &package_interface, &lib);
        assert_eq!(interface.standard, CxxStandard::Cxx17);
        assert_eq!(interface.source, InterfaceStandardSource::Package);

        let lib_override = target(
            TargetKind::Library,
            &["a.cc"],
            LanguageStandardSettings {
                interface_cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx14)),
                ..Default::default()
            },
        );
        let interface = interface_cxx(&resolved, &package_interface, &lib_override);
        assert_eq!(interface.standard, CxxStandard::Cxx14);
        assert_eq!(interface.source, InterfaceStandardSource::Target);
    }

    #[test]
    fn imposes_requirement_relevance_rules() {
        let none = LanguageStandardSettings::default();
        // A pure-C library imposes no C++ requirement.
        let c_lib = target(
            TargetKind::Library,
            &["a.c"],
            LanguageStandardSettings::default(),
        );
        assert!(imposes_requirement(&c_lib, &none, SourceLanguage::C));
        assert!(!imposes_requirement(&c_lib, &none, SourceLanguage::Cxx));

        // A package-level *implementation* default alone creates no
        // relevance for a target without that language.
        let package_impl = LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
            ..Default::default()
        };
        assert!(!imposes_requirement(
            &c_lib,
            &package_impl,
            SourceLanguage::Cxx
        ));

        // A target-level field (implementation or interface) does.
        let declared = target(
            TargetKind::Library,
            &["a.c"],
            LanguageStandardSettings {
                interface_cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx17)),
                ..Default::default()
            },
        );
        assert!(imposes_requirement(&declared, &none, SourceLanguage::Cxx));

        // Header-only + package-level *interface* standard does.
        let header_only = target(
            TargetKind::HeaderOnly,
            &[],
            LanguageStandardSettings::default(),
        );
        assert!(!imposes_requirement(
            &header_only,
            &none,
            SourceLanguage::Cxx
        ));
        let package_interface = LanguageStandardSettings {
            interface_cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
            ..Default::default()
        };
        assert!(imposes_requirement(
            &header_only,
            &package_interface,
            SourceLanguage::Cxx
        ));
        // ... but not via a package-level implementation default.
        assert!(!imposes_requirement(
            &header_only,
            &package_impl,
            SourceLanguage::Cxx
        ));
    }

    #[test]
    fn conflict_fires_only_for_declared_language_and_matching_bucket() {
        let flags = ResolvedProfileFlags {
            cxxflags: vec!["-std=c++14".to_owned()],
            ..Default::default()
        };
        let declared_cxx = LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx17)),
            ..Default::default()
        };

        // Nothing declared: never a conflict.
        assert!(
            find_standard_flag_conflicts("p", &LanguageStandardSettings::default(), &[], &flags)
                .is_empty()
        );

        // Declared C++ + `-std=` in cxxflags: a package-scoped
        // conflict candidate.
        let conflicts = find_standard_flag_conflicts("p", &declared_cxx, &[], &flags);
        let conflict = conflicts.first().unwrap();
        assert_eq!(conflicts.len(), 1);
        assert_eq!(conflict.language, SourceLanguage::Cxx);
        assert_eq!(conflict.flag, "-std=c++14");
        assert_eq!(conflict.field, "cxx-standard");
        assert_eq!(conflict.target, None);
        assert!(conflict.to_string().contains("cxx-standard"));

        // Declared C++ + `-std=` in cflags only: no conflict.
        let c_only_flags = ResolvedProfileFlags {
            cflags: vec!["-std=c99".to_owned()],
            ..Default::default()
        };
        assert!(find_standard_flag_conflicts("p", &declared_cxx, &[], &c_only_flags).is_empty());

        // A target-level declaration counts as declared.
        let t = target(
            TargetKind::Executable,
            &["a.c"],
            LanguageStandardSettings {
                c_standard: Some(StandardDeclaration::Declared(CStandard::C17)),
                ..Default::default()
            },
        );
        let conflicts = find_standard_flag_conflicts(
            "p",
            &LanguageStandardSettings::default(),
            std::slice::from_ref(&t),
            &c_only_flags,
        );
        let conflict = conflicts.first().unwrap();
        assert_eq!(conflict.language, SourceLanguage::C);
        assert_eq!(conflict.flag_list, "cflags");
        // A target-level declaration scopes the candidate to that
        // target so the planner only surfaces it when the target's
        // compile is planned.
        assert_eq!(conflict.target.as_deref(), Some("t"));

        // `/std:` and `--std=` prefixes are recognized too.
        let msvc_flags = ResolvedProfileFlags {
            cxxflags: vec!["/std:c++latest".to_owned()],
            ..Default::default()
        };
        assert!(!find_standard_flag_conflicts("p", &declared_cxx, &[], &msvc_flags).is_empty());
    }

    #[test]
    fn summary_lists_every_target_with_interface_only_for_library_like() {
        use crate::{Package, PackageName};
        let package = Package::new(
            PackageName::new("demo").unwrap(),
            semver::Version::parse("0.1.0").unwrap(),
            vec![
                target(
                    TargetKind::Executable,
                    &["main.cc"],
                    LanguageStandardSettings::default(),
                ),
                Target {
                    name: TargetName::new("core").unwrap(),
                    kind: TargetKind::Library,
                    sources: vec![Utf8PathBuf::from("core.cc")],
                    include_dirs: Vec::new(),
                    defines: Vec::new(),
                    deps: Vec::new(),
                    language: LanguageStandardSettings {
                        cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
                        interface_cxx_standard: Some(StandardDeclaration::Declared(
                            CxxStandard::Cxx17,
                        )),
                        ..Default::default()
                    },
                },
            ],
            Vec::new(),
        )
        .unwrap();
        let summary = LanguageStandardsSummary::from_package(&package);
        assert_eq!(summary.cxx.standard, CxxStandard::Cxx17);
        assert_eq!(summary.targets.len(), 2);
        let exe = &summary.targets["t"];
        assert!(exe.interface_c.is_none() && exe.interface_cxx.is_none());
        let core = &summary.targets["core"];
        assert_eq!(core.cxx.standard, CxxStandard::Cxx20);
        assert_eq!(core.interface_cxx.unwrap().standard, CxxStandard::Cxx17);
        assert_eq!(
            core.interface_c.unwrap().source,
            InterfaceStandardSource::CompileStandard
        );
    }

    #[test]
    fn package_level_interface_fields_are_inert_without_library_like_targets() {
        use crate::{Package, PackageName};
        // docs/language-standards.md: package-level interface fields
        // are "allowed, and inert, in packages without any"
        // library-like target — the summary must not attach them to
        // executables.
        let package = Package::new(
            PackageName::new("demo").unwrap(),
            semver::Version::parse("0.1.0").unwrap(),
            vec![target(
                TargetKind::Executable,
                &["main.cc"],
                LanguageStandardSettings::default(),
            )],
            Vec::new(),
        )
        .unwrap()
        .with_language(LanguageStandardSettings {
            interface_c_standard: Some(StandardDeclaration::Declared(CStandard::C17)),
            interface_cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
            ..Default::default()
        });
        let summary = LanguageStandardsSummary::from_package(&package);
        let exe = &summary.targets["t"];
        assert!(
            exe.interface_c.is_none() && exe.interface_cxx.is_none(),
            "package-level interface fields must stay inert on executables"
        );
    }

    #[test]
    fn fingerprint_lines_are_values_only_and_deterministic() {
        let mut summary = LanguageStandardsSummary::default();
        let lines = summary.fingerprint_lines();
        assert_eq!(lines, vec!["c=c11".to_owned(), "cxx=c++17".to_owned()]);

        // Provenance must not appear in the lines.
        summary.cxx = ResolvedStandard {
            standard: CxxStandard::Cxx17,
            source: LanguageStandardSource::Package,
        };
        assert_eq!(summary.fingerprint_lines(), lines);

        summary.targets.insert(
            "core".to_owned(),
            TargetStandardsSummary {
                c: summary.c,
                cxx: ResolvedStandard {
                    standard: CxxStandard::Cxx20,
                    source: LanguageStandardSource::Target,
                },
                interface_c: None,
                interface_cxx: Some(InterfaceStandard {
                    standard: CxxStandard::Cxx17,
                    source: InterfaceStandardSource::Target,
                }),
            },
        );
        assert_eq!(
            summary.fingerprint_lines(),
            vec![
                "c=c11".to_owned(),
                "cxx=c++17".to_owned(),
                "target=core".to_owned(),
                "c=c11".to_owned(),
                "cxx=c++20".to_owned(),
                "interface-cxx=c++17".to_owned(),
            ]
        );
    }

    #[test]
    fn standard_declaration_serde_is_a_bare_string_and_rejects_markers() {
        let declared: StandardDeclaration<CxxStandard> =
            StandardDeclaration::Declared(CxxStandard::Cxx20);
        let inherited: StandardDeclaration<CxxStandard> =
            StandardDeclaration::Inherited(CxxStandard::Cxx20);
        assert_eq!(serde_json::to_string(&declared).unwrap(), "\"c++20\"");
        assert_eq!(serde_json::to_string(&inherited).unwrap(), "\"c++20\"");
        let marker: StandardDeclaration<CxxStandard> = StandardDeclaration::Workspace;
        assert!(serde_json::to_string(&marker).is_err());
        let parsed: StandardDeclaration<CxxStandard> = serde_json::from_str("\"c++20\"").unwrap();
        assert_eq!(parsed, StandardDeclaration::Declared(CxxStandard::Cxx20));
    }

    #[test]
    fn inherited_standard_resolves_with_workspace_source() {
        let settings = LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Inherited(CxxStandard::Cxx20)),
            ..Default::default()
        };
        let resolved = resolve_language_standards(&settings);
        assert_eq!(resolved.cxx.standard, CxxStandard::Cxx20);
        assert_eq!(resolved.cxx.source, LanguageStandardSource::Workspace);
        assert_eq!(resolved.c.source, LanguageStandardSource::BuiltinDefault);
    }

    #[test]
    fn inherited_interface_standard_resolves_with_workspace_source() {
        let settings = LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Declared(CxxStandard::Cxx20)),
            interface_cxx_standard: Some(StandardDeclaration::Inherited(CxxStandard::Cxx17)),
            ..Default::default()
        };
        let resolved = resolve_language_standards(&settings);
        let lib = target(
            TargetKind::Library,
            &["a.cc"],
            LanguageStandardSettings::default(),
        );
        let interface = interface_cxx(&resolved, &settings, &lib);
        assert_eq!(interface.standard, CxxStandard::Cxx17);
        assert_eq!(interface.source, InterfaceStandardSource::Workspace);
    }

    #[test]
    fn inherited_values_behave_like_declarations_for_conflicts_and_relevance() {
        let flags = ResolvedProfileFlags {
            cxxflags: vec!["-std=gnu++20".to_owned()],
            ..Default::default()
        };
        let inherited = LanguageStandardSettings {
            cxx_standard: Some(StandardDeclaration::Inherited(CxxStandard::Cxx17)),
            ..Default::default()
        };
        assert_eq!(
            find_standard_flag_conflicts("p", &inherited, &[], &flags).len(),
            1
        );

        let header_only = target(
            TargetKind::HeaderOnly,
            &[],
            LanguageStandardSettings::default(),
        );
        let pkg = LanguageStandardSettings {
            interface_cxx_standard: Some(StandardDeclaration::Inherited(CxxStandard::Cxx20)),
            ..Default::default()
        };
        assert!(imposes_requirement(&header_only, &pkg, SourceLanguage::Cxx));
    }

    #[test]
    fn fingerprint_is_identical_for_declared_and_inherited_values() {
        use crate::{Package, PackageName};
        let make = |decl: StandardDeclaration<CxxStandard>| {
            let package = Package::new(
                PackageName::new("demo").unwrap(),
                semver::Version::parse("0.1.0").unwrap(),
                vec![target(
                    TargetKind::Executable,
                    &["main.cc"],
                    LanguageStandardSettings::default(),
                )],
                Vec::new(),
            )
            .unwrap()
            .with_language(LanguageStandardSettings {
                cxx_standard: Some(decl),
                ..Default::default()
            });
            LanguageStandardsSummary::from_package(&package).fingerprint_lines()
        };
        assert_eq!(
            make(StandardDeclaration::Declared(CxxStandard::Cxx20)),
            make(StandardDeclaration::Inherited(CxxStandard::Cxx20))
        );
    }

    #[test]
    fn workspace_marker_field_reports_the_first_marker() {
        assert_eq!(
            LanguageStandardSettings::default().workspace_marker_field(),
            None
        );
        let settings = LanguageStandardSettings {
            interface_c_standard: Some(StandardDeclaration::Workspace),
            ..Default::default()
        };
        assert_eq!(
            settings.workspace_marker_field(),
            Some("interface-c-standard")
        );
        let settings = LanguageStandardSettings {
            c_standard: Some(StandardDeclaration::Workspace),
            interface_c_standard: Some(StandardDeclaration::Workspace),
            ..Default::default()
        };
        assert_eq!(settings.workspace_marker_field(), Some("c-standard"));
    }
}