nsh-cli 0.7.0

The nsh command line tool: build, package and run apps.
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
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
//! `nsh.json` `"ios"` and `"macos"`: the `Info.plist` keys and the
//! entitlements an app adds to what `nsh package` writes for it, and how a
//! macOS app takes part in the system's UI (`macos.activationPolicy`), and
//! what iOS shows while the app starts (`ios.launchScreen`).
//!
//! Values are JSON, written as property list values (strings, numbers,
//! booleans, arrays, dictionaries). The keys NSH sets from other `nsh.json`
//! keys or that the runtime depends on are NSH's ([`ApplePlatform::reserved`]):
//! an app setting one is an error that names what to set instead. Every other
//! key the app sets replaces NSH's default, if it has one. The oldest system
//! the app runs on (`MinimumOSVersion`, `LSMinimumSystemVersion`) is such a
//! default, which an app raises for the APIs of a later system but never
//! lowers below the runtime's own ([`IOS_MINIMUM`], [`MACOS_MINIMUM`]);
//! builds of the app's Rust code compile for it too ([`AppleMinimums`]).
//!
//! `<platform>.infoPlistStrings` gives the text of `Info.plist` strings in
//! other languages, by language: packages write each language's
//! `<lang>.lproj/InfoPlist.strings`, which the system reads in place of
//! the `Info.plist` value for users of that language. The `Info.plist`
//! value is the text of every other language, so a translated key needs
//! one there ([`localized_info`]).

use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
};

use anyhow::bail;
use serde::Deserialize;

/// The oldest iOS the runtime runs on. Packages declare it
/// (`MinimumOSVersion`, and `actool`'s deployment target) unless the app
/// raises it, and every build `nsh` makes of an app's Rust code for iOS
/// compiles its Rust and C for it ([`AppleMinimums::deployment_target`]),
/// as nsh's own builds do (`.cargo/config.toml`'s
/// `IPHONEOS_DEPLOYMENT_TARGET`, `cargo xtask dist`).
pub(crate) const IOS_MINIMUM: &str = "15.0";

/// The oldest macOS the AppKit runtime runs on (its display links are
/// `NSView`'s, new in macOS 14). Packages declare it
/// (`LSMinimumSystemVersion`) unless the app raises it, and builds of an
/// app's Rust code for macOS compile for it as for iOS.
pub(crate) const MACOS_MINIMUM: &str = "14.0";

/// The oldest systems an app runs on, as its packages declare them: its
/// own `<platform>.infoPlist` minimum when it raises the runtime's, else
/// the runtime's.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AppleMinimums {
    /// iOS's (Mac Catalyst's too: its versions are iOS's).
    ios: String,
    /// macOS's.
    macos: String,
}

impl Default for AppleMinimums {
    fn default() -> Self {
        AppleMinimums {
            ios: IOS_MINIMUM.into(),
            macos: MACOS_MINIMUM.into(),
        }
    }
}

impl AppleMinimums {
    /// The minimums of the app whose `nsh.json` is `config`. A minimum the
    /// app sets is checked as packages check it; a section of another form
    /// is left to the reading of the whole `nsh.json` to reject.
    pub(crate) fn of(config: &serde_json::Value) -> anyhow::Result<Self> {
        let minimum = |platform: ApplePlatform| -> anyhow::Result<String> {
            let Some(value) = config
                .get(platform.key())
                .and_then(|section| section.get("infoPlist"))
                .and_then(|info| info.get(platform.minimum_key()))
            else {
                return Ok(platform.minimum().into());
            };
            check_minimum(value, platform, &format!("{}.infoPlist", platform.key()))?;
            Ok(value.as_str().unwrap_or_default().into())
        };
        Ok(AppleMinimums {
            ios: minimum(ApplePlatform::Ios)?,
            macos: minimum(ApplePlatform::Macos)?,
        })
    }

    /// The deployment target variable and version of a build for `triple`
    /// (`None`: this machine) when it is an Apple system's. rustc links for
    /// it and `cc` compiles C for it; without it they disagree (rustc's
    /// old default against the SDK's own version), and the C code may need
    /// a later system than the package declares, or fail to link.
    pub(crate) fn deployment_target(&self, triple: Option<&str>) -> Option<(&'static str, &str)> {
        let platform = match triple {
            Some(triple) if triple.contains("-apple-ios") => ApplePlatform::Ios,
            Some(triple) if triple.contains("-apple-darwin") => ApplePlatform::Macos,
            Some(_) => return None,
            None if cfg!(target_os = "macos") => ApplePlatform::Macos,
            None => return None,
        };
        Some(match platform {
            ApplePlatform::Ios => ("IPHONEOS_DEPLOYMENT_TARGET", &self.ios),
            ApplePlatform::Macos => ("MACOSX_DEPLOYMENT_TARGET", &self.macos),
        })
    }
}

/// `nsh.json` `"ios"` (and the keys [`MacosConfig`] shares with it).
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct AppleConfig {
    /// Keys added to the app's `Info.plist`.
    #[serde(default)]
    pub(crate) info_plist: serde_json::Map<String, serde_json::Value>,
    /// The text of `Info.plist` strings in other languages, by language
    /// (`{ "zh-Hans": { "NSCameraUsageDescription": ".." } }`).
    #[serde(default)]
    pub(crate) info_plist_strings: serde_json::Map<String, serde_json::Value>,
    /// Entitlements the app is signed with. iOS device builds check them
    /// against the provisioning profile.
    #[serde(default)]
    pub(crate) entitlements: serde_json::Map<String, serde_json::Value>,
    /// What iOS shows from the tap on the app's icon until the app's first
    /// frame: the system's background without it.
    pub(crate) launch_screen: Option<LaunchScreen>,
    /// App extensions the app builds itself, which iOS packages carry in
    /// `PlugIns/` ([`crate::app_extensions`]).
    #[serde(default)]
    pub(crate) extensions: Vec<crate::app_extensions::AppExtension>,
    /// The app's privacy manifest, which iOS packages carry as
    /// `PrivacyInfo.xcprivacy` ([`keep_privacy_manifest`] leaves out what
    /// is not one of its keys in its form).
    #[serde(default)]
    pub(crate) privacy_manifest: serde_json::Map<String, serde_json::Value>,
}

/// `nsh.json` `ios.launchScreen`: a background color and an image in the
/// middle of the screen, each one for both appearances or one for each.
/// Packages compile them into the app's asset catalog and name them in
/// `UILaunchScreen`, so the system draws them before the app runs.
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LaunchScreen {
    /// The screen's color (`"#131312"`, or `{ "light": .., "dark": .. }`).
    pub(crate) background: Option<Themed<Color>>,
    /// A PNG relative to `nsh.json`, drawn for 3x screens: the system
    /// shows it centered, at a third of its pixel size in points.
    pub(crate) image: Option<Themed<PathBuf>>,
}

/// One value for both appearances, or one for each.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(untagged)]
pub(crate) enum Themed<T> {
    /// The value in light and dark appearance.
    Both(T),
    /// A value for each appearance.
    Each {
        /// In light appearance.
        light: T,
        /// In dark appearance.
        dark: T,
    },
}

impl<T> Themed<T> {
    /// The light value, and the dark one when it differs.
    pub(crate) fn variants(&self) -> (&T, Option<&T>) {
        match self {
            Themed::Both(value) => (value, None),
            Themed::Each { light, dark } => (light, Some(dark)),
        }
    }

    fn map<U>(self, f: impl Fn(T) -> U) -> Themed<U> {
        match self {
            Themed::Both(value) => Themed::Both(f(value)),
            Themed::Each { light, dark } => Themed::Each {
                light: f(light),
                dark: f(dark),
            },
        }
    }
}

/// An sRGB color written `#rrggbb` or `#rrggbbaa`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(try_from = "String")]
pub(crate) struct Color(pub(crate) [u8; 4]);

impl TryFrom<String> for Color {
    type Error = String;

    fn try_from(text: String) -> Result<Self, String> {
        let digits = text.strip_prefix('#').unwrap_or_default();
        let byte = |i: usize| u8::from_str_radix(digits.get(i * 2..i * 2 + 2)?, 16).ok();
        let alpha = match digits.len() {
            6 => Some(255),
            8 => byte(3),
            _ => None,
        };
        // Digits only: `from_str_radix` also takes a sign.
        let hex = digits.bytes().all(|b| b.is_ascii_hexdigit());
        match (hex, byte(0), byte(1), byte(2), alpha) {
            (true, Some(r), Some(g), Some(b), Some(a)) => Ok(Color([r, g, b, a])),
            _ => Err(format!(
                "{text:?} is no color; write `#rrggbb` or `#rrggbbaa`"
            )),
        }
    }
}

impl LaunchScreen {
    /// The same, with its image relative to `dir`.
    pub(crate) fn relative_to(self, dir: &Path) -> Self {
        LaunchScreen {
            image: self.image.map(|image| image.map(|path| dir.join(path))),
            ..self
        }
    }
}

/// `nsh.json` `"macos"`: [`AppleConfig`]'s keys, and the app's activation
/// policy, which iOS has no counterpart of.
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct MacosConfig {
    /// Keys added to the app's `Info.plist`.
    #[serde(default)]
    pub(crate) info_plist: serde_json::Map<String, serde_json::Value>,
    /// The text of `Info.plist` strings in other languages, as `ios`'s.
    #[serde(default)]
    pub(crate) info_plist_strings: serde_json::Map<String, serde_json::Value>,
    /// Entitlements the app is signed with.
    #[serde(default)]
    pub(crate) entitlements: serde_json::Map<String, serde_json::Value>,
    /// How the app takes part in the system's UI. Every way NSH starts it
    /// on macOS applies it before the app's code runs: packages say so in
    /// their `Info.plist` (`LSUIElement`) and `app.json` (as the Rust apps
    /// `nsh run` runs in place do), `nsh run` and `nsh test` pass it to the
    /// development host (`--activation-policy`).
    #[serde(default)]
    pub(crate) activation_policy: ActivationPolicy,
    /// App extensions the app builds itself, which macOS packages carry in
    /// `Contents/PlugIns/` ([`crate::app_extensions`]).
    #[serde(default)]
    pub(crate) extensions: Vec<crate::app_extensions::AppExtension>,
}

/// `nsh.json` `macos.activationPolicy`, as GPUI's `ActivationPolicy`. Read
/// from its name only: serde's own enums also take an object
/// (`{ "accessory": null }`), and one `nsh.json`'s reader could not leave
/// out as it does a value of another form.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(try_from = "String")]
pub(crate) enum ActivationPolicy {
    /// A Dock icon, the menu bar while the app is active, a place in the
    /// app switcher.
    #[default]
    Regular,
    /// None of them: a menu bar app.
    Accessory,
}

impl ActivationPolicy {
    /// Its name in `nsh.json`, `app.json` and the development host's
    /// `--activation-policy`.
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            ActivationPolicy::Regular => "regular",
            ActivationPolicy::Accessory => "accessory",
        }
    }
}

impl TryFrom<String> for ActivationPolicy {
    type Error = String;

    fn try_from(name: String) -> Result<Self, String> {
        match name.as_str() {
            "regular" => Ok(ActivationPolicy::Regular),
            "accessory" => Ok(ActivationPolicy::Accessory),
            _ => Err(format!(
                "unknown variant `{name}`, expected `regular` or `accessory`"
            )),
        }
    }
}

/// The Apple platform an [`AppleConfig`] is for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ApplePlatform {
    Ios,
    Macos,
}

/// Why a key is NSH's to set.
#[derive(Debug, Clone, Copy)]
enum Owner {
    /// Set from this `nsh.json` key.
    Config(&'static str),
    /// Set by the packaging itself: the runtime or the system depends on it.
    Package,
}

/// The `Info.plist` keys of every Apple package that are NSH's.
const RESERVED_INFO: [(&str, Owner); 9] = [
    ("CFBundleIdentifier", Owner::Config("id")),
    ("CFBundleName", Owner::Config("name")),
    ("CFBundleDisplayName", Owner::Config("name")),
    ("CFBundleShortVersionString", Owner::Config("version")),
    ("CFBundleVersion", Owner::Config("build")),
    ("CFBundleURLTypes", Owner::Config("schemes")),
    ("CFBundleDocumentTypes", Owner::Config("fileTypes")),
    ("CFBundleExecutable", Owner::Package),
    ("CFBundlePackageType", Owner::Package),
];

/// Whether `key` of an Apple app's `Info.plist` is one NSH sets itself.
pub(crate) fn is_reserved_info(platform: ApplePlatform, key: &str) -> bool {
    platform.reserved().any(|(k, _)| *k == key)
}

/// Whether `key` of an Apple app's entitlements is one NSH sets itself
/// ([`RESERVED_ENTITLEMENTS`]).
pub(crate) fn is_reserved_entitlement(key: &str) -> bool {
    RESERVED_ENTITLEMENTS.iter().any(|(k, _)| *k == key)
}

/// The `Info.plist` keys of iOS packages that are NSH's, besides
/// [`RESERVED_INFO`].
const RESERVED_IOS_INFO: [(&str, Owner); 10] = [
    ("CFBundleIcons", Owner::Config("icon")),
    ("CFBundleIcons~ipad", Owner::Config("icon")),
    ("CFBundleIconName", Owner::Config("icon")),
    ("UIUserInterfaceStyle", Owner::Config("appearance")),
    ("UIStatusBarStyle", Owner::Config("statusBar")),
    ("UIStatusBarHidden", Owner::Config("statusBar")),
    ("CFBundleSupportedPlatforms", Owner::Package),
    ("LSRequiresIPhoneOS", Owner::Package),
    ("UIApplicationSceneManifest", Owner::Package),
    ("UIApplicationSupportsMultipleScenes", Owner::Package),
];

/// The `Info.plist` keys of macOS packages that are NSH's, besides
/// [`RESERVED_INFO`].
const RESERVED_MACOS_INFO: [(&str, Owner); 4] = [
    ("CFBundleIconFile", Owner::Config("icon")),
    ("LSUIElement", Owner::Config("macos.activationPolicy")),
    ("CFBundleSupportedPlatforms", Owner::Package),
    ("NSPrincipalClass", Owner::Package),
];

/// The entitlements that are NSH's: the app's identity, which signing
/// derives from its `id` and the provisioning profile.
const RESERVED_ENTITLEMENTS: [(&str, Owner); 3] = [
    ("application-identifier", Owner::Config("id")),
    ("com.apple.application-identifier", Owner::Config("id")),
    ("com.apple.developer.team-identifier", Owner::Package),
];

impl ApplePlatform {
    /// Its key in `nsh.json`.
    pub(crate) fn key(self) -> &'static str {
        match self {
            ApplePlatform::Ios => "ios",
            ApplePlatform::Macos => "macos",
        }
    }

    /// The `Info.plist` keys NSH sets on this platform that apps may not.
    fn reserved(self) -> impl Iterator<Item = &'static (&'static str, Owner)> + Clone {
        let own: &'static [(&str, Owner)] = match self {
            ApplePlatform::Ios => &RESERVED_IOS_INFO,
            ApplePlatform::Macos => &RESERVED_MACOS_INFO,
        };
        RESERVED_INFO.iter().chain(own)
    }

    /// Whether apps translate `key` (`infoPlistStrings`): any key but
    /// those NSH sets ([`Self::reserved`]), of which only the app's name.
    fn translatable(self, key: &str) -> bool {
        TRANSLATED_RESERVED_INFO.contains(&key) || !self.reserved().any(|(k, _)| *k == key)
    }

    /// The system's name, for errors.
    fn os(self) -> &'static str {
        match self {
            ApplePlatform::Ios => "iOS",
            ApplePlatform::Macos => "macOS",
        }
    }

    /// The `Info.plist` key of the oldest system version the app runs on.
    fn minimum_key(self) -> &'static str {
        match self {
            ApplePlatform::Ios => "MinimumOSVersion",
            ApplePlatform::Macos => "LSMinimumSystemVersion",
        }
    }

    /// The oldest system version the runtime runs on: the
    /// [`minimum_key`](Self::minimum_key) of packages whose app does not
    /// raise it.
    fn minimum(self) -> &'static str {
        match self {
            ApplePlatform::Ios => IOS_MINIMUM,
            ApplePlatform::Macos => MACOS_MINIMUM,
        }
    }
}

impl AppleConfig {
    /// The app's `Info.plist` keys as property list values.
    pub(crate) fn info_plist(&self, platform: ApplePlatform) -> anyhow::Result<plist::Dictionary> {
        info_plist(&self.info_plist, platform)
    }

    /// The app's `Info.plist` strings in other languages.
    pub(crate) fn info_plist_strings(
        &self,
        platform: ApplePlatform,
    ) -> anyhow::Result<InfoPlistStrings> {
        info_plist_strings(&self.info_plist_strings, platform)
    }

    /// The `InfoPlist.strings` files of a package whose `Info.plist` is
    /// `info` ([`localized_info`]).
    pub(crate) fn localized_info(
        &self,
        platform: ApplePlatform,
        info: &plist::Dictionary,
    ) -> anyhow::Result<BTreeMap<String, String>> {
        localized_info(&self.info_plist_strings(platform)?, info, platform)
    }

    /// The app's entitlements as property list values.
    pub(crate) fn entitlements(
        &self,
        platform: ApplePlatform,
    ) -> anyhow::Result<plist::Dictionary> {
        entitlements(&self.entitlements, platform)
    }

    /// Checks both when `nsh.json` is read, so a mistake shows before a
    /// build.
    pub(crate) fn check(&self, platform: ApplePlatform) -> anyhow::Result<()> {
        self.info_plist(platform)?;
        self.info_plist_strings(platform)?;
        self.entitlements(platform)?;
        for extension in &self.extensions {
            extension_entitlements(extension, platform)?;
        }
        if self.launch_screen.is_some() && self.info_plist.contains_key("UILaunchScreen") {
            bail!(
                "nsh.json {0}.infoPlist: `UILaunchScreen` is set by NSH from `{0}.launchScreen`; \
                 set one of them",
                platform.key()
            );
        }
        Ok(())
    }
}

impl MacosConfig {
    /// The app's `Info.plist` keys as property list values.
    pub(crate) fn info_plist(&self) -> anyhow::Result<plist::Dictionary> {
        info_plist(&self.info_plist, ApplePlatform::Macos)
    }

    /// The app's `Info.plist` strings in other languages.
    pub(crate) fn info_plist_strings(&self) -> anyhow::Result<InfoPlistStrings> {
        info_plist_strings(&self.info_plist_strings, ApplePlatform::Macos)
    }

    /// The `InfoPlist.strings` files of a package whose `Info.plist` is
    /// `info` ([`localized_info`]).
    pub(crate) fn localized_info(
        &self,
        info: &plist::Dictionary,
    ) -> anyhow::Result<BTreeMap<String, String>> {
        localized_info(&self.info_plist_strings()?, info, ApplePlatform::Macos)
    }

    /// The app's entitlements as property list values.
    pub(crate) fn entitlements(&self) -> anyhow::Result<plist::Dictionary> {
        entitlements(&self.entitlements, ApplePlatform::Macos)
    }

    /// Checks both when `nsh.json` is read, so a mistake shows before a
    /// build.
    pub(crate) fn check(&self) -> anyhow::Result<()> {
        self.info_plist()?;
        self.info_plist_strings()?;
        self.entitlements()?;
        for extension in &self.extensions {
            extension_entitlements(extension, ApplePlatform::Macos)?;
        }
        Ok(())
    }

    /// The development host's arguments that start the app as `nsh.json`
    /// says (`nsh run`, `nsh test`): none for the default.
    pub(crate) fn host_args(&self) -> Vec<&'static str> {
        match self.activation_policy {
            ActivationPolicy::Regular => Vec::new(),
            policy => vec!["--activation-policy", policy.as_str()],
        }
    }

    /// Adds to `info`, the app's `app.json`, what the release runner reads
    /// before the app's code runs: nothing for the default.
    pub(crate) fn add_to_app_json(&self, info: &mut serde_json::Value) {
        if self.activation_policy != ActivationPolicy::Regular {
            info["activationPolicy"] = self.activation_policy.as_str().into();
        }
    }
}

/// The keys of a privacy manifest (`PrivacyInfo.xcprivacy`) and, for the
/// arrays of dictionaries, the keys their entries have, each of which an
/// entry must have: Apple's "Privacy manifest files", which App Store
/// Connect checks for exactly these.
const PRIVACY_KEYS: [(&str, PrivacyForm); 4] = [
    ("NSPrivacyTracking", PrivacyForm::Boolean),
    ("NSPrivacyTrackingDomains", PrivacyForm::Strings),
    (
        "NSPrivacyCollectedDataTypes",
        PrivacyForm::Entries(&[
            ("NSPrivacyCollectedDataType", PrivacyForm::String),
            ("NSPrivacyCollectedDataTypeLinked", PrivacyForm::Boolean),
            ("NSPrivacyCollectedDataTypeTracking", PrivacyForm::Boolean),
            ("NSPrivacyCollectedDataTypePurposes", PrivacyForm::Strings),
        ]),
    ),
    (
        "NSPrivacyAccessedAPITypes",
        PrivacyForm::Entries(&[
            ("NSPrivacyAccessedAPIType", PrivacyForm::String),
            ("NSPrivacyAccessedAPITypeReasons", PrivacyForm::Strings),
        ]),
    ),
];

/// The form of a privacy manifest value.
#[derive(Clone, Copy)]
enum PrivacyForm {
    Boolean,
    String,
    Strings,
    /// An array of dictionaries with exactly these keys, each its form.
    Entries(&'static [(&'static str, PrivacyForm)]),
}

impl PrivacyForm {
    /// Whether `value` has this form (an array of entries: is an array).
    fn fits(self, value: &serde_json::Value) -> bool {
        use serde_json::Value as Json;
        match (self, value) {
            (PrivacyForm::Boolean, Json::Bool(_)) | (PrivacyForm::String, Json::String(_)) => true,
            (PrivacyForm::Strings, Json::Array(items)) => items.iter().all(Json::is_string),
            (PrivacyForm::Entries(_), Json::Array(_)) => true,
            _ => false,
        }
    }

    /// What is wrong with `entry` of an array of entries with `keys`, if
    /// anything: a key of another kind or form, or one it lacks.
    fn entry_problem(
        keys: &[(&'static str, PrivacyForm)],
        entry: &serde_json::Value,
    ) -> Option<String> {
        let Some(entry) = entry.as_object() else {
            return Some("is no dictionary".into());
        };
        for (key, value) in entry {
            match keys.iter().find(|(name, _)| name == key) {
                None => return Some(format!("has `{key}`, which is no key of it")),
                Some((_, form)) if !form.fits(value) => {
                    return Some(format!("`{key}` is not in its form"));
                }
                Some(_) => {}
            }
        }
        keys.iter()
            .find(|(name, _)| !entry.contains_key(*name))
            .map(|(name, _)| format!("lacks `{name}`"))
    }
}

/// Keeps the keys of `nsh.json` `ios.privacyManifest` (`manifest`) that are
/// a privacy manifest's, in their form, and of its arrays of entries the
/// entries that are whole: each other key or entry is reported in
/// `ignored` and left out, as any `nsh.json` value an `nsh` cannot read
/// is. What App Store Connect would refuse in the values it keeps (an
/// entry without reasons, tracking without its domains) is reported too.
pub(crate) fn keep_privacy_manifest(
    manifest: &mut serde_json::Map<String, serde_json::Value>,
    ignored: &mut Vec<String>,
) {
    let at = "ios.privacyManifest";
    manifest.retain(|key, value| {
        let Some((_, form)) = PRIVACY_KEYS.iter().find(|(name, _)| name == key) else {
            ignored.push(format!(
                "unknown key `{at}.{key}`: a privacy manifest has {}",
                PRIVACY_KEYS
                    .iter()
                    .map(|(name, _)| format!("`{name}`"))
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
            return false;
        };
        if !form.fits(value) {
            ignored.push(format!(
                "{at}.{key}: not in the form a privacy manifest has"
            ));
            return false;
        }
        if let (PrivacyForm::Entries(keys), serde_json::Value::Array(entries)) = (form, value) {
            let mut i = 0;
            entries.retain(|entry| {
                let problem = PrivacyForm::entry_problem(keys, entry);
                if let Some(problem) = &problem {
                    ignored.push(format!("{at}.{key}[{i}] {problem}: left out"));
                }
                i += 1;
                problem.is_none()
            });
        }
        true
    });
    // Kept, but what App Store Connect refuses as it is.
    let mut refused = |problem: String| {
        ignored.push(format!("{at}: {problem}, which App Store Connect refuses"));
    };
    // An empty `NSPrivacyAccessedAPITypes` is not one of them: packages
    // add NSH's own entries to it ([`NSH_ACCESSED_APIS`]).
    let accessed = manifest
        .get("NSPrivacyAccessedAPITypes")
        .and_then(serde_json::Value::as_array);
    for (i, entry) in accessed.into_iter().flatten().enumerate() {
        if entry["NSPrivacyAccessedAPITypeReasons"]
            .as_array()
            .is_some_and(Vec::is_empty)
        {
            refused(format!("`NSPrivacyAccessedAPITypes[{i}]` gives no reason"));
        }
    }
    let tracking = manifest
        .get("NSPrivacyTracking")
        .and_then(serde_json::Value::as_bool);
    let domains = manifest
        .get("NSPrivacyTrackingDomains")
        .and_then(serde_json::Value::as_array)
        .is_some_and(|d| !d.is_empty());
    match (tracking, domains) {
        (Some(true), false) => refused("`NSPrivacyTracking` is on with no tracking domains".into()),
        (Some(false) | None, true) => {
            refused("tracking domains are listed with `NSPrivacyTracking` off".into());
        }
        _ => {}
    }
}

/// The required-reason APIs NSH's runtime calls, which is linked into
/// every iOS app's executable, so App Store Connect counts them as the
/// app's own (ITMS-91053 without them): by category, the reasons that
/// cover NSH's use.
///
/// - File timestamp: Rust's standard library reading file metadata
///   (`stat`, `fstat`), and the image cache, which ages its files in the
///   app's container (`C617.1`).
/// - System boot time: the renderer's timer, `mach_absolute_time`
///   (`35F9.1`).
const NSH_ACCESSED_APIS: [(&str, &[&str]); 2] = [
    ("NSPrivacyAccessedAPICategoryFileTimestamp", &["C617.1"]),
    ("NSPrivacyAccessedAPICategorySystemBootTime", &["35F9.1"]),
];

/// The app's privacy manifest (`nsh.json` `ios.privacyManifest`, as
/// [`keep_privacy_manifest`] kept it) with NSH's own required-reason APIs
/// ([`NSH_ACCESSED_APIS`]) added: `NSPrivacyAccessedAPITypes` has one entry
/// per category, in the order the app first declares it and NSH's after,
/// each with the reasons of every entry of its category once, the app's
/// first.
fn with_nsh_privacy(
    manifest: &serde_json::Map<String, serde_json::Value>,
) -> serde_json::Map<String, serde_json::Value> {
    use serde_json::Value as Json;
    let declared = manifest
        .get("NSPrivacyAccessedAPITypes")
        .and_then(Json::as_array)
        .into_iter()
        .flatten()
        .filter_map(|entry| {
            let category = entry["NSPrivacyAccessedAPIType"].as_str()?;
            let reasons = entry["NSPrivacyAccessedAPITypeReasons"].as_array()?;
            Some((category, reasons.iter().filter_map(Json::as_str).collect()))
        });
    let nsh = NSH_ACCESSED_APIS
        .iter()
        .map(|(category, reasons)| (*category, reasons.to_vec()));
    let mut categories: Vec<(&str, Vec<&str>)> = Vec::new();
    for (category, reasons) in declared.chain(nsh) {
        let at = match categories.iter().position(|(c, _)| *c == category) {
            Some(at) => at,
            None => {
                categories.push((category, Vec::new()));
                categories.len() - 1
            }
        };
        let joined = &mut categories[at].1;
        for reason in reasons {
            if !joined.contains(&reason) {
                joined.push(reason);
            }
        }
    }
    let accessed = categories
        .into_iter()
        .map(|(category, reasons)| {
            serde_json::json!({
                "NSPrivacyAccessedAPIType": category,
                "NSPrivacyAccessedAPITypeReasons": reasons,
            })
        })
        .collect();
    let mut merged = manifest.clone();
    merged.insert("NSPrivacyAccessedAPITypes".into(), Json::Array(accessed));
    merged
}

/// Writes the iOS app bundle `app`'s privacy manifest,
/// `PrivacyInfo.xcprivacy`, where the system and App Store Connect read
/// it, before the bundle is signed: the app's (`nsh.json`
/// `ios.privacyManifest`) with NSH's own required-reason APIs added, so
/// every app has one, and apps declare only what they call themselves.
pub(crate) fn write_privacy_manifest(
    manifest: &serde_json::Map<String, serde_json::Value>,
    app: &std::path::Path,
) -> anyhow::Result<()> {
    let manifest = with_nsh_privacy(manifest);
    let dictionary = convert(&manifest, "ios.privacyManifest", std::iter::empty())?;
    plist::Value::Dictionary(dictionary)
        .to_file_xml(app.join("PrivacyInfo.xcprivacy"))
        .map_err(|e| anyhow::anyhow!("write PrivacyInfo.xcprivacy: {e}"))
}

/// `nsh.json` `<platform>.infoPlist` as property list values.
fn info_plist(
    entries: &serde_json::Map<String, serde_json::Value>,
    platform: ApplePlatform,
) -> anyhow::Result<plist::Dictionary> {
    let section = format!("{}.infoPlist", platform.key());
    if let Some(value) = entries.get(platform.minimum_key()) {
        check_minimum(value, platform, &section)?;
    }
    convert(entries, &section, platform.reserved())
}

/// `Info.plist` strings in other languages: by language (`"zh-Hans"`),
/// the text of each key.
pub(crate) type InfoPlistStrings = BTreeMap<String, BTreeMap<String, String>>;

/// The keys of NSH's that apps translate: the app's name, which NSH sets
/// from `name` in the language of `Info.plist`.
const TRANSLATED_RESERVED_INFO: [&str; 2] = ["CFBundleDisplayName", "CFBundleName"];

/// Whether an app or a crate may translate `key` of `platform`'s
/// `Info.plist` ([`ApplePlatform::translatable`]).
pub(crate) fn is_translatable_info(platform: ApplePlatform, key: &str) -> bool {
    platform.translatable(key)
}

/// `nsh.json` `<platform>.infoPlistStrings`, checked: each language's code
/// ([`check_language`]), an object of strings, of keys apps translate.
fn info_plist_strings(
    entries: &serde_json::Map<String, serde_json::Value>,
    platform: ApplePlatform,
) -> anyhow::Result<InfoPlistStrings> {
    let section = format!("{}.infoPlistStrings", platform.key());
    let mut languages = InfoPlistStrings::new();
    for (language, texts) in entries {
        if let Err(why) = check_language(language) {
            bail!("nsh.json {section}: {why}");
        }
        let Some(texts) = texts.as_object() else {
            bail!(
                "nsh.json {section}.{language}: {texts} is no object; write the language's text of \
                 each `Info.plist` key, as `{{ \"NSCameraUsageDescription\": \"..\" }}`"
            );
        };
        let mut strings = BTreeMap::new();
        for (key, text) in texts {
            if !platform.translatable(key) {
                bail!(
                    "nsh.json {section}.{language}: `{key}` is set by NSH and not translated; of \
                     NSH's keys, apps translate the app's name (`CFBundleDisplayName`, `CFBundleName`)"
                );
            }
            let Some(text) = text.as_str() else {
                bail!(
                    "nsh.json {section}.{language}.{key}: {text} is no string; `InfoPlist.strings` \
                     translates strings only"
                );
            };
            strings.insert(key.clone(), text.to_owned());
        }
        languages.insert(language.clone(), strings);
    }
    Ok(languages)
}

/// Checks `code` names a language as `.lproj` directories do: a language
/// (`en`, `fil`), then a script (`Hans`) or a region (`BR`, `419`) or
/// both, joined with `-` as Xcode names them (`zh-Hans`, `pt-BR`,
/// `zh-Hant-HK`), cased as the system names them: the system finds the
/// directory by this name. The older `_` (`zh_TW`) is refused, so one
/// language never has two directories.
pub(crate) fn check_language(code: &str) -> Result<(), String> {
    let mut parts = code.split('-');
    let language = parts.next().unwrap_or_default();
    let mut ok =
        (2..=3).contains(&language.len()) && language.bytes().all(|b| b.is_ascii_lowercase());
    let mut script = false;
    let mut region = false;
    for part in parts {
        let bytes = part.as_bytes();
        if !script
            && !region
            && bytes.len() == 4
            && bytes[0].is_ascii_uppercase()
            && bytes[1..].iter().all(u8::is_ascii_lowercase)
        {
            script = true;
        } else if !region
            && ((bytes.len() == 2 && bytes.iter().all(u8::is_ascii_uppercase))
                || (bytes.len() == 3 && bytes.iter().all(u8::is_ascii_digit)))
        {
            region = true;
        } else {
            ok = false;
        }
    }
    if ok {
        Ok(())
    } else {
        Err(format!(
            "{code:?} is no language code; write a language, then a script or a region if needed, \
             joined with `-` and cased as the system names them: \"en\", \"ja\", \"zh-Hans\", \
             \"zh-Hant\", \"pt-BR\""
        ))
    }
}

/// The `<lang>.lproj/InfoPlist.strings` files of a package whose
/// `Info.plist` is `info`, by language: `strings`, each key checked against
/// `info`. A key without a string there is an error: the system shows the
/// translated text only in place of the `Info.plist` value, and the users
/// of every language without one see that value, so a translation of a
/// missing key never shows (a misspelled key, a usage description left out).
pub(crate) fn localized_info(
    strings: &InfoPlistStrings,
    info: &plist::Dictionary,
    platform: ApplePlatform,
) -> anyhow::Result<BTreeMap<String, String>> {
    let section = format!("{}.infoPlistStrings", platform.key());
    let mut files = BTreeMap::new();
    for (language, texts) in strings {
        for key in texts.keys() {
            match info.get(key) {
                Some(plist::Value::String(_)) => {}
                Some(_) => bail!(
                    "nsh.json {section}.{language}.{key}: the app's Info.plist value of `{key}` is \
                     no string; `InfoPlist.strings` translates strings only"
                ),
                None => bail!(
                    "nsh.json {section}.{language}.{key}: the app's Info.plist has no `{key}`; set \
                     its text in `{}.infoPlist`, which users of the other languages see",
                    platform.key()
                ),
            }
        }
        if !texts.is_empty() {
            files.insert(language.clone(), strings_file(texts));
        }
    }
    Ok(files)
}

/// `texts` as a strings file: `"key" = "text";` lines, quoted and escaped
/// as the system's old-style property lists read them, in UTF-8.
pub(crate) fn strings_file(texts: &BTreeMap<String, String>) -> String {
    let mut out = String::new();
    for (key, text) in texts {
        quote(&mut out, key);
        out.push_str(" = ");
        quote(&mut out, text);
        out.push_str(";\n");
    }
    out
}

/// Appends `text` to `out` in quotes, escaping the quote, the backslash and
/// control characters.
fn quote(out: &mut String, text: &str) {
    use std::fmt::Write as _;
    out.push('"');
    for c in text.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if c.is_control() => {
                let _ = write!(out, "\\U{:04x}", u32::from(c));
            }
            c => out.push(c),
        }
    }
    out.push('"');
}

/// Writes `files` (by language, [`localized_info`]) to `dir` as
/// `<lang>.lproj/InfoPlist.strings`, and removes the `InfoPlist.strings`
/// of the other languages a previous write left there (a refreshed
/// development host), with their directory when nothing else is in it.
/// Whether anything changed.
pub(crate) fn write_localized_info(
    dir: &Path,
    files: &BTreeMap<String, String>,
) -> anyhow::Result<bool> {
    let mut changed = false;
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries {
            let path = entry?.path();
            let stale = path
                .file_name()
                .and_then(|name| name.to_str())
                .and_then(|name| name.strip_suffix(".lproj"))
                .is_some_and(|language| !files.contains_key(language));
            let file = path.join("InfoPlist.strings");
            if stale && file.exists() {
                std::fs::remove_file(&file)?;
                // Only when empty: another file of the language stays.
                let _ = std::fs::remove_dir(&path);
                changed = true;
            }
        }
    }
    for (language, text) in files {
        let file = dir
            .join(format!("{language}.lproj"))
            .join("InfoPlist.strings");
        if std::fs::read_to_string(&file).ok().as_deref() == Some(text.as_str()) {
            continue;
        }
        std::fs::create_dir_all(file.parent().expect("a file in a directory"))?;
        std::fs::write(&file, text)?;
        changed = true;
    }
    Ok(changed)
}

/// Checks the app's own minimum system version (`value`, its
/// [`ApplePlatform::minimum_key`] in `section`): a version string that
/// raises the runtime's minimum or names it, never one below it, which
/// would let the system install the app where the runtime cannot run.
fn check_minimum(
    value: &serde_json::Value,
    platform: ApplePlatform,
    section: &str,
) -> anyhow::Result<()> {
    let minimum = platform.minimum();
    let problem = match value {
        serde_json::Value::String(text) => match version(text) {
            Some(raised) if version(minimum).is_some_and(|floor| raised >= floor) => {
                return Ok(());
            }
            Some(_) => format!("{text:?} is below NSH's minimum"),
            None => format!("{text:?} is no version"),
        },
        other => format!("{other} is no string"),
    };
    bail!(
        "nsh.json {section}.{}: {problem}; write the {} version the app needs as a string of \
         numbers and dots, from NSH's minimum, \"{minimum}\", up",
        platform.minimum_key(),
        platform.os()
    )
}

/// The numbers of a version string of one to three (`"15"`, `"15.2"`,
/// `"15.2.1"`), those left out 0, so `"14"` is `"14.0"`; `None` for
/// anything else.
fn version(text: &str) -> Option<[u32; 3]> {
    let mut numbers = [0; 3];
    for (i, part) in text.split('.').enumerate() {
        // Digits only: `parse` also takes a sign.
        if i == numbers.len() || part.is_empty() || !part.bytes().all(|b| b.is_ascii_digit()) {
            return None;
        }
        numbers[i] = part.parse().ok()?;
    }
    Some(numbers)
}

/// `nsh.json` `<platform>.entitlements` as property list values.
fn entitlements(
    entries: &serde_json::Map<String, serde_json::Value>,
    platform: ApplePlatform,
) -> anyhow::Result<plist::Dictionary> {
    let section = format!("{}.entitlements", platform.key());
    convert(entries, &section, RESERVED_ENTITLEMENTS.iter())
}

/// The entitlements of one of `ios.extensions` or `macos.extensions` as
/// property list values, refusing the keys `<platform>.entitlements` refuses,
/// with the `nsh.json` key they come from, for errors.
pub(crate) fn extension_entitlements(
    extension: &crate::app_extensions::AppExtension,
    platform: ApplePlatform,
) -> anyhow::Result<(plist::Dictionary, String)> {
    let section = format!(
        "{}.extensions[{}].entitlements",
        platform.key(),
        extension.index
    );
    let dictionary = convert(
        &extension.entitlements,
        &section,
        RESERVED_ENTITLEMENTS.iter(),
    )?;
    Ok((dictionary, section))
}

/// `entries` as a property list dictionary, refusing the `reserved` keys.
fn convert<'a>(
    entries: &serde_json::Map<String, serde_json::Value>,
    section: &str,
    reserved: impl Iterator<Item = &'a (&'static str, Owner)> + Clone,
) -> anyhow::Result<plist::Dictionary> {
    let mut dictionary = plist::Dictionary::new();
    for (key, value) in entries {
        if let Some((_, owner)) = reserved.clone().find(|(name, _)| name == key) {
            match owner {
                Owner::Config(config) => bail!(
                    "nsh.json {section}: `{key}` is set by NSH from `{config}`; set `{config}` instead"
                ),
                Owner::Package => bail!(
                    "nsh.json {section}: `{key}` is set by NSH, as the packaged app depends on its value"
                ),
            }
        }
        dictionary.insert(key.clone(), to_plist(value, &format!("{section}.{key}"))?);
    }
    Ok(dictionary)
}

/// A JSON value as a property list value: strings, booleans, integers,
/// reals, arrays and dictionaries; `null` has no property list form.
fn to_plist(value: &serde_json::Value, path: &str) -> anyhow::Result<plist::Value> {
    use serde_json::Value as Json;
    Ok(match value {
        Json::Null => bail!("nsh.json {path}: null has no property list form"),
        Json::Bool(b) => plist::Value::Boolean(*b),
        Json::Number(n) => {
            if let Some(i) = n.as_i64() {
                plist::Value::Integer(i.into())
            } else if let Some(u) = n.as_u64() {
                plist::Value::Integer(u.into())
            } else {
                plist::Value::Real(n.as_f64().unwrap_or_default())
            }
        }
        Json::String(s) => plist::Value::String(s.clone()),
        Json::Array(items) => plist::Value::Array(
            items
                .iter()
                .enumerate()
                .map(|(i, item)| to_plist(item, &format!("{path}[{i}]")))
                .collect::<anyhow::Result<_>>()?,
        ),
        Json::Object(entries) => plist::Value::Dictionary(
            entries
                .iter()
                .map(|(key, item)| Ok((key.clone(), to_plist(item, &format!("{path}.{key}"))?)))
                .collect::<anyhow::Result<_>>()?,
        ),
    })
}

/// Adds the app's entitlements (`requested`, from `nsh.json` `section`:
/// `ios.entitlements`, or an app extension's) to those a provisioning
/// profile grants (`granted`), checking that the profile covers each one.
/// The app's value replaces the profile's: a profile grants
/// `com.apple.developer.associated-domains` as `*`, the app names its
/// domains.
pub(crate) fn grant(
    granted: &mut plist::Dictionary,
    requested: plist::Dictionary,
    section: &str,
) -> anyhow::Result<()> {
    for (key, value) in requested {
        let Some(allowed) = granted.get(&key) else {
            bail!(
                "the provisioning profile does not grant the entitlement `{key}` (nsh.json {section}): \
                 enable its capability for the app's identifier and download the profile again"
            );
        };
        if !covers(allowed, &value) {
            bail!(
                "the provisioning profile grants `{key}` as {}, which does not cover {} (nsh.json {section})",
                describe(allowed),
                describe(&value)
            );
        }
        granted.insert(key, value);
    }
    Ok(())
}

/// Whether a profile's entitlement value covers the app's: equal, a string
/// pattern (`*`, `TEAMID.*`) matching it or each of its strings, an array
/// whose items cover each of its items, `true` covering `false`, a
/// dictionary covering each of its keys.
fn covers(allowed: &plist::Value, value: &plist::Value) -> bool {
    use plist::Value;
    if allowed == value {
        return true;
    }
    match (allowed, value) {
        (Value::String(pattern), Value::String(value)) => matches(pattern, value),
        (Value::String(_), Value::Array(items)) => items.iter().all(|item| covers(allowed, item)),
        (Value::Array(allowed), Value::Array(items)) => items
            .iter()
            .all(|item| allowed.iter().any(|allowed| covers(allowed, item))),
        (Value::Array(allowed), item) => allowed.iter().any(|allowed| covers(allowed, item)),
        (Value::Boolean(allowed), Value::Boolean(value)) => *allowed || !*value,
        (Value::Dictionary(allowed), Value::Dictionary(entries)) => {
            entries.iter().all(|(key, item)| {
                allowed
                    .get(key)
                    .is_some_and(|allowed| covers(allowed, item))
            })
        }
        _ => false,
    }
}

/// Whether a profile's string `pattern` (`*`, `TEAMID.*` or a value)
/// matches `value`.
fn matches(pattern: &str, value: &str) -> bool {
    match pattern.strip_suffix('*') {
        Some(prefix) => value.starts_with(prefix),
        None => pattern == value,
    }
}

/// A property list value as JSON-like text, for errors.
fn describe(value: &plist::Value) -> String {
    use plist::Value;
    match value {
        Value::String(s) => format!("{s:?}"),
        Value::Boolean(b) => b.to_string(),
        Value::Integer(i) => i.to_string(),
        Value::Real(r) => r.to_string(),
        Value::Array(items) => {
            let items: Vec<String> = items.iter().map(describe).collect();
            format!("[{}]", items.join(", "))
        }
        Value::Dictionary(entries) => {
            let entries: Vec<String> = entries
                .iter()
                .map(|(key, item)| format!("{key:?}: {}", describe(item)))
                .collect();
            format!("{{{}}}", entries.join(", "))
        }
        _ => "a value".to_owned(),
    }
}

/// Whether macOS launches an app signed with `entitlement` only under a
/// provisioning profile that grants it: the `com.apple.developer.*` ones
/// (associated domains, iCloud, push) and keychain access groups.
pub(crate) fn needs_profile(entitlement: &str) -> bool {
    entitlement.starts_with("com.apple.developer.") || entitlement == "keychain-access-groups"
}

#[cfg(test)]
mod tests {
    use super::*;

    fn config(json: serde_json::Value) -> AppleConfig {
        serde_json::from_value(json).unwrap()
    }

    /// A privacy manifest as Apple's documentation writes one.
    fn privacy() -> serde_json::Map<String, serde_json::Value> {
        serde_json::json!({
            "NSPrivacyTracking": false,
            "NSPrivacyTrackingDomains": [],
            "NSPrivacyCollectedDataTypes": [{
                "NSPrivacyCollectedDataType": "NSPrivacyCollectedDataTypeCrashData",
                "NSPrivacyCollectedDataTypeLinked": false,
                "NSPrivacyCollectedDataTypeTracking": false,
                "NSPrivacyCollectedDataTypePurposes": ["NSPrivacyCollectedDataTypePurposeAppFunctionality"],
            }],
            "NSPrivacyAccessedAPITypes": [{
                "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategoryUserDefaults",
                "NSPrivacyAccessedAPITypeReasons": ["CA92.1"],
            }],
        })
        .as_object()
        .unwrap()
        .clone()
    }

    #[test]
    fn a_privacy_manifest_keeps_its_own_keys_in_their_form() {
        let mut manifest = privacy();
        let mut ignored = Vec::new();
        keep_privacy_manifest(&mut manifest, &mut ignored);
        assert!(ignored.is_empty(), "{ignored:?}");
        assert_eq!(manifest, privacy());
        // An unknown key and a value of another form are left out; of an
        // array of entries, only the entries that are not whole.
        let mut manifest = privacy();
        manifest.insert("NSPrivacyAccessedAPI".into(), serde_json::json!([]));
        manifest.insert("NSPrivacyTrackingDomains".into(), serde_json::json!(true));
        manifest.insert(
            "NSPrivacyAccessedAPITypes".into(),
            serde_json::json!([
                { "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategoryUserDefaults",
                  "NSPrivacyAccessedAPITypeReasons": ["CA92.1"] },
                { "NSPrivacyAccessedAPIType": "x", "Reasons": ["CA92.1"] },
                { "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategorySystemBootTime" },
            ]),
        );
        let mut ignored = Vec::new();
        keep_privacy_manifest(&mut manifest, &mut ignored);
        let mut kept: Vec<&String> = manifest.keys().collect();
        kept.sort();
        assert_eq!(
            kept,
            [
                "NSPrivacyAccessedAPITypes",
                "NSPrivacyCollectedDataTypes",
                "NSPrivacyTracking"
            ]
        );
        assert_eq!(
            manifest["NSPrivacyAccessedAPITypes"]
                .as_array()
                .unwrap()
                .len(),
            1
        );
        for says in [
            "unknown key `ios.privacyManifest.NSPrivacyAccessedAPI`",
            "ios.privacyManifest.NSPrivacyTrackingDomains: not in the form",
            "ios.privacyManifest.NSPrivacyAccessedAPITypes[1] has `Reasons`",
            "ios.privacyManifest.NSPrivacyAccessedAPITypes[2] lacks `NSPrivacyAccessedAPITypeReasons`",
        ] {
            assert!(
                ignored.iter().any(|i| i.starts_with(says)),
                "{says}: {ignored:?}"
            );
        }
        assert_eq!(ignored.len(), 4, "{ignored:?}");
    }

    #[test]
    fn what_app_store_connect_refuses_is_reported() {
        let refused = |json: serde_json::Value| {
            let mut manifest = json.as_object().unwrap().clone();
            let mut ignored = Vec::new();
            keep_privacy_manifest(&mut manifest, &mut ignored);
            ignored
        };
        for (json, says) in [
            (
                serde_json::json!({ "NSPrivacyTracking": true }),
                "no tracking domains",
            ),
            (
                serde_json::json!({ "NSPrivacyTracking": false, "NSPrivacyTrackingDomains": ["ads.example"] }),
                "with `NSPrivacyTracking` off",
            ),
            (
                serde_json::json!({ "NSPrivacyAccessedAPITypes": [{
                    "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategoryUserDefaults",
                    "NSPrivacyAccessedAPITypeReasons": [],
                }] }),
                "gives no reason",
            ),
        ] {
            let ignored = refused(json);
            assert!(
                ignored.len() == 1 && ignored[0].contains(says) && ignored[0].contains("refuses"),
                "{says}: {ignored:?}"
            );
        }
        assert!(refused(serde_json::Value::Object(privacy())).is_empty());
        // Packages add NSH's entries to an empty list.
        assert!(refused(serde_json::json!({ "NSPrivacyAccessedAPITypes": [] })).is_empty());
    }

    /// `NSPrivacyAccessedAPITypes` of a manifest: each category's reasons.
    fn accessed(manifest: &serde_json::Map<String, serde_json::Value>) -> Vec<(&str, Vec<&str>)> {
        manifest["NSPrivacyAccessedAPITypes"]
            .as_array()
            .unwrap()
            .iter()
            .map(|entry| {
                (
                    entry["NSPrivacyAccessedAPIType"].as_str().unwrap(),
                    entry["NSPrivacyAccessedAPITypeReasons"]
                        .as_array()
                        .unwrap()
                        .iter()
                        .map(|r| r.as_str().unwrap())
                        .collect(),
                )
            })
            .collect()
    }

    #[test]
    fn an_app_without_a_privacy_manifest_gets_nsh_s() {
        let merged = with_nsh_privacy(&serde_json::Map::new());
        assert_eq!(merged.len(), 1, "{merged:?}");
        assert_eq!(
            accessed(&merged),
            [
                ("NSPrivacyAccessedAPICategoryFileTimestamp", vec!["C617.1"]),
                ("NSPrivacyAccessedAPICategorySystemBootTime", vec!["35F9.1"]),
            ]
        );
    }

    #[test]
    fn nsh_s_entries_join_the_app_s_by_category() {
        let mut manifest = privacy();
        manifest.insert(
            "NSPrivacyAccessedAPITypes".into(),
            serde_json::json!([
                { "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategoryFileTimestamp",
                  "NSPrivacyAccessedAPITypeReasons": ["3B52.1"] },
                { "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategoryUserDefaults",
                  "NSPrivacyAccessedAPITypeReasons": ["CA92.1"] },
                { "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategorySystemBootTime",
                  "NSPrivacyAccessedAPITypeReasons": ["35F9.1"] },
                { "NSPrivacyAccessedAPIType": "NSPrivacyAccessedAPICategoryFileTimestamp",
                  "NSPrivacyAccessedAPITypeReasons": ["C617.1", "DDA9.1"] },
            ]),
        );
        let merged = with_nsh_privacy(&manifest);
        // One entry per category, in the app's order, each reason once.
        assert_eq!(
            accessed(&merged),
            [
                (
                    "NSPrivacyAccessedAPICategoryFileTimestamp",
                    vec!["3B52.1", "C617.1", "DDA9.1"]
                ),
                ("NSPrivacyAccessedAPICategoryUserDefaults", vec!["CA92.1"]),
                ("NSPrivacyAccessedAPICategorySystemBootTime", vec!["35F9.1"]),
            ]
        );
        // The app's other keys are its own.
        for key in [
            "NSPrivacyTracking",
            "NSPrivacyTrackingDomains",
            "NSPrivacyCollectedDataTypes",
        ] {
            assert_eq!(merged[key], manifest[key], "{key}");
        }
        // The app's other categories come first, NSH's after.
        assert_eq!(
            accessed(&with_nsh_privacy(&privacy())),
            [
                ("NSPrivacyAccessedAPICategoryUserDefaults", vec!["CA92.1"]),
                ("NSPrivacyAccessedAPICategoryFileTimestamp", vec!["C617.1"]),
                ("NSPrivacyAccessedAPICategorySystemBootTime", vec!["35F9.1"]),
            ]
        );
    }

    #[test]
    fn the_privacy_manifest_is_written_into_the_app() {
        let app = std::env::temp_dir().join(format!("nsh-cli-privacy-{}.app", std::process::id()));
        let _ = std::fs::remove_dir_all(&app);
        std::fs::create_dir_all(&app).unwrap();
        // An app without one still carries NSH's.
        write_privacy_manifest(&serde_json::Map::new(), &app).unwrap();
        let written = plist::Value::from_file(app.join("PrivacyInfo.xcprivacy")).unwrap();
        let accessed = written.as_dictionary().unwrap()["NSPrivacyAccessedAPITypes"]
            .as_array()
            .unwrap()
            .len();
        assert_eq!(accessed, NSH_ACCESSED_APIS.len());
        write_privacy_manifest(&privacy(), &app).unwrap();
        let written = plist::Value::from_file(app.join("PrivacyInfo.xcprivacy")).unwrap();
        let written = written.as_dictionary().unwrap();
        assert_eq!(written["NSPrivacyTracking"], plist::Value::Boolean(false));
        let accessed = written["NSPrivacyAccessedAPITypes"].as_array().unwrap();
        assert_eq!(
            accessed[0].as_dictionary().unwrap()["NSPrivacyAccessedAPITypeReasons"],
            plist::Value::Array(vec![plist::Value::String("CA92.1".into())])
        );
        std::fs::remove_dir_all(&app).unwrap();
    }

    /// `ios.launchScreen` takes a color and an image for both appearances
    /// or one for each appearance; colors are `#rrggbb` or `#rrggbbaa`.
    #[test]
    fn launch_screens_read_one_value_or_one_per_appearance() {
        let both = config(serde_json::json!({
            "launchScreen": { "background": "#131312", "image": "launch.png" }
        }));
        assert_eq!(
            both.launch_screen,
            Some(LaunchScreen {
                background: Some(Themed::Both(Color([0x13, 0x13, 0x12, 255]))),
                image: Some(Themed::Both("launch.png".into())),
            })
        );
        let each = config(serde_json::json!({
            "launchScreen": {
                "background": { "light": "#FFFFFF", "dark": "#00000080" },
                "image": { "light": "light.png", "dark": "dark.png" }
            }
        }))
        .launch_screen
        .unwrap()
        .relative_to(Path::new("/app"));
        assert_eq!(
            each.background.unwrap().variants(),
            (&Color([255, 255, 255, 255]), Some(&Color([0, 0, 0, 0x80])))
        );
        assert_eq!(
            each.image.unwrap().variants(),
            (
                &PathBuf::from("/app/light.png"),
                Some(&PathBuf::from("/app/dark.png"))
            )
        );
        for text in [
            "131312",
            "#13131",
            "#1313121",
            "#gg1312",
            "#+f+f+f",
            "#1313",
        ] {
            assert!(
                Color::try_from(text.to_owned()).is_err(),
                "{text} is no color"
            );
        }
    }

    /// The launch screen is either NSH's, from `ios.launchScreen`, or the
    /// app's own `UILaunchScreen`, never both.
    #[test]
    fn a_launch_screen_and_its_info_plist_key_are_not_both_set() {
        let error = config(serde_json::json!({
            "launchScreen": { "background": "#131312" },
            "infoPlist": { "UILaunchScreen": {} }
        }))
        .check(ApplePlatform::Ios)
        .unwrap_err()
        .to_string();
        assert!(
            error.contains("`UILaunchScreen` is set by NSH from `ios.launchScreen`"),
            "{error}"
        );
        config(serde_json::json!({ "infoPlist": { "UILaunchScreen": {} } }))
            .check(ApplePlatform::Ios)
            .unwrap();
    }

    #[test]
    fn json_values_become_property_list_values() {
        let config = config(serde_json::json!({
            "infoPlist": {
                "ITSAppUsesNonExemptEncryption": false,
                "LSApplicationCategoryType": "public.app-category.finance",
                "Count": 3,
                "Big": u64::MAX,
                "Ratio": 0.5,
                "Schemes": ["https", "mailto"],
                "NSAppTransportSecurity": {
                    "NSExceptionDomains": {
                        "example.com": { "NSIncludesSubdomains": true }
                    }
                }
            }
        }));
        let info = config.info_plist(ApplePlatform::Ios).unwrap();
        use plist::Value;
        assert_eq!(info["ITSAppUsesNonExemptEncryption"], Value::Boolean(false));
        assert_eq!(
            info["LSApplicationCategoryType"],
            Value::String("public.app-category.finance".into())
        );
        assert_eq!(info["Count"], Value::Integer(3.into()));
        assert_eq!(info["Big"], Value::Integer(u64::MAX.into()));
        assert_eq!(info["Ratio"], Value::Real(0.5));
        assert_eq!(
            info["Schemes"],
            Value::Array(vec!["https".into(), "mailto".into()])
        );
        let domains = info["NSAppTransportSecurity"]
            .as_dictionary()
            .and_then(|d| d.get("NSExceptionDomains"))
            .and_then(|d| d.as_dictionary())
            .and_then(|d| d.get("example.com"))
            .and_then(|d| d.as_dictionary())
            .unwrap();
        assert_eq!(domains["NSIncludesSubdomains"], Value::Boolean(true));
    }

    #[test]
    fn null_has_no_property_list_form() {
        let config = config(serde_json::json!({
            "infoPlist": { "Things": [1, { "Gone": null }] }
        }));
        let error = config
            .info_plist(ApplePlatform::Macos)
            .unwrap_err()
            .to_string();
        assert!(
            error.contains("macos.infoPlist.Things[1].Gone: null"),
            "{error}"
        );
    }

    #[test]
    fn nsh_keeps_its_own_keys() {
        let refused = |platform, json| config(json).check(platform).unwrap_err().to_string();
        let error = refused(
            ApplePlatform::Ios,
            serde_json::json!({ "infoPlist": { "CFBundleIdentifier": "com.other" } }),
        );
        assert!(
            error.contains("ios.infoPlist: `CFBundleIdentifier` is set by NSH from `id`"),
            "{error}"
        );
        let error = refused(
            ApplePlatform::Ios,
            serde_json::json!({ "infoPlist": { "LSRequiresIPhoneOS": false } }),
        );
        assert!(
            error.contains("`LSRequiresIPhoneOS` is set by NSH"),
            "{error}"
        );
        let error = refused(
            ApplePlatform::Macos,
            serde_json::json!({ "infoPlist": { "NSPrincipalClass": "App" } }),
        );
        assert!(
            error.contains("macos.infoPlist: `NSPrincipalClass`"),
            "{error}"
        );
        let error = refused(
            ApplePlatform::Macos,
            serde_json::json!({ "entitlements": { "application-identifier": "T.com.other" } }),
        );
        assert!(
            error.contains("macos.entitlements: `application-identifier` is set by NSH from `id`"),
            "{error}"
        );
        let error = refused(
            ApplePlatform::Macos,
            serde_json::json!({ "infoPlist": { "CFBundleURLTypes": [] } }),
        );
        assert!(
            error.contains("`CFBundleURLTypes` is set by NSH from `schemes`"),
            "{error}"
        );
        let macos = |json| serde_json::from_value::<MacosConfig>(json).unwrap();
        let error = macos(serde_json::json!({ "infoPlist": { "LSUIElement": true } }))
            .check()
            .unwrap_err()
            .to_string();
        assert!(
            error.contains(
                "macos.infoPlist: `LSUIElement` is set by NSH from `macos.activationPolicy`; set \
                 `macos.activationPolicy` instead"
            ),
            "{error}"
        );
        // A key that is NSH's on one platform only is the app's on the other.
        assert!(
            config(serde_json::json!({ "infoPlist": { "NSPrincipalClass": "App" } }))
                .check(ApplePlatform::Ios)
                .is_ok()
        );
    }

    /// An app raises the oldest system its package runs on, never below the
    /// runtime's: anything else is an error when `nsh.json` is read.
    #[test]
    fn apps_raise_the_minimum_system_version() {
        let check = |platform: ApplePlatform, value: serde_json::Value| {
            let mut info = serde_json::Map::new();
            info.insert(platform.minimum_key().to_owned(), value);
            config(serde_json::json!({ "infoPlist": info })).check(platform)
        };
        // NSH's own, written either way, and later ones.
        let accepted = [
            (
                ApplePlatform::Ios,
                ["15", "15.0", "15.0.0", "16.4", "17.0.1"],
            ),
            (ApplePlatform::Macos, ["14", "14.0", "15", "15.2", "15.2.1"]),
        ];
        for (platform, values) in accepted {
            for value in values {
                check(platform, value.into()).unwrap();
                // Written as the app wrote it.
                let info = config(serde_json::json!({
                    "infoPlist": { platform.minimum_key(): value }
                }))
                .info_plist(platform)
                .unwrap();
                assert_eq!(
                    info[platform.minimum_key()],
                    plist::Value::String(value.into())
                );
            }
        }
        let below = [
            (ApplePlatform::Ios, ["14.0", "14.9.9", "13", "8"]),
            (ApplePlatform::Macos, ["13.0", "13.9.9", "13", "10.15"]),
        ];
        for (platform, values) in below {
            for value in values {
                let error = check(platform, value.into()).unwrap_err().to_string();
                assert!(
                    error.contains(&format!(
                        "{}.infoPlist.{}: {value:?} is below NSH's minimum",
                        platform.key(),
                        platform.minimum_key()
                    )),
                    "{error}"
                );
                // The error says what to write instead.
                assert!(
                    error.contains(&format!(
                        "write the {} version the app needs as a string of numbers and dots, \
                         from NSH's minimum, \"{}\", up",
                        platform.os(),
                        platform.minimum()
                    )),
                    "{error}"
                );
            }
        }
        for platform in [ApplePlatform::Ios, ApplePlatform::Macos] {
            for malformed in [
                "abc", "15.x", "", "15.", ".15", "15..2", "15.2.1.4", " 15.2", "+15", "v15", "15",
            ] {
                let error = check(platform, malformed.into()).unwrap_err().to_string();
                assert!(
                    error.contains(&format!("{malformed:?} is no version")),
                    "{error}"
                );
            }
            for other in [
                serde_json::json!(15),
                serde_json::json!(15.2),
                serde_json::json!(true),
                serde_json::json!(null),
                serde_json::json!(["15.2"]),
                serde_json::json!({ "version": "15.2" }),
            ] {
                let error = check(platform, other.clone()).unwrap_err().to_string();
                assert!(error.contains(&format!("{other} is no string")), "{error}");
            }
        }
        // `macos`'s own section reads it as `ios`'s does.
        let macos = |json| serde_json::from_value::<MacosConfig>(json).unwrap();
        assert!(
            macos(serde_json::json!({ "infoPlist": { "LSMinimumSystemVersion": "15.2" } }))
                .check()
                .is_ok()
        );
        let error = macos(serde_json::json!({ "infoPlist": { "LSMinimumSystemVersion": "13.0" } }))
            .check()
            .unwrap_err()
            .to_string();
        assert!(
            error.contains("macos.infoPlist.LSMinimumSystemVersion: \"13.0\" is below"),
            "{error}"
        );
        // The other platform's key is the app's own, as it writes it.
        assert!(
            config(serde_json::json!({ "infoPlist": { "LSMinimumSystemVersion": "11.0" } }))
                .check(ApplePlatform::Ios)
                .is_ok()
        );
        assert!(
            macos(serde_json::json!({ "infoPlist": { "MinimumOSVersion": "11.0" } }))
                .check()
                .is_ok()
        );
    }

    /// Builds for an Apple system compile for the oldest one the package
    /// declares: the app's own minimum when it raises the runtime's.
    #[test]
    fn builds_target_the_minimum_their_package_declares() {
        let minimums = AppleMinimums::of(&serde_json::json!({ "name": "App" })).unwrap();
        assert_eq!(minimums, AppleMinimums::default());
        for triple in [
            "aarch64-apple-ios",
            "aarch64-apple-ios-sim",
            "aarch64-apple-ios-macabi",
        ] {
            assert_eq!(
                minimums.deployment_target(Some(triple)),
                Some(("IPHONEOS_DEPLOYMENT_TARGET", IOS_MINIMUM))
            );
        }
        for triple in ["aarch64-apple-darwin", "x86_64-apple-darwin"] {
            assert_eq!(
                minimums.deployment_target(Some(triple)),
                Some(("MACOSX_DEPLOYMENT_TARGET", MACOS_MINIMUM))
            );
        }
        for triple in [
            "wasm32-unknown-unknown",
            "aarch64-linux-android",
            "x86_64-pc-windows-msvc",
        ] {
            assert_eq!(minimums.deployment_target(Some(triple)), None);
        }
        assert_eq!(
            minimums.deployment_target(None).is_some(),
            cfg!(target_os = "macos")
        );

        let raised = AppleMinimums::of(&serde_json::json!({
            "ios": { "infoPlist": { "MinimumOSVersion": "16.4" } },
            "macos": { "infoPlist": { "LSMinimumSystemVersion": "15.2" } }
        }))
        .unwrap();
        assert_eq!(
            raised.deployment_target(Some("aarch64-apple-ios")),
            Some(("IPHONEOS_DEPLOYMENT_TARGET", "16.4"))
        );
        assert_eq!(
            raised.deployment_target(Some("aarch64-apple-darwin")),
            Some(("MACOSX_DEPLOYMENT_TARGET", "15.2"))
        );

        let error = AppleMinimums::of(&serde_json::json!({
            "ios": { "infoPlist": { "MinimumOSVersion": "13.0" } }
        }))
        .unwrap_err()
        .to_string();
        assert!(
            error.contains("ios.infoPlist.MinimumOSVersion: \"13.0\" is below"),
            "{error}"
        );
    }

    /// Versions compare number by number, the numbers left out 0.
    #[test]
    fn versions_compare_by_their_numbers() {
        assert_eq!(version("15.2.1"), Some([15, 2, 1]));
        assert_eq!(version("14"), version("14.0"));
        assert_eq!(version("14.0"), version("14.0.0"));
        assert!(version("15.10") > version("15.9"));
        assert!(version("15") > version("14.9.9"));
        assert!(version(IOS_MINIMUM).is_some());
        assert!(version(MACOS_MINIMUM).is_some());
        assert_eq!(version("4294967296"), None);
    }

    /// Languages are named as `.lproj` directories are: a language, then a
    /// script or a region or both, cased as the system names them.
    #[test]
    fn languages_are_lproj_names() {
        for code in [
            "en",
            "ja",
            "fil",
            "zh-Hans",
            "zh-Hant",
            "pt-BR",
            "zh-TW",
            "es-419",
            "zh-Hant-HK",
            "sr-Latn-RS",
        ] {
            assert_eq!(check_language(code), Ok(()), "{code}");
        }
        for code in [
            "",
            "e",
            "english",
            "English",
            "EN",
            "zh-hans",
            "zh-CN-Hans",
            "pt-br",
            "en-",
            "-en",
            "en US",
            "Base",
            "zh-Hans-Hant",
            "en-US-US",
            "zh.Hans",
            "../en",
            "en-1234",
            "zh_TW",
            "zh_Hant",
            "sr_Latn_RS",
        ] {
            let error = check_language(code).unwrap_err();
            assert!(error.contains("is no language code"), "{code}: {error}");
        }
    }

    /// `ios.infoPlistStrings` takes, by language, the text of keys apps
    /// translate: errors name the `nsh.json` key.
    #[test]
    fn info_plist_strings_are_checked_when_read() {
        let strings = config(serde_json::json!({
            "infoPlistStrings": {
                "zh-Hans": { "NSCameraUsageDescription": "拍照", "CFBundleDisplayName": "应用" },
                "ja": {}
            }
        }))
        .info_plist_strings(ApplePlatform::Ios)
        .unwrap();
        assert_eq!(strings["zh-Hans"]["NSCameraUsageDescription"], "拍照");
        assert!(strings["ja"].is_empty());
        let error = |json: serde_json::Value| {
            config(serde_json::json!({ "infoPlistStrings": json }))
                .check(ApplePlatform::Ios)
                .unwrap_err()
                .to_string()
        };
        let wrong = error(serde_json::json!({ "zh-hans": {} }));
        assert!(
            wrong.contains("nsh.json ios.infoPlistStrings: \"zh-hans\" is no language code"),
            "{wrong}"
        );
        let wrong = error(serde_json::json!({ "ja": "カメラ" }));
        assert!(
            wrong.contains("nsh.json ios.infoPlistStrings.ja: \"カメラ\" is no object"),
            "{wrong}"
        );
        let wrong = error(serde_json::json!({ "ja": { "UIBackgroundModes": ["audio"] } }));
        assert!(
            wrong.contains(
                "nsh.json ios.infoPlistStrings.ja.UIBackgroundModes: [\"audio\"] is no string"
            ),
            "{wrong}"
        );
        let wrong = error(serde_json::json!({ "ja": { "CFBundleIdentifier": "x" } }));
        assert!(
            wrong.contains("nsh.json ios.infoPlistStrings.ja: `CFBundleIdentifier` is set by NSH"),
            "{wrong}"
        );
        let macos: MacosConfig = serde_json::from_value(serde_json::json!({
            "infoPlistStrings": { "de": { "NSPrincipalClass": "x" } }
        }))
        .unwrap();
        let wrong = macos.check().unwrap_err().to_string();
        assert!(
            wrong.contains("nsh.json macos.infoPlistStrings.de: `NSPrincipalClass` is set by NSH"),
            "{wrong}"
        );
    }

    /// A strings file quotes keys and texts, escaping quotes, backslashes
    /// and control characters, and keeps every other character as it is.
    #[test]
    fn strings_files_escape_what_old_style_property_lists_do() {
        let texts = BTreeMap::from([
            (
                "NSCameraUsageDescription".to_owned(),
                "扫描 \"二维码\"\n路径 C:\\photos\tend\r\u{1}".to_owned(),
            ),
            ("CFBundleDisplayName".to_owned(), "アプリ".to_owned()),
        ]);
        assert_eq!(
            strings_file(&texts),
            "\"CFBundleDisplayName\" = \"アプリ\";\n\
             \"NSCameraUsageDescription\" = \"扫描 \\\"二维码\\\"\\n路径 C:\\\\photos\\tend\\r\\U0001\";\n"
        );
        assert_eq!(strings_file(&BTreeMap::new()), "");
    }

    /// A package translates only what its `Info.plist` has as a string:
    /// the users of the other languages see that value.
    #[test]
    fn translated_keys_need_an_info_plist_string() {
        let config = config(serde_json::json!({
            "infoPlistStrings": {
                "zh-Hans": { "NSCameraUsageDescription": "用相机拍照。" },
                "fr": {}
            }
        }));
        let mut info = plist::Dictionary::new();
        info.insert(
            "NSCameraUsageDescription".into(),
            "The app takes photos.".into(),
        );
        let files = config.localized_info(ApplePlatform::Ios, &info).unwrap();
        assert_eq!(
            files,
            BTreeMap::from([(
                "zh-Hans".to_owned(),
                "\"NSCameraUsageDescription\" = \"用相机拍照。\";\n".to_owned()
            )])
        );
        let missing = config
            .localized_info(ApplePlatform::Ios, &plist::Dictionary::new())
            .unwrap_err()
            .to_string();
        assert!(
            missing.contains(
                "nsh.json ios.infoPlistStrings.zh-Hans.NSCameraUsageDescription: the app's \
                 Info.plist has no `NSCameraUsageDescription`; set its text in `ios.infoPlist`"
            ),
            "{missing}"
        );
        info.insert("NSCameraUsageDescription".into(), true.into());
        let other = config
            .localized_info(ApplePlatform::Ios, &info)
            .unwrap_err()
            .to_string();
        assert!(
            other.contains("value of `NSCameraUsageDescription` is no string"),
            "{other}"
        );
    }

    /// Writing the strings again changes nothing; a language no longer
    /// given loses its file, and its directory unless something else is in it.
    #[test]
    fn localized_info_is_written_per_language() {
        let dir = std::env::temp_dir().join(format!("nsh-lproj-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let files = BTreeMap::from([
            ("ja".to_owned(), "\"A\" = \"あ\";\n".to_owned()),
            ("zh-Hans".to_owned(), "\"A\" = \"甲\";\n".to_owned()),
        ]);
        assert!(write_localized_info(&dir, &files).unwrap());
        assert_eq!(
            std::fs::read_to_string(dir.join("zh-Hans.lproj/InfoPlist.strings")).unwrap(),
            "\"A\" = \"甲\";\n"
        );
        assert!(!write_localized_info(&dir, &files).unwrap());
        std::fs::write(dir.join("ja.lproj/Other.strings"), "").unwrap();
        let fewer = BTreeMap::from([("zh-Hans".to_owned(), files["zh-Hans"].clone())]);
        assert!(write_localized_info(&dir, &fewer).unwrap());
        assert!(!dir.join("ja.lproj/InfoPlist.strings").exists());
        assert!(dir.join("ja.lproj/Other.strings").exists());
        assert!(write_localized_info(&dir, &BTreeMap::new()).unwrap());
        assert!(!dir.join("zh-Hans.lproj").exists());
        std::fs::remove_dir_all(&dir).unwrap();
    }

    /// `macos.activationPolicy`: `regular` unless the app says
    /// `accessory`, which the development host and `app.json` are told.
    #[test]
    fn macos_apps_choose_their_activation_policy() {
        let macos = |json| serde_json::from_value::<MacosConfig>(json);
        let regular = macos(serde_json::json!({})).unwrap();
        assert_eq!(regular.activation_policy, ActivationPolicy::Regular);
        assert!(regular.host_args().is_empty());
        let mut info = serde_json::json!({ "id": "com.a" });
        regular.add_to_app_json(&mut info);
        assert_eq!(info, serde_json::json!({ "id": "com.a" }));
        let accessory = macos(serde_json::json!({ "activationPolicy": "accessory" })).unwrap();
        assert_eq!(accessory.activation_policy, ActivationPolicy::Accessory);
        assert_eq!(accessory.host_args(), ["--activation-policy", "accessory"]);
        accessory.add_to_app_json(&mut info);
        assert_eq!(
            info,
            serde_json::json!({ "id": "com.a", "activationPolicy": "accessory" })
        );
        assert_eq!(
            macos(serde_json::json!({ "activationPolicy": "regular" }))
                .unwrap()
                .activation_policy,
            ActivationPolicy::Regular
        );
        // `nsh.json`'s reader leaves another value out with a warning, an
        // object (the form serde's own enums also take) too.
        for value in [
            serde_json::json!("dock"),
            serde_json::json!("Accessory"),
            serde_json::json!(true),
            serde_json::json!({ "accessory": null }),
        ] {
            assert!(macos(serde_json::json!({ "activationPolicy": value })).is_err());
        }
    }

    fn dictionary(json: serde_json::Value) -> plist::Dictionary {
        config(serde_json::json!({ "entitlements": json }))
            .entitlements(ApplePlatform::Ios)
            .unwrap()
    }

    #[test]
    fn a_profile_grants_what_it_covers() {
        let profile = || {
            to_plist(
                &serde_json::json!({
                "application-identifier": "TEAM.com.acme.app",
                "get-task-allow": false,
                "aps-environment": "production",
                "keychain-access-groups": ["TEAM.*"],
                "com.apple.developer.associated-domains": "*",
                "com.apple.security.application-groups": ["group.com.acme.a", "group.com.acme.b"]
            }),
                "profile",
            )
            .unwrap()
            .into_dictionary()
            .unwrap()
        };
        let mut granted = profile();
        grant(
            &mut granted,
            dictionary(serde_json::json!({
                "com.apple.developer.associated-domains": ["applinks:acme.com", "webcredentials:acme.com"],
                "keychain-access-groups": ["TEAM.com.acme.shared"],
                "com.apple.security.application-groups": ["group.com.acme.b"],
                "aps-environment": "production"
            })),
            "ios.entitlements",
        )
        .unwrap();
        // The app's values replace the profile's; the rest stays.
        assert_eq!(
            granted["com.apple.developer.associated-domains"],
            plist::Value::Array(vec![
                "applinks:acme.com".into(),
                "webcredentials:acme.com".into()
            ])
        );
        assert_eq!(
            granted["com.apple.security.application-groups"],
            plist::Value::Array(vec!["group.com.acme.b".into()])
        );
        assert_eq!(
            granted["application-identifier"],
            plist::Value::String("TEAM.com.acme.app".into())
        );

        let refused = |json| {
            grant(&mut profile(), dictionary(json), "ios.entitlements")
                .unwrap_err()
                .to_string()
        };
        let error = refused(serde_json::json!({ "com.apple.developer.healthkit": true }));
        assert!(
            error.contains("does not grant the entitlement `com.apple.developer.healthkit`"),
            "{error}"
        );
        let error = refused(serde_json::json!({ "aps-environment": "development" }));
        assert!(
            error.contains(
                "grants `aps-environment` as \"production\", which does not cover \"development\""
            ),
            "{error}"
        );
        let error = refused(serde_json::json!({ "keychain-access-groups": ["OTHER.shared"] }));
        assert!(error.contains("`keychain-access-groups`"), "{error}");
        let error = refused(serde_json::json!({
            "com.apple.security.application-groups": ["group.com.acme.c"]
        }));
        assert!(error.contains("[\"group.com.acme.c\"]"), "{error}");
        let error = refused(serde_json::json!({ "get-task-allow": true }));
        assert!(error.contains("`get-task-allow` as false"), "{error}");
    }

    #[test]
    fn patterns_and_flags() {
        use plist::Value;
        assert!(covers(&"*".into(), &"anything".into()));
        assert!(covers(&"TEAM.*".into(), &"TEAM.x".into()));
        assert!(!covers(&"TEAM.*".into(), &"OTHER.x".into()));
        assert!(covers(&Value::Boolean(true), &Value::Boolean(false)));
        assert!(!covers(&Value::Boolean(false), &Value::Boolean(true)));
        assert!(!covers(&Value::Integer(1.into()), &"1".into()));
        assert!(needs_profile("com.apple.developer.associated-domains"));
        assert!(!needs_profile("com.apple.security.app-sandbox"));
    }
}