eml-nl 0.5.0

Parse, manipulate and emit EML_NL documents
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
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
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
//! Document variant for the EML_NL Count (`510a`, `510b`, `510c` or `510d`) document.

use std::{collections::BTreeMap, num::NonZeroU64, str::FromStr};

use crate::{
    EML_SCHEMA_VERSION, EMLError, EMLErrorKind, EMLResultExt as _, EMLValueResultExt, NS_EML,
    NS_KR,
    common::{
        CandidateIdentifier, CanonicalizationMethod, ContestIdentifier, CreationDateTime,
        ElectionDomain, ManagingAuthority, MinimalQualifyingAddress, PersonNameStructure,
        ReportingUnitIdentifier, TransactionId,
    },
    documents::{ElectionIdentifierBuilder, accepted_root},
    io::{
        EMLElement, EMLElementReader, EMLElementWriter, EMLReadElement as _, EMLWriteElement,
        QualifiedName, collect_struct,
    },
    utils::{
        AffiliationId, CandidateId, ElectionCategory, ElectionId, ElectionSubcategory, Gender,
        StringValue, XsDate, XsDateTime,
    },
};

/// Representing a `510a`, `510b`, `510c` or `510d` document, containing a count.
#[derive(Debug, Clone)]
pub struct ElectionCount {
    /// Type of count document.
    pub count_type: CountType,

    /// Transaction ID of the document.
    pub transaction_id: TransactionId,

    /// Managing authority of the document.
    pub managing_authority: ManagingAuthority,

    /// Creation date and time of the document.
    pub creation_date_time: CreationDateTime,

    /// Canonicalization method used in this document, if present.
    pub canonicalization_method: Option<CanonicalizationMethod>,

    /// The actual count data.
    pub count: ElectionCountCount,
}

impl ElectionCount {
    /// Create a builder for the [`ElectionCount`] document.
    pub fn builder() -> ElectionCountBuilder {
        ElectionCountBuilder::new()
    }
}

impl FromStr for ElectionCount {
    type Err = EMLError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use crate::io::EMLRead as _;
        Self::parse_eml(s, crate::io::EMLParsingMode::Strict).ok()
    }
}

impl TryFrom<&str> for ElectionCount {
    type Error = EMLError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        use crate::io::EMLRead as _;
        Self::parse_eml(value, crate::io::EMLParsingMode::Strict).ok()
    }
}

impl TryFrom<ElectionCount> for String {
    type Error = EMLError;

    fn try_from(value: ElectionCount) -> Result<Self, Self::Error> {
        use crate::io::EMLWrite as _;
        value.write_eml_root_str(true, true)
    }
}

/// Builder for [`ElectionCount`].
#[derive(Debug, Clone)]
pub struct ElectionCountBuilder {
    count_type: Option<CountType>,
    transaction_id: Option<TransactionId>,
    managing_authority: Option<ManagingAuthority>,
    creation_date_time: Option<CreationDateTime>,
    canonicalization_method: Option<CanonicalizationMethod>,
    count: Option<ElectionCountCount>,
    election_identifier: Option<ElectionCountElectionIdentifier>,
    contests: Vec<ElectionCountContest>,
}

impl ElectionCountBuilder {
    /// Create a new ElectionCountBuilder for building [`ElectionCount`] documents.
    pub fn new() -> Self {
        Self {
            count_type: None,
            transaction_id: None,
            managing_authority: None,
            creation_date_time: None,
            canonicalization_method: None,
            count: None,
            election_identifier: None,
            contests: vec![],
        }
    }

    /// Set the count type for the document.
    pub fn count_type(mut self, count_type: impl Into<CountType>) -> Self {
        self.count_type = Some(count_type.into());
        self
    }

    /// Set the transaction id for the document.
    pub fn transaction_id(mut self, transaction_id: impl Into<TransactionId>) -> Self {
        self.transaction_id = Some(transaction_id.into());
        self
    }

    /// Set the managing authority for the document.
    pub fn managing_authority(mut self, managing_authority: impl Into<ManagingAuthority>) -> Self {
        self.managing_authority = Some(managing_authority.into());
        self
    }

    /// Set the creation date and time for the document.
    pub fn creation_date_time(mut self, creation_date_time: impl Into<XsDateTime>) -> Self {
        self.creation_date_time = Some(CreationDateTime::new(creation_date_time.into()));
        self
    }

    /// Set the canonicalization method for the document.
    pub fn canonicalization_method(
        mut self,
        canonicalization_method: impl Into<CanonicalizationMethod>,
    ) -> Self {
        self.canonicalization_method = Some(canonicalization_method.into());
        self
    }

    /// Set the count for the document.
    ///
    /// You may either set the entire election count at once using this method,
    /// or use any of [`Self::election_identifier`], [`Self::contests`] and/or
    /// [`Self::push_contest`] to construct the individual components of the
    /// count document.
    pub fn count(mut self, count: impl Into<ElectionCountCount>) -> Self {
        self.count = Some(count.into());
        self
    }

    /// Set the election identifier for the document.
    ///
    /// This only has effect if the count was not set using the  [`Self::count`]
    /// method on this builder.
    pub fn election_identifier(
        mut self,
        election_identifier: impl Into<ElectionCountElectionIdentifier>,
    ) -> Self {
        self.election_identifier = Some(election_identifier.into());
        self
    }

    /// Set the contests for the document. This overrides any previously set contests.
    ///
    /// This only has effect if the count was not set using the  [`Self::count`]
    /// method on this builder.
    pub fn contests(mut self, contests: impl Into<Vec<ElectionCountContest>>) -> Self {
        self.contests = contests.into();
        self
    }

    /// Add a contest to the document.
    ///
    /// This only has effect if the count was not set using the  [`Self::count`]
    /// method on this builder.
    pub fn push_contest(mut self, contest: impl Into<ElectionCountContest>) -> Self {
        self.contests.push(contest.into());
        self
    }

    /// Build the [`ElectionCount`] document, returning an error if any of the required fields are missing.
    pub fn build(self) -> Result<ElectionCount, EMLError> {
        Ok(ElectionCount {
            count_type: self
                .count_type
                .ok_or_else(|| EMLErrorKind::MissingBuildProperty("count_type").without_span())?,
            transaction_id: self.transaction_id.ok_or_else(|| {
                EMLErrorKind::MissingBuildProperty("transaction_id").without_span()
            })?,
            managing_authority: self.managing_authority.ok_or_else(|| {
                EMLErrorKind::MissingBuildProperty("managing_authority").without_span()
            })?,
            creation_date_time: self.creation_date_time.ok_or_else(|| {
                EMLErrorKind::MissingBuildProperty("creation_date_time").without_span()
            })?,
            canonicalization_method: self.canonicalization_method,
            count: self.count.map_or_else(
                || {
                    if self.contests.is_empty() {
                        return Err(EMLErrorKind::MissingBuildProperty("contests").without_span());
                    }

                    Ok(ElectionCountCount::new(ElectionCountElection::new(
                        self.election_identifier.ok_or_else(|| {
                            EMLErrorKind::MissingBuildProperty("election_identifier").without_span()
                        })?,
                        self.contests,
                    )))
                },
                Ok,
            )?,
        })
    }
}

impl Default for ElectionCountBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl EMLElement for ElectionCount {
    const EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("EML", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        accepted_root(elem)?;

        let document_id = elem.attribute_value_req(("Id", None))?;
        let count_type = CountType::from_eml_id(document_id.as_ref())
            .map_err(|e| e.into_kind().with_span(elem.span()))?;

        Ok(collect_struct!(elem, ElectionCount {
            count_type: count_type,
            transaction_id: TransactionId::EML_NAME => |elem| TransactionId::read_eml(elem)?,
            managing_authority: ManagingAuthority::EML_NAME => |elem| ManagingAuthority::read_eml(elem)?,
            creation_date_time: CreationDateTime::EML_NAME => |elem| CreationDateTime::read_eml(elem)?,
            canonicalization_method as Option: CanonicalizationMethod::EML_NAME => |elem| CanonicalizationMethod::read_eml(elem)?,
            count: ElectionCountCount::EML_NAME => |elem| ElectionCountCount::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .attr(("Id", None), self.count_type.to_eml_id())?
            .attr(("SchemaVersion", None), EML_SCHEMA_VERSION)?
            .child_elem(TransactionId::EML_NAME, &self.transaction_id)?
            .child_elem(ManagingAuthority::EML_NAME, &self.managing_authority)?
            .child_elem(CreationDateTime::EML_NAME, &self.creation_date_time)?
            // Note: we don't output the CanonicalizationMethod because we aren't canonicalizing our output
            // .child_elem_option(
            //     CanonicalizationMethod::EML_NAME,
            //     self.canonicalization_method.as_ref(),
            // )?
            .child_elem(ElectionCountCount::EML_NAME, &self.count)?
            .finish()
    }
}

/// EML document ID for Count of polling stationdocuments.
pub(crate) const EML_COUNT_POLLING_STATION_ID: &str = "510a";

/// EML document ID for Count of municipality documents.
pub(crate) const EML_COUNT_MUNICIPAL_ID: &str = "510b";

/// EML document ID for Count of district documents.
pub(crate) const EML_COUNT_DISTRICT_ID: &str = "510c";

/// EML document ID for central Count documents.
pub(crate) const EML_COUNT_CENTRAL_ID: &str = "510d";

/// Type of Count document.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CountType {
    /// Representing a `510a` document, containing the count for a polling station.
    PollingStation,
    /// Representing a `510b` document, containing the count for a local region (= municipality).
    Municipal,
    /// Representing a `510c` document, containing the count for a district (= HSB).
    District,
    /// Representing a `510d` document, containing the count for the entire election.
    Central,
}

impl CountType {
    /// Create a CountType from an EML document ID string.
    pub fn from_eml_id(s: impl AsRef<str>) -> Result<Self, EMLError> {
        let data = s.as_ref();
        match data {
            EML_COUNT_POLLING_STATION_ID => Ok(CountType::PollingStation),
            EML_COUNT_MUNICIPAL_ID => Ok(CountType::Municipal),
            EML_COUNT_DISTRICT_ID => Ok(CountType::District),
            EML_COUNT_CENTRAL_ID => Ok(CountType::Central),
            _ => Err(
                EMLErrorKind::InvalidDocumentType("510a/510b/510c/510d", data.to_string())
                    .without_span(),
            ),
        }
    }

    /// Get the EML document ID string for this CountType.
    pub fn to_eml_id(&self) -> &'static str {
        match self {
            CountType::PollingStation => EML_COUNT_POLLING_STATION_ID,
            CountType::Municipal => EML_COUNT_MUNICIPAL_ID,
            CountType::District => EML_COUNT_DISTRICT_ID,
            CountType::Central => EML_COUNT_CENTRAL_ID,
        }
    }

    /// Get a friendly name for this CountType.
    pub fn to_friendly_name(&self) -> &'static str {
        match self {
            CountType::PollingStation => "Polling Station Count",
            CountType::Municipal => "Municipal Count",
            CountType::District => "District Count",
            CountType::Central => "Central Count",
        }
    }

    /// Returns if the given EML document ID string is a valid CountType ID.
    pub fn is_valid_eml_id(s: &str) -> bool {
        matches!(
            s,
            EML_COUNT_POLLING_STATION_ID
                | EML_COUNT_MUNICIPAL_ID
                | EML_COUNT_DISTRICT_ID
                | EML_COUNT_CENTRAL_ID
        )
    }
}

/// The actual count data.
#[derive(Debug, Clone)]
pub struct ElectionCountCount {
    /// The election for this count.
    pub election: ElectionCountElection,
}

impl ElectionCountCount {
    /// Create a new count for the election count document.
    pub fn new(election: impl Into<ElectionCountElection>) -> Self {
        ElectionCountCount {
            election: election.into(),
        }
    }
}

impl From<ElectionCountElection> for ElectionCountCount {
    fn from(value: ElectionCountElection) -> Self {
        ElectionCountCount::new(value)
    }
}

impl EMLElement for ElectionCountCount {
    const EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Count", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, ElectionCountCount {
            id as None: ("EventIdentifier", NS_EML) => |elem| elem.skip().map(|_| ())?,
            election: ElectionCountElection::EML_NAME => |elem| ElectionCountElection::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child(("EventIdentifier", NS_EML), |w| w.empty())?
            .child_elem(ElectionCountElection::EML_NAME, &self.election)?
            .finish()
    }
}

/// The election for this count.
#[derive(Debug, Clone)]
pub struct ElectionCountElection {
    /// Identifier
    pub identifier: ElectionCountElectionIdentifier,

    /// Contests within this election.
    pub contests: Vec<ElectionCountContest>,
}

impl ElectionCountElection {
    /// Create a new election for the election count document.
    pub fn new(
        identifier: impl Into<ElectionCountElectionIdentifier>,
        contests: impl Into<Vec<ElectionCountContest>>,
    ) -> Self {
        ElectionCountElection {
            identifier: identifier.into(),
            contests: contests.into(),
        }
    }
}

impl EMLElement for ElectionCountElection {
    const EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Election", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        let data = collect_struct!(elem, ElectionCountElection {
            identifier: ElectionCountElectionIdentifier::EML_NAME => |elem| ElectionCountElectionIdentifier::read_eml(elem)?,
            contests: ("Contests", NS_EML) => |elem| {
                struct VecCollector {
                    contests: Vec<ElectionCountContest>,
                }

                let data = collect_struct!(elem, VecCollector {
                    contests as Vec: ElectionCountContest::EML_NAME => |elem| ElectionCountContest::read_eml(elem)?,
                });

                data.contests
            },
        });

        if data.contests.is_empty() {
            let err = EMLErrorKind::MissingElement(ElectionCountContest::EML_NAME.as_owned())
                .with_span(elem.full_span());
            if elem.parsing_mode().is_strict() {
                return Err(err);
            } else {
                elem.push_err(err);
            }
        }

        Ok(data)
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elem(ElectionCountElectionIdentifier::EML_NAME, &self.identifier)?
            .child(("Contests", NS_EML), |writer| {
                writer
                    .child_elems(ElectionCountContest::EML_NAME, &self.contests)?
                    .finish()
            })?
            .finish()
    }
}

/// Identifier for the election in this count.
#[derive(Debug, Clone)]
pub struct ElectionCountElectionIdentifier {
    /// Id of the election
    pub id: StringValue<ElectionId>,

    /// Name of the election
    pub name: Option<String>,

    /// Category of the election
    pub category: StringValue<ElectionCategory>,

    /// Subcategory of the election
    pub subcategory: Option<StringValue<ElectionSubcategory>>,

    /// The (top level) region where the election takes place.
    pub domain: Option<ElectionDomain>,

    /// Date of the election
    pub election_date: StringValue<XsDate>,
}

impl ElectionCountElectionIdentifier {
    /// Create a builder for the [`ElectionCountElectionIdentifier`].
    pub fn builder() -> ElectionIdentifierBuilder {
        ElectionIdentifierBuilder::new()
    }
}

impl EMLElement for ElectionCountElectionIdentifier {
    const EML_NAME: QualifiedName<'_, '_> =
        QualifiedName::from_static("ElectionIdentifier", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, ElectionCountElectionIdentifier {
            id: elem.string_value_attr("Id", None)?,
            name as Option: ("ElectionName", NS_EML) => |elem| elem.text_without_children()?,
            category: ("ElectionCategory", NS_EML) => |elem| elem.string_value()?,
            subcategory as Option: ("ElectionSubcategory", NS_KR) => |elem| elem.string_value()?,
            domain as Option: ElectionDomain::EML_NAME => |elem| ElectionDomain::read_eml(elem)?,
            election_date: ("ElectionDate", NS_KR) => |elem| elem.string_value()?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .attr("Id", self.id.raw().as_ref())?
            .child_option(
                ("ElectionName", NS_EML),
                self.name.as_ref(),
                |elem, value| elem.text(value.as_ref())?.finish(),
            )?
            .child(("ElectionCategory", NS_EML), |elem| {
                elem.text(self.category.raw().as_ref())?.finish()
            })?
            .child_option(
                ("ElectionSubcategory", NS_KR),
                self.subcategory.as_ref(),
                |elem, value| elem.text(value.raw().as_ref())?.finish(),
            )?
            .child_elem_option(ElectionDomain::EML_NAME, self.domain.as_ref())?
            .child(("ElectionDate", NS_KR), |elem| {
                elem.text(self.election_date.raw().as_ref())?.finish()
            })?
            .finish()
    }
}

/// A contest within the election count.
#[derive(Debug, Clone)]
pub struct ElectionCountContest {
    /// Identifier for the contest.
    pub identifier: ContestIdentifier,

    /// Total votes in this contest, if present.
    pub total_votes: Option<TotalVotes>,

    /// Votes per reporting unit in this contest.
    pub reporting_unit_votes: Vec<ReportingUnitVotes>,
}

impl ElectionCountContest {
    /// Create a builder for the [`ElectionCountContest`].
    pub fn builder() -> ElectionCountContestBuilder {
        ElectionCountContestBuilder::new()
    }
}

/// A builder for [`ElectionCountContest`].
#[derive(Debug, Clone)]
pub struct ElectionCountContestBuilder {
    identifier: Option<ContestIdentifier>,
    total_votes: Option<TotalVotes>,
    total_votes_selections: Vec<ElectionCountSelection>,
    total_eligible_voter_count: Option<StringValue<u64>>,
    total_candidate_votes_count: Option<StringValue<u64>>,
    total_rejected_votes: BTreeMap<RejectedVotesReason, StringValue<u64>>,
    total_uncounted_votes: BTreeMap<UncountedVotesReason, StringValue<u64>>,
    reporting_unit_votes: Vec<ReportingUnitVotes>,
}

impl ElectionCountContestBuilder {
    /// Create a new ElectionCountContestBuilder for building [`ElectionCountContest`] documents.
    pub fn new() -> Self {
        Self {
            identifier: None,
            total_votes: None,
            total_votes_selections: vec![],
            total_eligible_voter_count: None,
            total_candidate_votes_count: None,
            total_rejected_votes: BTreeMap::new(),
            total_uncounted_votes: BTreeMap::new(),
            reporting_unit_votes: vec![],
        }
    }

    /// Set the identifier for the contest.
    pub fn identifier(mut self, identifier: impl Into<ContestIdentifier>) -> Self {
        self.identifier = Some(identifier.into());
        self
    }

    /// Set the total votes for the contest.
    pub fn total_votes(mut self, total_votes: impl Into<TotalVotes>) -> Self {
        self.total_votes = Some(total_votes.into());
        self
    }

    /// Set the selections within the total votes for the contest. This overrides any previously set selections.
    pub fn total_votes_selections(
        mut self,
        selections: impl Into<Vec<ElectionCountSelection>>,
    ) -> Self {
        self.total_votes_selections = selections.into();
        self
    }

    /// Add a selection to the selections within the total votes for the contest.
    pub fn push_total_votes_selection(
        mut self,
        selection: impl Into<ElectionCountSelection>,
    ) -> Self {
        self.total_votes_selections.push(selection.into());
        self
    }

    /// Set the total number of eligible voters within the contest.
    pub fn total_eligible_voter_count(mut self, count: impl Into<u64>) -> Self {
        self.total_eligible_voter_count = Some(StringValue::from_value(count.into()));
        self
    }

    /// Set the total number of votes on candidates within the contest.
    pub fn total_candidate_votes_count(mut self, count: impl Into<u64>) -> Self {
        self.total_candidate_votes_count = Some(StringValue::from_value(count.into()));
        self
    }

    /// Set the total number of rejected votes within the contest for a given reason.
    pub fn total_rejected_votes(
        mut self,
        reason: RejectedVotesReason,
        count: impl Into<u64>,
    ) -> Self {
        self.total_rejected_votes
            .insert(reason, StringValue::from_value(count.into()));
        self
    }

    /// Set the total number of uncounted votes within the contest for a given reason.
    pub fn total_uncounted_votes(
        mut self,
        reason: UncountedVotesReason,
        count: impl Into<u64>,
    ) -> Self {
        self.total_uncounted_votes
            .insert(reason, StringValue::from_value(count.into()));
        self
    }

    /// Set the details for all the reporting units within the contest. This
    /// overrides any previously set reporting unit votes.
    pub fn reporting_unit_votes(
        mut self,
        reporting_unit_votes: impl Into<Vec<ReportingUnitVotes>>,
    ) -> Self {
        self.reporting_unit_votes = reporting_unit_votes.into();
        self
    }

    /// Add the details for a reporting unit within the contest.
    pub fn push_reporting_unit_votes(
        mut self,
        reporting_unit_votes: impl Into<ReportingUnitVotes>,
    ) -> Self {
        self.reporting_unit_votes.push(reporting_unit_votes.into());
        self
    }

    /// Build the [`ElectionCountContest`] document, returning an error if any of the required fields are missing.
    pub fn build(self) -> Result<ElectionCountContest, EMLError> {
        Ok(ElectionCountContest {
            identifier: self
                .identifier
                .ok_or_else(|| EMLErrorKind::MissingBuildProperty("identifier").without_span())?,
            total_votes: self.total_votes.map_or_else(
                || {
                    if self.total_votes_selections.is_empty()
                        && self.total_eligible_voter_count.is_none()
                        && self.total_candidate_votes_count.is_none()
                        && self.total_rejected_votes.is_empty()
                        && self.total_uncounted_votes.is_empty()
                    {
                        Ok(None)
                    } else {
                        if self.total_votes_selections.is_empty() {
                            return Err(EMLErrorKind::MissingBuildProperty(
                                "total_votes_selections",
                            )
                            .without_span());
                        }

                        if !self
                            .total_rejected_votes
                            .contains_key(&RejectedVotesReason::Blank)
                        {
                            return Err(EMLErrorKind::MissingRejectedVotesBlank).without_span();
                        }

                        if !self
                            .total_rejected_votes
                            .contains_key(&RejectedVotesReason::Invalid)
                        {
                            return Err(EMLErrorKind::MissingRejectedVotesInvalid).without_span();
                        }

                        Ok(Some(TotalVotes {
                            selections: self.total_votes_selections,
                            eligible_voter_count: self.total_eligible_voter_count.ok_or_else(
                                || {
                                    EMLErrorKind::MissingBuildProperty("total_eligible_voter_count")
                                        .without_span()
                                },
                            )?,
                            candidate_votes_count: self.total_candidate_votes_count.ok_or_else(
                                || {
                                    EMLErrorKind::MissingBuildProperty(
                                        "total_candidate_votes_count",
                                    )
                                    .without_span()
                                },
                            )?,
                            rejected_votes: self.total_rejected_votes,
                            uncounted_votes: self.total_uncounted_votes,
                        }))
                    }
                },
                |v| Ok(Some(v)),
            )?,
            reporting_unit_votes: self.reporting_unit_votes,
        })
    }
}

impl Default for ElectionCountContestBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl EMLElement for ElectionCountContest {
    const EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Contest", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, ElectionCountContest {
            identifier: ContestIdentifier::EML_NAME => |elem| ContestIdentifier::read_eml(elem)?,
            total_votes as Option: TotalVotes::EML_NAME => |elem| TotalVotes::read_eml(elem)?,
            reporting_unit_votes as Vec: ReportingUnitVotes::EML_NAME => |elem| ReportingUnitVotes::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elem(ContestIdentifier::EML_NAME, &self.identifier)?
            .child_elem_option(TotalVotes::EML_NAME, self.total_votes.as_ref())?
            .child_elems(ReportingUnitVotes::EML_NAME, &self.reporting_unit_votes)?
            .finish()
    }
}

const REJECTED_VOTES_EML_NAME: QualifiedName<'_, '_> =
    QualifiedName::from_static("RejectedVotes", Some(NS_EML));

const UNCOUNTED_VOTES_EML_NAME: QualifiedName<'_, '_> =
    QualifiedName::from_static("UncountedVotes", Some(NS_EML));

/// Total votes in a contest.
#[derive(Debug, Clone)]
pub struct TotalVotes {
    /// Selections within the total votes.
    pub selections: Vec<ElectionCountSelection>,

    /// Total number of eligible voters within the reporting unit votes.
    ///
    /// This element is called `Cast` within EML_NL, but is renamed here to
    /// `eligible_voter_count` for clarity. This element was repurposed in
    /// EML_NL to represent the total number of eligible voters instead of the
    /// actual number of cast votes.
    pub eligible_voter_count: StringValue<u64>,

    /// Total number of votes on candidates.
    ///
    /// This element is called `TotalCounted` within EML_NL, but is renamed here
    /// to `candidate_votes_count` for clarity. This element was repurposed in
    /// EML_NL to represent the total number of votes on candidates instead of
    /// the actual total number of counted votes.
    pub candidate_votes_count: StringValue<u64>,

    /// Rejected votes within the reporting unit votes.
    ///
    /// Contains blank and invalid votes.
    pub rejected_votes: BTreeMap<RejectedVotesReason, StringValue<u64>>,

    /// Uncounted votes within the reporting unit votes.
    pub uncounted_votes: BTreeMap<UncountedVotesReason, StringValue<u64>>,
}

impl TotalVotes {
    /// Return the votes of each affiliation with each of their candidates and
    /// the amount of votes they received.
    ///
    /// This errors if any Selections of type ReferendumOption are encountered.
    pub fn selections_per_affiliation(
        &self,
    ) -> Result<Vec<SelectionAffiliationVotes<'_>>, EMLError> {
        selections_per_affiliation(&self.selections)
    }

    /// Return the total number of blank votes.
    pub fn blank_votes(&self) -> Result<&StringValue<u64>, EMLError> {
        self.rejected_votes
            .get(&RejectedVotesReason::Blank)
            .ok_or_else(|| EMLErrorKind::MissingRejectedVotesBlank.without_span())
    }

    /// Return the total number of invalid votes.
    pub fn invalid_votes(&self) -> Result<&StringValue<u64>, EMLError> {
        self.rejected_votes
            .get(&RejectedVotesReason::Invalid)
            .ok_or_else(|| EMLErrorKind::MissingRejectedVotesInvalid.without_span())
    }
}

impl EMLElement for TotalVotes {
    const EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("TotalVotes", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        let data = collect_struct!(elem, TotalVotes {
            selections as Vec: ElectionCountSelection::EML_NAME => |elem| ElectionCountSelection::read_eml(elem)?,
            eligible_voter_count: ("Cast", NS_EML) => |elem| elem.string_value()?,
            candidate_votes_count: ("TotalCounted", NS_EML) => |elem| elem.string_value()?,
            rejected_votes as BTreeMap: REJECTED_VOTES_EML_NAME => |elem| {
                let reason_code = elem.attribute_value_req("ReasonCode")?;
                let reason = RejectedVotesReason::from_eml_value(&reason_code)
                    .map_err(|e| EMLError::invalid_value(REJECTED_VOTES_EML_NAME.as_owned(), e, Some(elem.full_span())))?;

                (reason, elem.string_value()?)
            },
            uncounted_votes as BTreeMap: UNCOUNTED_VOTES_EML_NAME => |elem| {
                let reason_code = elem.attribute_value_req("ReasonCode")?;
                let reason = UncountedVotesReason::from_eml_value(&reason_code)
                    .map_err(|e| EMLError::invalid_value(UNCOUNTED_VOTES_EML_NAME.as_owned(), e, Some(elem.full_span())))?;

                (reason, elem.string_value()?)
            },
        });

        if !data
            .rejected_votes
            .contains_key(&RejectedVotesReason::Blank)
        {
            let err = EMLErrorKind::MissingRejectedVotesBlank.with_span(elem.full_span());
            if elem.parsing_mode().is_strict() {
                return Err(err);
            } else {
                elem.push_err(err);
            }
        }

        if !data
            .rejected_votes
            .contains_key(&RejectedVotesReason::Invalid)
        {
            let err = EMLErrorKind::MissingRejectedVotesInvalid.with_span(elem.full_span());
            if elem.parsing_mode().is_strict() {
                return Err(err);
            } else {
                elem.push_err(err);
            }
        }

        Ok(data)
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elems(ElectionCountSelection::EML_NAME, &self.selections)?
            .child(("Cast", NS_EML), |elem| {
                elem.text(self.eligible_voter_count.raw().as_ref())?
                    .finish()
            })?
            .child(("TotalCounted", NS_EML), |elem| {
                elem.text(self.candidate_votes_count.raw().as_ref())?
                    .finish()
            })?
            .child_elems_map(
                REJECTED_VOTES_EML_NAME,
                &self.rejected_votes,
                |elem, (reason, count)| {
                    let reason_code = reason.to_eml_value();
                    elem.attr("ReasonCode", reason_code.as_ref())?
                        .text(count.raw().as_ref())?
                        .finish()
                },
            )?
            .child_elems_map(
                UNCOUNTED_VOTES_EML_NAME,
                &self.uncounted_votes,
                |elem, (reason, count)| {
                    let reason_code = reason.to_eml_value();
                    elem.attr("ReasonCode", reason_code.as_ref())?
                        .text(count.raw().as_ref())?
                        .finish()
                },
            )?
            .finish()
    }
}

/// Votes per reporting unit.
#[derive(Debug, Clone)]
pub struct ReportingUnitVotes {
    /// Identifier for the reporting unit votes.
    pub identifier: ReportingUnitIdentifier,

    /// Selections within the reporting unit votes.
    pub selections: Vec<ElectionCountSelection>,

    /// Total number of eligible voters within the reporting unit votes.
    ///
    /// This element is called `Cast` within EML_NL, but is renamed here to
    /// `eligible_voter_count` for clarity. This element was repurposed in
    /// EML_NL to represent the total number of eligible voters instead of the
    /// actual number of cast votes.
    pub eligible_voter_count: StringValue<u64>,

    /// Total number of votes on candidates.
    ///
    /// This element is called `TotalCounted` within EML_NL, but is renamed here
    /// to `candidate_votes_count` for clarity. This element was repurposed in
    /// EML_NL to represent the total number of votes on candidates instead of
    /// the actual total number of counted votes.
    pub candidate_votes_count: StringValue<u64>,

    /// Rejected votes within the reporting unit votes.
    ///
    /// Contains blank and invalid votes.
    pub rejected_votes: BTreeMap<RejectedVotesReason, StringValue<u64>>,

    /// Uncounted votes within the reporting unit votes.
    pub uncounted_votes: BTreeMap<UncountedVotesReason, StringValue<u64>>,

    /// Investigations within the reporting unit votes.
    pub investigations: BTreeMap<InvestigationReason, StringValue<bool>>,
}

/// Gathered votes and candidates for an affiliation selection within a reporting unit or total votes for a contest.
pub struct SelectionAffiliationVotes<'a> {
    /// The affiliation selection for which the votes were gathered.
    pub affiliation: &'a AffiliationSelection,

    /// The total number of valid votes for this affiliation.
    pub valid_votes: u64,

    /// The candidates for this affiliation and the number of votes they received.
    pub candidates: Vec<SelectionCandidateVotes<'a>>,
}

/// Gathered votes for a candidate selection within some affiliation.
pub struct SelectionCandidateVotes<'a> {
    /// The affiliation to which the candidate belongs.
    pub affiliation: &'a AffiliationSelection,

    /// The candidate selection for which the votes were gathered.
    pub candidate: &'a CandidateSelection,

    /// The total number of valid votes for this candidate.
    pub valid_votes: u64,
}

impl ReportingUnitVotes {
    /// Create a builder for the [`ReportingUnitVotes`].
    pub fn builder() -> ReportingUnitVotesBuilder {
        ReportingUnitVotesBuilder::new()
    }

    /// Return the votes of each affiliation with each of their candidates and
    /// the amount of votes they received.
    ///
    /// This errors if any Selections of type ReferendumOption are encountered.
    pub fn selections_per_affiliation(
        &self,
    ) -> Result<Vec<SelectionAffiliationVotes<'_>>, EMLError> {
        selections_per_affiliation(&self.selections)
    }

    /// Return the number of blank votes for this reporting unit.
    pub fn blank_votes(&self) -> Result<&StringValue<u64>, EMLError> {
        self.rejected_votes
            .get(&RejectedVotesReason::Blank)
            .ok_or_else(|| EMLErrorKind::MissingRejectedVotesBlank.without_span())
    }

    /// Return the number of invalid votes for this reporting unit.
    pub fn invalid_votes(&self) -> Result<&StringValue<u64>, EMLError> {
        self.rejected_votes
            .get(&RejectedVotesReason::Invalid)
            .ok_or_else(|| EMLErrorKind::MissingRejectedVotesInvalid.without_span())
    }
}

fn selections_per_affiliation(
    selections: &[ElectionCountSelection],
) -> Result<Vec<SelectionAffiliationVotes<'_>>, EMLError> {
    let mut result = Vec::new();

    // Selections consist an affiliation selection followed by zero or more
    // candidate selections for that affiliation.
    let mut current_affiliation = None;
    let mut current_affiliation_selection: Option<&ElectionCountSelection> = None;
    let mut current_candidates = vec![];

    for selection in selections {
        match &selection.selection_type {
            ElectionCountSelectionType::Affiliation(affiliation) => {
                if let (Some(ca), Some(cas)) = (current_affiliation, current_affiliation_selection)
                {
                    result.push(SelectionAffiliationVotes {
                        affiliation: ca,
                        valid_votes: cas
                            .valid_votes
                            .copied_value()
                            .wrap_field_value_error(VALID_VOTES_EML_NAME)?,
                        candidates: current_candidates,
                    });
                }

                current_affiliation = Some(affiliation);
                current_affiliation_selection = Some(selection);
                current_candidates = vec![];
            }
            ElectionCountSelectionType::Candidate(candidate) => {
                // Candidate selections should only be encountered after an affiliation selection.
                let curr_aff = current_affiliation
                    .ok_or_else(|| EMLErrorKind::CandidateWithoutAffiliationFound.without_span())?;

                current_candidates.push(SelectionCandidateVotes {
                    affiliation: curr_aff,
                    candidate,
                    valid_votes: selection
                        .valid_votes
                        .copied_value()
                        .wrap_field_value_error(VALID_VOTES_EML_NAME)?,
                });
            }
            // Referendum option selections should not be encountered at all.
            ElectionCountSelectionType::ReferendumOption(_) => {
                return Err(EMLErrorKind::UnexpectedReferendumOptionSelection.without_span());
            }
        }
    }

    // push the last affiliation and its candidates if it exists
    if let (Some(ca), Some(cas)) = (current_affiliation, current_affiliation_selection) {
        result.push(SelectionAffiliationVotes {
            affiliation: ca,
            valid_votes: cas
                .valid_votes
                .copied_value()
                .wrap_field_value_error(VALID_VOTES_EML_NAME)?,
            candidates: current_candidates,
        });
    }

    Ok(result)
}

/// A builder for [`ReportingUnitVotes`].
#[derive(Debug, Clone)]
pub struct ReportingUnitVotesBuilder {
    identifier: Option<ReportingUnitIdentifier>,
    selections: Vec<ElectionCountSelection>,
    eligible_voter_count: Option<StringValue<u64>>,
    candidate_votes_count: Option<StringValue<u64>>,
    rejected_votes: BTreeMap<RejectedVotesReason, StringValue<u64>>,
    uncounted_votes: BTreeMap<UncountedVotesReason, StringValue<u64>>,
    investigations: BTreeMap<InvestigationReason, StringValue<bool>>,
}

impl ReportingUnitVotesBuilder {
    /// Create a new ReportingUnitVotesBuilder for building [`ReportingUnitVotes`] documents.
    pub fn new() -> Self {
        Self {
            identifier: None,
            selections: vec![],
            eligible_voter_count: None,
            candidate_votes_count: None,
            rejected_votes: BTreeMap::new(),
            uncounted_votes: BTreeMap::new(),
            investigations: BTreeMap::new(),
        }
    }

    /// Set the identifier for the reporting unit votes.
    pub fn identifier(mut self, identifier: impl Into<ReportingUnitIdentifier>) -> Self {
        self.identifier = Some(identifier.into());
        self
    }

    /// Set the selections within the reporting unit votes. This overrides any previously set selections.
    pub fn selections(mut self, selections: impl Into<Vec<ElectionCountSelection>>) -> Self {
        self.selections = selections.into();
        self
    }

    /// Add a selection to the selections within the reporting unit votes.
    pub fn push_selection(mut self, selection: impl Into<ElectionCountSelection>) -> Self {
        self.selections.push(selection.into());
        self
    }

    /// Set the total number of eligible voters within the reporting unit votes.
    pub fn eligible_voter_count(mut self, count: impl Into<u64>) -> Self {
        self.eligible_voter_count = Some(StringValue::from_value(count.into()));
        self
    }

    /// Set the total number of votes on candidates within the reporting unit votes.
    pub fn candidate_votes_count(mut self, count: impl Into<u64>) -> Self {
        self.candidate_votes_count = Some(StringValue::from_value(count.into()));
        self
    }

    /// Set the total number of rejected votes within the reporting unit votes for a given reason.
    pub fn rejected_votes(mut self, reason: RejectedVotesReason, count: impl Into<u64>) -> Self {
        self.rejected_votes
            .insert(reason, StringValue::from_value(count.into()));
        self
    }

    /// Set the total number of uncounted votes within the reporting unit votes for a given reason.
    pub fn uncounted_votes(mut self, reason: UncountedVotesReason, count: impl Into<u64>) -> Self {
        self.uncounted_votes
            .insert(reason, StringValue::from_value(count.into()));
        self
    }

    /// Set the investigations within the reporting unit votes for a given reason.
    pub fn investigation(mut self, reason: InvestigationReason, value: bool) -> Self {
        self.investigations
            .insert(reason, StringValue::from_value(value));
        self
    }

    /// Build the [`ReportingUnitVotes`] document, returning an error if any of the required fields are missing.
    pub fn build(self) -> Result<ReportingUnitVotes, EMLError> {
        if self.selections.is_empty() {
            return Err(EMLErrorKind::MissingBuildProperty("selections").without_span());
        }

        if !self
            .rejected_votes
            .contains_key(&RejectedVotesReason::Blank)
        {
            return Err(EMLErrorKind::MissingRejectedVotesBlank).without_span();
        }

        if !self
            .rejected_votes
            .contains_key(&RejectedVotesReason::Invalid)
        {
            return Err(EMLErrorKind::MissingRejectedVotesInvalid).without_span();
        }

        Ok(ReportingUnitVotes {
            identifier: self
                .identifier
                .ok_or_else(|| EMLErrorKind::MissingBuildProperty("identifier").without_span())?,
            selections: self.selections,
            eligible_voter_count: self.eligible_voter_count.ok_or_else(|| {
                EMLErrorKind::MissingBuildProperty("eligible_voter_count").without_span()
            })?,
            candidate_votes_count: self.candidate_votes_count.ok_or_else(|| {
                EMLErrorKind::MissingBuildProperty("candidate_votes_count").without_span()
            })?,
            rejected_votes: self.rejected_votes,
            uncounted_votes: self.uncounted_votes,
            investigations: self.investigations,
        })
    }
}

impl Default for ReportingUnitVotesBuilder {
    fn default() -> Self {
        Self::new()
    }
}

const REPORTING_UNIT_INVESTIGATIONS_EML_NAME: QualifiedName<'_, '_> =
    QualifiedName::from_static("ReportingUnitInvestigations", Some(NS_KR));

const INVESTIGATION_EML_NAME: QualifiedName<'_, '_> =
    QualifiedName::from_static("Investigation", Some(NS_KR));

impl EMLElement for ReportingUnitVotes {
    const EML_NAME: QualifiedName<'_, '_> =
        QualifiedName::from_static("ReportingUnitVotes", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        struct ReportingUnitVotesInternal {
            identifier: ReportingUnitIdentifier,
            selections: Vec<ElectionCountSelection>,
            eligible_voter_count: StringValue<u64>,
            candidate_votes_count: StringValue<u64>,
            rejected_votes: BTreeMap<RejectedVotesReason, StringValue<u64>>,
            uncounted_votes: BTreeMap<UncountedVotesReason, StringValue<u64>>,
            investigations: Option<BTreeMap<InvestigationReason, StringValue<bool>>>,
        }

        let data = collect_struct!(elem, ReportingUnitVotesInternal {
            identifier: ReportingUnitIdentifier::EML_NAME => |elem| ReportingUnitIdentifier::read_eml(elem)?,
            selections as Vec: ElectionCountSelection::EML_NAME => |elem| ElectionCountSelection::read_eml(elem)?,
            eligible_voter_count: ("Cast", NS_EML) => |elem| elem.string_value()?,
            candidate_votes_count: ("TotalCounted", NS_EML) => |elem| elem.string_value()?,
            rejected_votes as BTreeMap: REJECTED_VOTES_EML_NAME => |elem| {
                let reason_code = elem.attribute_value_req("ReasonCode")?;
                let reason = RejectedVotesReason::from_eml_value(&reason_code)
                    .map_err(|e| EMLError::invalid_value(REJECTED_VOTES_EML_NAME.as_owned(), e, Some(elem.full_span())))?;

                (reason, elem.string_value()?)
            },
            uncounted_votes as BTreeMap: UNCOUNTED_VOTES_EML_NAME => |elem| {
                let reason_code = elem.attribute_value_req("ReasonCode")?;
                let reason = UncountedVotesReason::from_eml_value(&reason_code)
                    .map_err(|e| EMLError::invalid_value(UNCOUNTED_VOTES_EML_NAME.as_owned(), e, Some(elem.full_span())))?;

                (reason, elem.string_value()?)
            },
            investigations as Option: REPORTING_UNIT_INVESTIGATIONS_EML_NAME => |elem| {
                struct Collector {
                    investigations: BTreeMap<InvestigationReason, StringValue<bool>>,
                }

                let data = collect_struct!(elem, Collector {
                    investigations as BTreeMap: INVESTIGATION_EML_NAME => |elem| {
                        let reason_code = elem.attribute_value_req("ReasonCode")?;
                        let reason = InvestigationReason::from_eml_value(&reason_code)
                            .map_err(|e| EMLError::invalid_value(INVESTIGATION_EML_NAME.as_owned(), e, Some(elem.full_span())))?;

                        (reason, elem.string_value()?)
                    },
                });

                data.investigations
            },
        });

        if !data
            .rejected_votes
            .contains_key(&RejectedVotesReason::Blank)
        {
            let err = EMLErrorKind::MissingRejectedVotesBlank.with_span(elem.full_span());
            if elem.parsing_mode().is_strict() {
                return Err(err);
            } else {
                elem.push_err(err);
            }
        }

        if !data
            .rejected_votes
            .contains_key(&RejectedVotesReason::Invalid)
        {
            let err = EMLErrorKind::MissingRejectedVotesInvalid.with_span(elem.full_span());
            if elem.parsing_mode().is_strict() {
                return Err(err);
            } else {
                elem.push_err(err);
            }
        }

        Ok(ReportingUnitVotes {
            identifier: data.identifier,
            selections: data.selections,
            eligible_voter_count: data.eligible_voter_count,
            candidate_votes_count: data.candidate_votes_count,
            rejected_votes: data.rejected_votes,
            uncounted_votes: data.uncounted_votes,
            investigations: data.investigations.unwrap_or_default(),
        })
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elem(ReportingUnitIdentifier::EML_NAME, &self.identifier)?
            .child_elems(ElectionCountSelection::EML_NAME, &self.selections)?
            .child(("Cast", NS_EML), |elem| {
                elem.text(self.eligible_voter_count.raw().as_ref())?
                    .finish()
            })?
            .child(("TotalCounted", NS_EML), |elem| {
                elem.text(self.candidate_votes_count.raw().as_ref())?
                    .finish()
            })?
            .child_elems_map(
                REJECTED_VOTES_EML_NAME,
                &self.rejected_votes,
                |elem, (reason, count)| {
                    let reason_code = reason.to_eml_value();
                    elem.attr("ReasonCode", reason_code.as_ref())?
                        .text(count.raw().as_ref())?
                        .finish()
                },
            )?
            .child_elems_map(
                UNCOUNTED_VOTES_EML_NAME,
                &self.uncounted_votes,
                |elem, (reason, count)| {
                    let reason_code = reason.to_eml_value();
                    elem.attr("ReasonCode", reason_code.as_ref())?
                        .text(count.raw().as_ref())?
                        .finish()
                },
            )?
            .child_option(
                REPORTING_UNIT_INVESTIGATIONS_EML_NAME,
                if self.investigations.is_empty() {
                    None
                } else {
                    Some(&self.investigations)
                },
                |elem, value| {
                    let mut elem = elem.content()?;

                    for (reason, investigated) in value {
                        elem = elem.child(INVESTIGATION_EML_NAME, |elem| {
                            let reason_code = reason.to_eml_value();
                            elem.attr("ReasonCode", reason_code.as_ref())?
                                .text(investigated.raw().as_ref())?
                                .finish()
                        })?;
                    }

                    elem.finish()
                },
            )?
            .finish()
    }
}

/// Reason code for a specific investigation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum InvestigationReason {
    /// onderzocht vanwege onverklaard verschil
    UnexplainedDifference,
    /// onderzocht vanwege andere fout
    OtherError,
    /// uitslag gecorrigeerd
    ResultCorrected,
    /// toegelaten kiezers opnieuw vastgesteld
    AdmittedVotersReestablished,
    /// onderzocht vanwege andere reden
    OtherReason,
    /// stembiljetten deels herteld
    PartiallyRecountedBallots,
}

impl InvestigationReason {
    /// Create an InvestigationReason from an EML reason code string.
    pub fn from_eml_value(s: impl AsRef<str>) -> Result<Self, InvalidInvestigationReasonError> {
        let data = s.as_ref();
        match data {
            "onderzocht vanwege onverklaard verschil" => {
                Ok(InvestigationReason::UnexplainedDifference)
            }
            "onderzocht vanwege andere fout" => Ok(InvestigationReason::OtherError),
            "uitslag gecorrigeerd" => Ok(InvestigationReason::ResultCorrected),
            "toegelaten kiezers opnieuw vastgesteld" => {
                Ok(InvestigationReason::AdmittedVotersReestablished)
            }
            "onderzocht vanwege andere reden" => Ok(InvestigationReason::OtherReason),
            "stembiljetten deels herteld" => Ok(InvestigationReason::PartiallyRecountedBallots),
            _ => Err(InvalidInvestigationReasonError(data.to_string())),
        }
    }

    /// Get the EML reason code string for this InvestigationReason.
    pub fn to_eml_value(&self) -> &'static str {
        match self {
            InvestigationReason::UnexplainedDifference => "onderzocht vanwege onverklaard verschil",
            InvestigationReason::OtherError => "onderzocht vanwege andere fout",
            InvestigationReason::ResultCorrected => "uitslag gecorrigeerd",
            InvestigationReason::AdmittedVotersReestablished => {
                "toegelaten kiezers opnieuw vastgesteld"
            }
            InvestigationReason::OtherReason => "onderzocht vanwege andere reden",
            InvestigationReason::PartiallyRecountedBallots => "stembiljetten deels herteld",
        }
    }
}

/// Error indicating an invalid InvestigationReason.
#[derive(Debug, Clone, thiserror::Error)]
#[error("Invalid investigation reason: {0}")]
pub struct InvalidInvestigationReasonError(String);

/// Reason code for a specific uncounted votes entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum UncountedVotesReason {
    /// geldige stempassen
    ValidPollCards,
    /// geldige volmachtbewijzen
    ValidProxyCertificates,
    /// geldige kiezerspassen
    ValidVoterCards,
    /// toegelaten kiezers
    AdmittedVoters,
    /// meer getelde stembiljetten
    MoreBallotsCounted,
    /// minder getelde stembiljetten
    FewerBallotsCounted,
    /// meegenomen stembiljetten
    BallotsTaken,
    /// te weinig uitgereikte stembiljetten
    TooFewBallotsIssued,
    /// te veel uitgereikte stembiljetten
    TooManyBallotsIssued,
    /// geen briefstembiljetten
    NoPostalBallots,
    /// te veel briefstembiljetten
    TooManyPostalBallots,
    /// kwijtgeraakte stembiljetten
    LostBallots,
    /// geen verklaring
    NoExplanation,
    /// andere verklaring
    OtherExplanation,
}

impl UncountedVotesReason {
    /// Create an UncountedVotesReason from an EML reason code string.
    pub fn from_eml_value(s: impl AsRef<str>) -> Result<Self, InvalidUncountedVotesReasonError> {
        match s.as_ref() {
            "geldige stempassen" => Ok(UncountedVotesReason::ValidPollCards),
            "geldige volmachtbewijzen" => Ok(UncountedVotesReason::ValidProxyCertificates),
            "geldige kiezerspassen" => Ok(UncountedVotesReason::ValidVoterCards),
            "toegelaten kiezers" => Ok(UncountedVotesReason::AdmittedVoters),
            "meer getelde stembiljetten" => Ok(UncountedVotesReason::MoreBallotsCounted),
            "minder getelde stembiljetten" => Ok(UncountedVotesReason::FewerBallotsCounted),
            "meegenomen stembiljetten" => Ok(UncountedVotesReason::BallotsTaken),
            "te weinig uitgereikte stembiljetten" => Ok(UncountedVotesReason::TooFewBallotsIssued),
            "te veel uitgereikte stembiljetten" => Ok(UncountedVotesReason::TooManyBallotsIssued),
            "geen briefstembiljetten" => Ok(UncountedVotesReason::NoPostalBallots),
            "te veel briefstembiljetten" => Ok(UncountedVotesReason::TooManyPostalBallots),
            "kwijtgeraakte stembiljetten" => Ok(UncountedVotesReason::LostBallots),
            "geen verklaring" => Ok(UncountedVotesReason::NoExplanation),
            "andere verklaring" => Ok(UncountedVotesReason::OtherExplanation),
            _ => Err(InvalidUncountedVotesReasonError(s.as_ref().to_string())),
        }
    }

    /// Get the EML reason code string for this UncountedVotesReason.
    pub fn to_eml_value(&self) -> &'static str {
        match self {
            UncountedVotesReason::ValidPollCards => "geldige stempassen",
            UncountedVotesReason::ValidProxyCertificates => "geldige volmachtbewijzen",
            UncountedVotesReason::ValidVoterCards => "geldige kiezerspassen",
            UncountedVotesReason::AdmittedVoters => "toegelaten kiezers",
            UncountedVotesReason::MoreBallotsCounted => "meer getelde stembiljetten",
            UncountedVotesReason::FewerBallotsCounted => "minder getelde stembiljetten",
            UncountedVotesReason::BallotsTaken => "meegenomen stembiljetten",
            UncountedVotesReason::TooFewBallotsIssued => "te weinig uitgereikte stembiljetten",
            UncountedVotesReason::TooManyBallotsIssued => "te veel uitgereikte stembiljetten",
            UncountedVotesReason::NoPostalBallots => "geen briefstembiljetten",
            UncountedVotesReason::TooManyPostalBallots => "te veel briefstembiljetten",
            UncountedVotesReason::LostBallots => "kwijtgeraakte stembiljetten",
            UncountedVotesReason::NoExplanation => "geen verklaring",
            UncountedVotesReason::OtherExplanation => "andere verklaring",
        }
    }
}

/// Error indicating an invalid uncounted votes reason.
#[derive(Debug, Clone, thiserror::Error)]
#[error("Invalid uncounted votes reason: {0}")]
pub struct InvalidUncountedVotesReasonError(String);

/// Reason code for rejected votes entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum RejectedVotesReason {
    /// Blank votes ("blanco")
    Blank,
    /// Invalid votes ("ongeldig")
    Invalid,
}

impl RejectedVotesReason {
    /// Create a RejectedVotesReason from an EML reason code string.
    pub fn from_eml_value(s: impl AsRef<str>) -> Result<Self, InvalidRejectedVotesReasonError> {
        let data = s.as_ref();
        match data {
            "blanco" => Ok(RejectedVotesReason::Blank),
            "ongeldig" => Ok(RejectedVotesReason::Invalid),
            _ => Err(InvalidRejectedVotesReasonError(data.to_string())),
        }
    }

    /// Get the EML reason code string for this RejectedVotesReason.
    pub fn to_eml_value(&self) -> &'static str {
        match self {
            RejectedVotesReason::Blank => "blanco",
            RejectedVotesReason::Invalid => "ongeldig",
        }
    }
}

/// Error indicating an invalid rejected votes reason.
#[derive(Debug, Clone, thiserror::Error)]
#[error("Invalid rejected votes reason: {0}")]
pub struct InvalidRejectedVotesReasonError(String);

/// A selection within the reporting unit votes.
#[derive(Debug, Clone)]
pub struct ElectionCountSelection {
    /// Type of selection.
    pub selection_type: ElectionCountSelectionType,

    /// Number of valid votes for this selection.
    pub valid_votes: StringValue<u64>,

    /// Value of the `Value` attribute, if present.
    pub value: Option<String>,

    /// Value of the `Category` attribute, if present.
    pub category: Option<String>,
}

impl ElectionCountSelection {
    /// Create a builder for the [`ElectionCountSelection`].
    pub fn builder() -> ElectionCountSelectionBuilder {
        ElectionCountSelectionBuilder::new()
    }
}

/// A builder for [`ElectionCountSelection`].
#[derive(Debug, Clone)]
pub struct ElectionCountSelectionBuilder {
    selection_type: Option<ElectionCountSelectionType>,
    valid_votes: Option<StringValue<u64>>,
    value: Option<String>,
    category: Option<String>,
}

impl ElectionCountSelectionBuilder {
    /// Create a new ElectionCountSelectionBuilder for building [`ElectionCountSelection`] documents.
    pub fn new() -> Self {
        Self {
            selection_type: None,
            valid_votes: None,
            value: None,
            category: None,
        }
    }

    /// Set the selection type to a candidate selection with the given candidate.
    pub fn candidate(mut self, candidate: impl Into<CandidateSelection>) -> Self {
        self.selection_type = Some(ElectionCountSelectionType::Candidate(Box::new(
            candidate.into(),
        )));
        self
    }

    /// Set the selection type to an affiliation selection with the given affiliation.
    pub fn affiliation(mut self, affiliation: impl Into<AffiliationSelection>) -> Self {
        self.selection_type = Some(ElectionCountSelectionType::Affiliation(Box::new(
            affiliation.into(),
        )));
        self
    }

    /// Set the selection type to a referendum option selection with the given referendum option.
    pub fn referendum_option(
        mut self,
        referendum_option: impl Into<ReferendumOptionSelection>,
    ) -> Self {
        self.selection_type = Some(ElectionCountSelectionType::ReferendumOption(Box::new(
            referendum_option.into(),
        )));
        self
    }

    /// Set the number of valid votes for the election count selection.
    pub fn valid_votes(mut self, valid_votes: impl Into<u64>) -> Self {
        self.valid_votes = Some(StringValue::from_value(valid_votes.into()));
        self
    }

    /// Set the Value attribute of the election count selection.
    pub fn value(mut self, value: impl Into<String>) -> Self {
        self.value = Some(value.into());
        self
    }

    /// Set the Category attribute of the election count selection.
    pub fn category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }

    /// Build the [`ElectionCountSelection`] document, returning an error if any of the required fields are missing.
    pub fn build(self) -> Result<ElectionCountSelection, EMLError> {
        Ok(ElectionCountSelection {
            selection_type: self.selection_type.ok_or_else(|| {
                EMLErrorKind::MissingBuildProperty("selection_type").without_span()
            })?,
            valid_votes: self
                .valid_votes
                .ok_or_else(|| EMLErrorKind::MissingBuildProperty("valid_votes").without_span())?,
            value: self.value,
            category: self.category,
        })
    }
}

impl Default for ElectionCountSelectionBuilder {
    fn default() -> Self {
        Self::new()
    }
}

const VALID_VOTES_EML_NAME: QualifiedName<'_, '_> =
    QualifiedName::from_static("ValidVotes", Some(NS_EML));

impl EMLElement for ElectionCountSelection {
    const EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Selection", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        let value = elem.attribute_value("Value")?.map(|s| s.to_string());
        let category = elem.attribute_value("Category")?.map(|s| s.to_string());
        let mut selection_type = None;
        let mut valid_votes = None;

        while let Some(mut child) = elem.next_child()? {
            let name = child.name()?;

            match name {
                n if n == CandidateSelection::EML_NAME => {
                    selection_type = Some(ElectionCountSelectionType::Candidate(Box::new(
                        CandidateSelection::read_eml(&mut child)?,
                    )));
                }
                n if n == AffiliationSelection::EML_NAME => {
                    selection_type = Some(ElectionCountSelectionType::Affiliation(Box::new(
                        AffiliationSelection::read_eml(&mut child)?,
                    )));
                }
                n if n == ReferendumOptionSelection::EML_NAME => {
                    selection_type = Some(ElectionCountSelectionType::ReferendumOption(Box::new(
                        ReferendumOptionSelection::read_eml(&mut child)?,
                    )));
                }
                n if n == VALID_VOTES_EML_NAME => {
                    valid_votes = Some(child.string_value()?);
                }
                n => {
                    let err =
                        EMLErrorKind::UnexpectedElement(n.as_owned(), Self::EML_NAME.as_owned())
                            .with_span(child.inner_span());
                    if child.parsing_mode().is_strict() {
                        return Err(err);
                    } else {
                        child.push_err(err);
                        child.skip()?;
                    }
                }
            }
        }
        Ok(ElectionCountSelection {
            selection_type: selection_type
                .ok_or_else(|| EMLErrorKind::MissingSelectionType.with_span(elem.inner_span()))?,
            valid_votes: valid_votes.ok_or_else(|| {
                EMLErrorKind::MissingElement(VALID_VOTES_EML_NAME.as_owned())
                    .with_span(elem.inner_span())
            })?,
            value,
            category,
        })
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        let writer = writer
            .attr_opt("Value", self.value.as_deref())?
            .attr_opt("Category", self.category.as_deref())?;
        let writer = match &self.selection_type {
            ElectionCountSelectionType::Candidate(candidate_selection) => {
                writer.child_elem(CandidateSelection::EML_NAME, candidate_selection.as_ref())?
            }
            ElectionCountSelectionType::Affiliation(affiliation_selection) => writer.child_elem(
                AffiliationSelection::EML_NAME,
                affiliation_selection.as_ref(),
            )?,
            ElectionCountSelectionType::ReferendumOption(referendum_option_selection) => writer
                .child_elem(
                    ReferendumOptionSelection::EML_NAME,
                    referendum_option_selection.as_ref(),
                )?,
        };
        writer
            .child(("ValidVotes", NS_EML), |elem| {
                elem.text(self.valid_votes.raw().as_ref())?.finish()
            })?
            .finish()
    }
}

/// The type of selection.
#[derive(Debug, Clone)]
pub enum ElectionCountSelectionType {
    /// Selection of a candidate.
    Candidate(Box<CandidateSelection>),

    /// Selection of an affiliation.
    Affiliation(Box<AffiliationSelection>),

    /// Selection of a referendum option.
    ReferendumOption(Box<ReferendumOptionSelection>),
}

impl ElectionCountSelectionType {
    /// Check if the selection type is a candidate selection.
    pub fn is_candidate(&self) -> bool {
        matches!(self, ElectionCountSelectionType::Candidate(_))
    }

    /// Check if the selection type is an affiliation selection.
    pub fn is_affiliation(&self) -> bool {
        matches!(self, ElectionCountSelectionType::Affiliation(_))
    }

    /// Check if the selection type is a referendum option selection.
    pub fn is_referendum_option(&self) -> bool {
        matches!(self, ElectionCountSelectionType::ReferendumOption(_))
    }

    /// Get the candidate selection if the selection type is a candidate selection.
    pub fn as_candidate(&self) -> Option<&CandidateSelection> {
        if let ElectionCountSelectionType::Candidate(candidate_selection) = self {
            Some(candidate_selection)
        } else {
            None
        }
    }

    /// Get the affiliation selection if the selection type is an affiliation selection.
    pub fn as_affiliation(&self) -> Option<&AffiliationSelection> {
        if let ElectionCountSelectionType::Affiliation(affiliation_selection) = self {
            Some(affiliation_selection)
        } else {
            None
        }
    }

    /// Get the referendum option selection if the selection type is a referendum option selection.
    pub fn as_referendum_option(&self) -> Option<&ReferendumOptionSelection> {
        if let ElectionCountSelectionType::ReferendumOption(referendum_option_selection) = self {
            Some(referendum_option_selection)
        } else {
            None
        }
    }
}

/// Selection of a candidate.
#[derive(Debug, Clone)]
pub struct CandidateSelection {
    /// Identifier of the candidate.
    pub identifier: CandidateIdentifier,

    /// Name of the candidate.
    pub name: Option<PersonNameStructure>,

    /// Gender of the candidate.
    pub gender: Option<StringValue<Gender>>,

    /// Qualified address of the candidate, if present.
    pub qualifying_address: Option<MinimalQualifyingAddress>,
}

impl CandidateSelection {
    /// Create a new builder for building an instance.
    pub fn builder() -> CandidateSelectionBuilder {
        CandidateSelectionBuilder::new()
    }
}

impl From<CandidateIdentifier> for CandidateSelection {
    fn from(identifier: CandidateIdentifier) -> Self {
        CandidateSelection {
            identifier,
            name: None,
            gender: None,
            qualifying_address: None,
        }
    }
}

impl From<CandidateId> for CandidateSelection {
    fn from(id: CandidateId) -> Self {
        CandidateSelection::from(CandidateIdentifier::from(id))
    }
}

/// A builder for [`CandidateSelection`].
#[derive(Debug, Clone)]
pub struct CandidateSelectionBuilder {
    identifier: Option<CandidateIdentifier>,
    name: Option<PersonNameStructure>,
    gender: Option<StringValue<Gender>>,
    qualifying_address: Option<MinimalQualifyingAddress>,
    locality_name: Option<String>,
    country_name_code: Option<String>,
}

impl CandidateSelectionBuilder {
    /// Create a new CandidateSelectionBuilder for building [`CandidateSelection`] instances.
    pub fn new() -> Self {
        CandidateSelectionBuilder {
            identifier: None,
            name: None,
            gender: None,
            qualifying_address: None,
            locality_name: None,
            country_name_code: None,
        }
    }

    /// Set the identifier of the candidate selection.
    pub fn identifier(mut self, identifier: impl Into<CandidateIdentifier>) -> Self {
        self.identifier = Some(identifier.into());
        self
    }

    /// Set the name of the candidate selection.
    pub fn name(mut self, name: impl Into<PersonNameStructure>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the gender of the candidate selection.
    pub fn gender(mut self, gender: impl Into<Gender>) -> Self {
        self.gender = Some(StringValue::from_value(gender.into()));
        self
    }

    /// Set the minimal qualifying address of the candidate selection.
    ///
    /// You may also set the locality name and country name code separately
    /// using the [`Self::locality_name`] and [`Self::country_name_code`] methods.
    pub fn qualifying_address(
        mut self,
        qualifying_address: impl Into<MinimalQualifyingAddress>,
    ) -> Self {
        self.qualifying_address = Some(qualifying_address.into());
        self
    }

    /// Set the locality name for the candidate selection.
    ///
    /// Has no effect if the qualifying address is already set using the
    /// [`Self::qualifying_address`] method.
    pub fn locality_name(mut self, locality_name: impl Into<String>) -> Self {
        self.locality_name = Some(locality_name.into());
        self
    }

    /// Set the country name code for the candidate selection.
    ///
    /// Has no effect if the qualifying address is already set using the
    /// [`Self::qualifying_address`] method.
    pub fn country_name_code(mut self, country_name_code: impl Into<String>) -> Self {
        self.country_name_code = Some(country_name_code.into());
        self
    }

    /// Build a CandidateSelection, returning an error if any required properties are missing.
    pub fn build(self) -> Result<CandidateSelection, EMLError> {
        Ok(CandidateSelection {
            identifier: self
                .identifier
                .ok_or_else(|| EMLErrorKind::MissingBuildProperty("identifier").without_span())?,
            name: self.name,
            gender: self.gender,
            qualifying_address: self.qualifying_address.map_or_else(
                || {
                    if let Some(locality_name) = self.locality_name {
                        if let Some(country_name_code) = self.country_name_code {
                            Ok(Some(MinimalQualifyingAddress::new_country(
                                country_name_code,
                                locality_name,
                            )))
                        } else {
                            Ok(Some(MinimalQualifyingAddress::new_locality(locality_name)))
                        }
                    } else {
                        if self.country_name_code.is_some() {
                            return Err(
                                EMLErrorKind::MissingBuildProperty("locality_name").without_span()
                            );
                        }
                        Ok(None)
                    }
                },
                |address| Ok(Some(address)),
            )?,
        })
    }
}

impl Default for CandidateSelectionBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl EMLElement for CandidateSelection {
    const EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Candidate", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, CandidateSelection {
            identifier: CandidateIdentifier::EML_NAME => |elem| CandidateIdentifier::read_eml(elem)?,
            name as Option: ("CandidateFullName", NS_EML) => |elem| PersonNameStructure::read_eml_element(elem)?,
            gender as Option: ("Gender", NS_EML) => |elem| elem.string_value()?,
            qualifying_address as Option: MinimalQualifyingAddress::EML_NAME => |elem| MinimalQualifyingAddress::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elem(CandidateIdentifier::EML_NAME, &self.identifier)?
            .child_option(
                ("CandidateFullName", NS_EML),
                self.name.as_ref(),
                |elem, value| value.write_eml_element(elem),
            )?
            .child_option(("Gender", NS_EML), self.gender.as_ref(), |elem, value| {
                elem.text(value.raw().as_ref())?.finish()
            })?
            .child_elem_option(
                MinimalQualifyingAddress::EML_NAME,
                self.qualifying_address.as_ref(),
            )?
            .finish()
    }
}

/// Selection of an affiliation.
#[derive(Debug, Clone)]
pub struct AffiliationSelection {
    /// Id of the affiliation.
    pub id: StringValue<AffiliationId>,

    /// Name of the affiliation.
    pub name: String,
}

impl AffiliationSelection {
    /// Create a new AffiliationSelection with the given id and name.
    pub fn new(id: impl Into<AffiliationId>, name: impl Into<String>) -> Self {
        AffiliationSelection {
            id: StringValue::from_value(id.into()),
            name: name.into(),
        }
    }
}

impl EMLElement for AffiliationSelection {
    const EML_NAME: QualifiedName<'_, '_> =
        QualifiedName::from_static("AffiliationIdentifier", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, AffiliationSelection {
            id: elem.string_value_attr("Id", None)?,
            name: ("RegisteredName", NS_EML) => |elem| elem.text_without_children()?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .attr("Id", self.id.raw().as_ref())?
            .child(("RegisteredName", NS_EML), |elem| {
                elem.text(self.name.as_ref())?.finish()
            })?
            .finish()
    }
}

/// Selection of a referendum option.
#[derive(Debug, Clone)]
pub struct ReferendumOptionSelection {
    /// Value of the referendum option.
    pub value: String,

    /// Id of the referendum option, if present.
    pub id: Option<String>,

    /// Display order of the referendum option, if present.
    pub display_order: Option<StringValue<NonZeroU64>>,

    /// Short code of the referendum option, if present.
    pub short_code: Option<String>,

    /// Expected confirmation reference of the referendum option, if present.
    pub expected_confirmation_reference: Option<String>,
}

impl ReferendumOptionSelection {
    /// Create a new ReferendumOptionSelection with the given value.
    pub fn new(value: impl Into<String>) -> Self {
        ReferendumOptionSelection {
            value: value.into(),
            id: None,
            display_order: None,
            short_code: None,
            expected_confirmation_reference: None,
        }
    }

    /// Set the Id attribute of the referendum option.
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Set the DisplayOrder attribute of the referendum option.
    pub fn with_display_order(mut self, display_order: impl Into<NonZeroU64>) -> Self {
        self.display_order = Some(StringValue::from_value(display_order.into()));
        self
    }

    /// Set the ShortCode attribute of the referendum option.
    pub fn with_short_code(mut self, short_code: impl Into<String>) -> Self {
        self.short_code = Some(short_code.into());
        self
    }

    /// Set the ExpectedConfirmationReference attribute of the referendum option.
    pub fn with_expected_confirmation_reference(
        mut self,
        expected_confirmation_reference: impl Into<String>,
    ) -> Self {
        self.expected_confirmation_reference = Some(expected_confirmation_reference.into());
        self
    }
}

impl EMLElement for ReferendumOptionSelection {
    const EML_NAME: QualifiedName<'_, '_> =
        QualifiedName::from_static("ReferendumOptionIdentifier", Some(NS_EML));

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(ReferendumOptionSelection {
            value: elem.text_without_children()?,
            id: elem.attribute_value("Id")?.map(|s| s.into_owned()),
            display_order: elem.string_value_attr_opt("DisplayOrder")?,
            short_code: elem.attribute_value("ShortCode")?.map(|s| s.into_owned()),
            expected_confirmation_reference: elem
                .attribute_value("ExpectedConfirmationReference")?
                .map(|s| s.into_owned()),
        })
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .attr_opt("Id", self.id.as_deref())?
            .attr_opt("DisplayOrder", self.display_order.as_ref().map(|s| s.raw()))?
            .attr_opt("ShortCode", self.short_code.as_deref())?
            .attr_opt(
                "ExpectedConfirmationReference",
                self.expected_confirmation_reference.as_deref(),
            )?
            .text(self.value.as_ref())?
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use chrono::{NaiveDate, TimeZone as _};

    use crate::{
        common::{AuthorityIdentifier, PersonName},
        io::{EMLParsingMode, EMLRead, EMLWrite},
        utils::{AuthorityId, CandidateId, ReportingUnitIdentifierId},
    };

    use super::*;

    #[test]
    fn test_election_count_construction() {
        let ec = ElectionCount::builder()
            .count_type(CountType::Municipal)
            .transaction_id(TransactionId::new(1))
            .creation_date_time(
                chrono::Utc
                    .with_ymd_and_hms(2014, 11, 28, 12, 0, 9)
                    .unwrap(),
            )
            .managing_authority(ManagingAuthority::new(
                AuthorityIdentifier::new(AuthorityId::new("1234").unwrap()).with_name("Rotterdam"),
            ))
            .election_identifier(
                ElectionCountElectionIdentifier::builder()
                    .id(ElectionId::new("GR2222_Cyber").unwrap())
                    .category(ElectionCategory::GR)
                    .election_date(NaiveDate::from_ymd_opt(2222, 11, 16).unwrap())
                    .build_for_count()
                    .unwrap(),
            )
            .contests([ElectionCountContest::builder()
                .identifier(ContestIdentifier::geen())
                .total_candidate_votes_count(65u64)
                .total_eligible_voter_count(100u64)
                .total_rejected_votes(RejectedVotesReason::Blank, 100u64)
                .total_rejected_votes(RejectedVotesReason::Invalid, 0u64)
                .total_uncounted_votes(UncountedVotesReason::AdmittedVoters, 10u64)
                .total_votes_selections([
                    ElectionCountSelection::builder()
                        .affiliation(AffiliationSelection::new(
                            AffiliationId::new("1").unwrap(),
                            "Example",
                        ))
                        .valid_votes(16u64)
                        .build()
                        .unwrap(),
                    ElectionCountSelection::builder()
                        .candidate(
                            CandidateSelection::builder()
                                .identifier(CandidateId::new("1").unwrap())
                                .name(PersonName::new("Smid").with_first_name("Example"))
                                .build()
                                .unwrap(),
                        )
                        .valid_votes(16u64)
                        .build()
                        .unwrap(),
                ])
                .reporting_unit_votes([ReportingUnitVotes::builder()
                    .identifier(ReportingUnitIdentifier::new(
                        ReportingUnitIdentifierId::new("SB1234").unwrap(),
                        "Stembureau",
                    ))
                    .rejected_votes(RejectedVotesReason::Blank, 10u64)
                    .rejected_votes(RejectedVotesReason::Invalid, 0u64)
                    .uncounted_votes(UncountedVotesReason::AdmittedVoters, 5u64)
                    .candidate_votes_count(10u64)
                    .eligible_voter_count(100u64)
                    .selections([
                        ElectionCountSelection::builder()
                            .affiliation(AffiliationSelection::new(
                                AffiliationId::new("1").unwrap(),
                                "Example",
                            ))
                            .valid_votes(16u64)
                            .build()
                            .unwrap(),
                        ElectionCountSelection::builder()
                            .candidate(
                                CandidateSelection::builder()
                                    .identifier(CandidateId::new("1").unwrap())
                                    .name(PersonName::new("Smid").with_first_name("Example"))
                                    .build()
                                    .unwrap(),
                            )
                            .valid_votes(16u64)
                            .build()
                            .unwrap(),
                    ])
                    .build()
                    .unwrap()])
                .build()
                .unwrap()])
            .build()
            .unwrap();

        let xml = ec.write_eml_root_str(true, true).unwrap();
        assert_eq!(
            xml,
            include_str!("../../test-emls/election_count/eml510b_construction_output.eml.xml")
        );

        // check if it still is the same after a second parse and write
        let parsed = ElectionCount::parse_eml(&xml, EMLParsingMode::Strict).unwrap();
        let xml2 = parsed.write_eml_root_str(true, true).unwrap();
        assert_eq!(xml, xml2);
    }

    #[test]
    fn test_parse_510b() {
        let xml = include_str!("../../test-emls/election_count/deserialize_eml510b_test.eml.xml");

        assert!(
            ElectionCount::parse_eml(xml, EMLParsingMode::Strict)
                .ok_with_errors()
                .is_ok()
        );
    }

    #[test]
    fn test_parse_510d() {
        let xml = include_str!("../../test-emls/election_count/deserialize_eml510d_test.eml.xml");

        assert!(
            ElectionCount::parse_eml(xml, EMLParsingMode::Strict)
                .ok_with_errors()
                .is_ok()
        );
    }

    #[test]
    fn test_parse_with_investigations() {
        let xml =
            include_str!("../../test-emls/election_count/eml510b_with_investigations.eml.xml");

        assert!(
            ElectionCount::parse_eml(xml, EMLParsingMode::Strict)
                .ok_with_errors()
                .is_ok()
        );
    }
}