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
use std::collections::{HashMap, HashSet, VecDeque};
use thiserror::Error;
use crate::common::{ElectionTree, Region, RegionKey};
use crate::{EMLError, EMLErrorKind};
/// Error returned when the regions of an election tree do not describe a valid
/// tree, or when an operation on an [`ElectionTreeHierarchy`] cannot be
/// performed.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
pub enum ElectionTreeHierarchyError {
/// None of the regions is a root region, so the tree has no starting point.
#[error("Election tree has no root region")]
NoRoot,
/// More than one region is a root region, while an election tree must have
/// exactly one.
#[error("Election tree has more than one root region: {0:?}")]
MultipleRoots(Vec<RegionKey>),
/// Two or more regions share the same category and number.
#[error("Election tree has more than one region with identity {0:?}")]
DuplicateRegion(RegionKey),
/// A region refers to a superior region that does not exist.
#[error("Region {region:?} refers to unknown superior region {superior:?}")]
UnknownSuperiorRegion {
/// The region containing the unresolvable reference.
region: RegionKey,
/// The superior region that was referred to.
superior: RegionKey,
},
/// A region refers to a superior region that does not sit at a higher level
/// in the election tree, while every region must be subordinate to a region
/// of a higher category than its own.
#[error(
"Region {region:?} refers to superior region {superior:?}, which does not sit at a higher level in the election tree"
)]
InvalidSuperiorRegionCategory {
/// The region containing the invalid reference.
region: RegionKey,
/// The superior region that was referred to.
superior: RegionKey,
},
/// A region sits below a region it does not refer to as its superior region,
/// while the position of a region within the election tree and the superior
/// region it refers to have to agree.
#[error(
"Region {region:?} sits below region {superior:?}, but does not refer to it as its superior region"
)]
InconsistentSuperiorRegion {
/// The region referring to the wrong superior region.
region: RegionKey,
/// The region it sits below.
superior: RegionKey,
},
/// The election tree does not contain a region with the given identity.
#[error("Election tree has no region with identity {0:?}")]
UnknownRegion(RegionKey),
/// The root region was removed, while an election tree must always have
/// exactly one root region.
#[error("The root region of an election tree cannot be removed")]
CannotRemoveRoot,
}
impl From<ElectionTreeHierarchyError> for EMLError {
fn from(err: ElectionTreeHierarchyError) -> Self {
EMLErrorKind::InvalidElectionTree(err).without_span()
}
}
/// A region within an [`ElectionTreeHierarchy`], together with the regions
/// directly subordinate to it.
///
/// The superior region key of the contained [`Region`] should match the position
/// of the region within the tree: it is set when the region is attached to a
/// superior region by [`RegionNode::add_child`], and cleared for the root region
/// of the tree.
#[derive(Debug, Clone)]
pub struct RegionNode {
region: Region,
children: Vec<RegionNode>,
}
impl RegionNode {
/// Create a new region node for a region and its subordinate regions.
pub fn new(region: Region, children: impl Into<Vec<RegionNode>>) -> Self {
RegionNode {
region,
children: children.into(),
}
}
/// The region itself, including its committees.
pub fn region(&self) -> &Region {
&self.region
}
/// The region itself, allowing it to be modified.
///
/// The key and the superior region key of the region are part of the
/// structure of an election tree, so keeping them pointing at the regions
/// around this one is up to the caller. Both are checked again when this
/// region node enters an election tree hierarchy.
pub fn region_mut(&mut self) -> &mut Region {
&mut self.region
}
/// The regions directly subordinate to this region, in document order.
pub fn children(&self) -> &[RegionNode] {
&self.children
}
/// The key identifying this region.
pub fn key(&self) -> RegionKey {
self.region.key
}
/// This region and all regions below it, breadth-first, starting with this
/// region itself.
pub fn iter(&self) -> impl Iterator<Item = &RegionNode> {
// The queue holds the regions that have been reached but not yielded
// yet, so only the region to start from has to be queued up front: every
// region queues the regions below it as it is yielded.
let mut queue = VecDeque::from([self]);
std::iter::from_fn(move || {
let node = queue.pop_front()?;
queue.extend(node.children.iter());
Some(node)
})
}
/// Find the region with the given key, either this region itself or one of
/// the regions below it.
pub fn find(&self, key: RegionKey) -> Option<&RegionNode> {
self.iter().find(|node| node.key() == key)
}
/// Find the region with the given key, allowing it to be modified.
pub fn find_mut(&mut self, key: RegionKey) -> Option<&mut RegionNode> {
let mut stack = vec![self];
while let Some(node) = stack.pop() {
if node.key() == key {
return Some(node);
}
stack.extend(node.children.iter_mut());
}
None
}
/// Detach the region with the given key from the regions below this region,
/// returning it together with all regions below it.
pub fn take_descendant(&mut self, key: RegionKey) -> Option<RegionNode> {
let mut stack = vec![self];
while let Some(node) = stack.pop() {
let position = node.children.iter().position(|child| child.key() == key);
if let Some(position) = position {
return Some(node.children.remove(position));
}
stack.extend(node.children.iter_mut());
}
None
}
/// Add a region below this region, setting the superior region key of the
/// added region accordingly.
///
/// The region being added must be of a lower region category than this
/// region. The regions below the added region are left as they are, so it is
/// only once the region enters an election tree that they are checked.
pub fn add_child(
&mut self,
mut child: RegionNode,
) -> Result<RegionKey, ElectionTreeHierarchyError> {
let child_key = child.key();
let parent_key = self.key();
if !parent_key.category.is_higher_level_than(child_key.category) {
return Err(ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: child_key,
superior: parent_key,
});
}
child.region.superior_region_key = Some(parent_key);
self.children.push(child);
Ok(child_key)
}
/// Verify that this region and the regions below it describe a valid part of
/// an election tree: every region below this one refers to the region it sits
/// below as its superior region and is of a lower region category, and no
/// region appears more than once.
///
/// The superior region key of this region itself is not checked, since where
/// this region sits is not known here.
fn check_subtree(
&self,
keys: &mut HashSet<RegionKey>,
) -> Result<(), ElectionTreeHierarchyError> {
let mut stack = vec![self];
while let Some(node) = stack.pop() {
if !keys.insert(node.key()) {
return Err(ElectionTreeHierarchyError::DuplicateRegion(node.key()));
}
let superior = node.key();
for child in &node.children {
let key = child.key();
if !superior.category.is_higher_level_than(key.category) {
return Err(ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: key,
superior,
});
}
if child.region.superior_region_key != Some(superior) {
return Err(ElectionTreeHierarchyError::InconsistentSuperiorRegion {
region: key,
superior,
});
}
}
stack.extend(node.children.iter());
}
Ok(())
}
}
impl From<Region> for RegionNode {
fn from(region: Region) -> Self {
RegionNode::new(region, vec![])
}
}
impl std::ops::Deref for RegionNode {
type Target = Region;
fn deref(&self) -> &Self::Target {
&self.region
}
}
/// The election tree of an EML_NL election definition in a structured form.
///
/// EML_NL encodes the election tree as a flat list of regions, where every
/// region refers to its superior region by category and number. This type
/// exposes that same data as an actual tree with a single root region, and can
/// be converted back into the flat [`ElectionTree`] representation.
///
/// Every region below the root region is of a lower
/// [`RegionCategory`](crate::utils::RegionCategory) than its superior region,
/// which is what keeps the flat list of regions from describing anything other
/// than a tree.
///
/// Use [`ElectionTree::hierarchy`] or [`TryFrom`] to build a tree from an
/// [`ElectionTree`], and [`ElectionTreeHierarchy::flattened`] or [`From`] to
/// convert a tree back into an [`ElectionTree`].
#[derive(Debug, Clone)]
pub struct ElectionTreeHierarchy {
root: RegionNode,
}
impl ElectionTreeHierarchy {
/// Create an election tree consisting of a single root region.
///
/// Any superior region key on the given region is cleared, since the root
/// region of an election tree has no superior region. The root
/// region does not have to be a `STAAT`, it can be any region without a
/// superior region, such as the `GEMEENTE` of a municipal election.
pub fn new(root: Region) -> Self {
let mut root = RegionNode::from(root);
root.region.superior_region_key = None;
ElectionTreeHierarchy { root }
}
/// Create an election tree from a region and the regions below it, checking
/// that the tree invariants are satisfied.
pub fn from_region_node(
node: impl Into<RegionNode>,
) -> Result<Self, ElectionTreeHierarchyError> {
let node: RegionNode = node.into();
node.try_into()
}
/// The root region of this election tree.
pub fn root(&self) -> &RegionNode {
&self.root
}
/// The total number of regions in this election tree.
pub fn region_count(&self) -> usize {
self.iter().count()
}
/// All regions in this election tree, breadth-first. The root region is
/// yielded first.
pub fn iter(&self) -> impl Iterator<Item = &RegionNode> {
self.root.iter()
}
/// Find the region with the given key.
pub fn get(&self, key: RegionKey) -> Option<&RegionNode> {
self.root.find(key)
}
/// Whether this election tree contains a region with the given key.
pub fn contains(&self, key: RegionKey) -> bool {
self.get(key).is_some()
}
/// Attach a region below the region identified by `parent`, setting the
/// superior region key of the inserted region accordingly.
///
/// The region being inserted must be of a lower region category than the
/// region it is attached to, since a region is always subordinate to a
/// region at a higher level of the election tree.
///
/// The regions below the region being inserted are inserted along with it,
/// and have to describe a valid part of an election tree themselves: each of
/// them refers to the region it sits below as its superior region and is of a
/// lower region category, and no region ends up in the tree twice.
///
/// Returns the key identifying the inserted region.
pub fn insert(
&mut self,
parent: RegionKey,
region: impl Into<RegionNode>,
) -> Result<RegionKey, ElectionTreeHierarchyError> {
let node = region.into();
let key = node.key();
if !parent.category.is_higher_level_than(key.category) {
return Err(ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: key,
superior: parent,
});
}
self.check_can_insert(&node)?;
let Some(parent_node) = self.root.find_mut(parent) else {
return Err(ElectionTreeHierarchyError::UnknownRegion(parent));
};
parent_node.add_child(node)
}
/// Detach the region identified by `key` together with all regions below
/// it, and return it.
///
/// The root region cannot be removed, since an election tree always has
/// exactly one root region.
pub fn remove(&mut self, key: RegionKey) -> Result<RegionNode, ElectionTreeHierarchyError> {
if key == self.root.key() {
return Err(ElectionTreeHierarchyError::CannotRemoveRoot);
}
self.root
.take_descendant(key)
.ok_or(ElectionTreeHierarchyError::UnknownRegion(key))
}
/// Verify that the given region and the regions below it can be added to this
/// election tree without breaking its invariants.
fn check_can_insert(&self, node: &RegionNode) -> Result<(), ElectionTreeHierarchyError> {
node.check_subtree(&mut self.iter().map(|node| node.key()).collect())
}
/// This election tree in the flat form EML_NL uses, with every region
/// referring to its superior region by category and number.
///
/// The regions are listed breadth-first.
pub fn flattened(&self) -> ElectionTree {
ElectionTree::from(self)
}
/// Consume the tree and retrieve the root node, allowing manipulation
/// on the node level.
pub fn into_inner(self) -> RegionNode {
self.root
}
/// Build a structured election tree from the flat list of regions of an
/// [`ElectionTree`].
///
/// The checks performed while indexing the regions guarantee that the result
/// is a tree covering every region: keys are unique, exactly one region has
/// no superior region, every other region refers to a superior region that
/// exists and that sits at a strictly higher level.
fn from_regions(regions: &[Region]) -> Result<Self, ElectionTreeHierarchyError> {
let (root, index) = index_regions(regions)?;
let tree = ElectionTreeHierarchy {
root: assemble(&index, root),
};
Ok(tree)
}
}
impl TryFrom<RegionNode> for ElectionTreeHierarchy {
type Error = ElectionTreeHierarchyError;
/// Build an election tree around the given region and the regions below it,
/// checking the tree invariants for the entire tree.
fn try_from(mut root: RegionNode) -> Result<Self, Self::Error> {
root.check_subtree(&mut HashSet::new())?;
root.region.superior_region_key = None;
Ok(ElectionTreeHierarchy { root })
}
}
impl TryFrom<&ElectionTree> for ElectionTreeHierarchy {
type Error = ElectionTreeHierarchyError;
fn try_from(tree: &ElectionTree) -> Result<Self, Self::Error> {
ElectionTreeHierarchy::from_regions(&tree.regions)
}
}
impl TryFrom<ElectionTree> for ElectionTreeHierarchy {
type Error = ElectionTreeHierarchyError;
fn try_from(tree: ElectionTree) -> Result<Self, Self::Error> {
ElectionTreeHierarchy::from_regions(&tree.regions)
}
}
impl From<&ElectionTreeHierarchy> for ElectionTree {
fn from(tree: &ElectionTreeHierarchy) -> Self {
let regions: Vec<Region> = tree.iter().map(|node| node.region.clone()).collect();
ElectionTree::new(regions)
}
}
impl From<ElectionTreeHierarchy> for ElectionTree {
fn from(tree: ElectionTreeHierarchy) -> Self {
let mut regions = Vec::new();
let mut queue = VecDeque::from([tree.root]);
while let Some(node) = queue.pop_front() {
regions.push(node.region);
queue.extend(node.children);
}
ElectionTree::new(regions)
}
}
/// The regions of an election tree by key, each together with the keys of the
/// regions directly subordinate to it in document order.
type RegionIndex<'a> = HashMap<RegionKey, (&'a Region, Vec<RegionKey>)>;
/// Index the given regions by their key, resolving the superior region of every
/// region, and return the key of the root region along with the index.
fn index_regions(
regions: &[Region],
) -> Result<(RegionKey, RegionIndex<'_>), ElectionTreeHierarchyError> {
let mut index: RegionIndex<'_> = HashMap::with_capacity(regions.len());
let mut roots = Vec::new();
for region in regions {
if index.insert(region.key, (region, Vec::new())).is_some() {
return Err(ElectionTreeHierarchyError::DuplicateRegion(region.key));
}
}
for region in regions {
let Some(superior) = region.superior_region_key else {
roots.push(region.key);
continue;
};
if !superior.category.is_higher_level_than(region.key.category) {
return Err(ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: region.key,
superior,
});
}
let Some((_, subordinates)) = index.get_mut(&superior) else {
return Err(ElectionTreeHierarchyError::UnknownSuperiorRegion {
region: region.key,
superior,
});
};
subordinates.push(region.key);
}
match roots.as_slice() {
[root] => Ok((*root, index)),
[] => Err(ElectionTreeHierarchyError::NoRoot),
_ => Err(ElectionTreeHierarchyError::MultipleRoots(roots)),
}
}
/// Build the node for the region with the given key, together with the nodes of
/// all regions below it.
///
/// This recurses once per level of the election tree. Every region sits below a
/// region of a higher category, which [`index_regions`] has already verified, so
/// the levels of a tree are limited to the number of region categories that
/// exist, however many regions are being assembled.
fn assemble(index: &RegionIndex<'_>, key: RegionKey) -> RegionNode {
let (region, subordinates) = &index[&key];
RegionNode::new(
(*region).clone(),
subordinates
.iter()
.map(|&child| assemble(index, child))
.collect::<Vec<_>>(),
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::documents::election_definition::ElectionDefinition;
use crate::io::{EMLParsingMode, EMLRead};
use crate::utils::{CommitteeCategory, RegionCategory};
/// The number of regions in the TK2025 election definition: one `STAAT`,
/// twenty `KIESKRING` regions and 346 `GEMEENTE` regions.
const TK2025_REGION_COUNT: usize = 367;
/// The `GEMEENTE` regions of the TK2025 election definition that allow
/// exporting in Frysian, in document order. These are exactly the
/// municipalities of the Leeuwarden electoral district.
const TK2025_FRYSIAN_MUNICIPALITIES: [u16; 18] = [
59, 60, 72, 74, 80, 85, 86, 88, 90, 93, 96, 98, 737, 1891, 1900, 1940, 1949, 1970,
];
/// The election tree of the TK2025 election definition, in its flat EML_NL
/// form.
fn tk2025_election_tree() -> ElectionTree {
let xml = include_str!(
"../../test-files/election_definition/Verkiezingsdefinitie_TK2025.eml.xml"
);
let definition = ElectionDefinition::parse_eml(xml, EMLParsingMode::Strict).unwrap();
definition.election_event.election.election_tree
}
/// The election tree of the TK2025 election definition, in its structured
/// form.
fn tk2025_hierarchy() -> ElectionTreeHierarchy {
tk2025_election_tree().hierarchy().unwrap()
}
#[test]
fn test_tk2025_root_region() {
let tree = tk2025_hierarchy();
let root = tree.root();
assert_eq!(root.key(), RegionKey::state());
assert_eq!(root.region().name.as_ref(), "Nederland");
assert_eq!(root.region().superior_region_key, None);
assert!(root.region().committees.is_empty());
assert_eq!(tree.region_count(), TK2025_REGION_COUNT);
}
#[test]
fn test_tk2025_electoral_districts_are_below_the_state() {
let tree = tk2025_hierarchy();
// All twenty electoral districts are directly below the root region, in
// document order, and each refers back to it.
let districts: Vec<RegionKey> = tree
.root()
.children()
.iter()
.map(|node| node.key())
.collect();
let expected: Vec<RegionKey> = (1..=20).map(RegionKey::electoral_district).collect();
assert_eq!(districts, expected);
for node in tree.root().children() {
assert_eq!(node.region().superior_region_key, Some(RegionKey::state()));
}
let names: Vec<&str> = tree
.root()
.children()
.iter()
.map(|node| node.region().name.as_ref())
.collect();
assert_eq!(names[0], "Groningen");
assert_eq!(names[1], "Leeuwarden");
assert_eq!(names[11], "'s-Gravenhage");
assert_eq!(names[19], "Bonaire");
}
#[test]
fn test_tk2025_municipalities_are_below_an_electoral_district() {
let tree = tk2025_hierarchy();
// Every municipality is a leaf region below the electoral district it
// refers to as its superior region.
let mut municipalities = 0;
for node in tree.iter() {
if node.region().key.category != RegionCategory::Municipality {
continue;
}
municipalities += 1;
assert!(node.children().is_empty(), "{:?}", node.key());
assert_eq!(
node.region().superior_region_key.map(|key| key.category),
Some(RegionCategory::ElectoralDistrict),
"{:?}",
node.key()
);
}
assert_eq!(municipalities, 346);
// All of them sit two levels below the root region, directly below one of
// the electoral districts.
let below_the_districts = tree
.root()
.children()
.iter()
.flat_map(|district| district.children())
.count();
assert_eq!(below_the_districts, 346);
// Spot check a few districts: Amsterdam and Rotterdam consist of a
// single municipality, while Bonaire covers the three Caribbean ones.
let amsterdam = tree.get(RegionKey::electoral_district(9)).unwrap();
assert_eq!(amsterdam.children().len(), 1);
assert_eq!(amsterdam.children()[0].key(), RegionKey::municipality(363));
assert_eq!(
tree.get(RegionKey::electoral_district(13))
.unwrap()
.children()
.len(),
1
);
let bonaire = tree.get(RegionKey::electoral_district(20)).unwrap();
let caribbean: Vec<RegionKey> = bonaire.children().iter().map(|node| node.key()).collect();
assert_eq!(
caribbean,
vec![
RegionKey::municipality(9001),
RegionKey::municipality(9002),
RegionKey::municipality(9003)
]
);
}
/// The number of regions at every level of the tree below the given region,
/// starting with the region itself.
fn regions_per_level(root: &RegionNode) -> Vec<usize> {
let mut level = vec![root];
let mut counts = Vec::new();
while !level.is_empty() {
counts.push(level.len());
level = level.iter().flat_map(|node| node.children()).collect();
}
counts
}
#[test]
fn test_tk2025_tree_is_three_levels_deep() {
let tree = tk2025_hierarchy();
assert_eq!(regions_per_level(tree.root()), vec![1, 20, 346]);
}
#[test]
fn test_tk2025_breadth_first_order_matches_document_order() {
let election_tree = tk2025_election_tree();
let tree = ElectionTreeHierarchy::try_from(&election_tree).unwrap();
// The TK2025 definition lists its regions level by level, which is the
// order the tree yields them in as well.
let document_order: Vec<RegionKey> = election_tree
.regions
.iter()
.map(|region| region.key)
.collect();
let tree_order: Vec<RegionKey> = tree.iter().map(|node| node.key()).collect();
assert_eq!(tree_order, document_order);
}
#[test]
fn test_tk2025_region_lookup() {
let tree = tk2025_hierarchy();
let leeuwarden = tree.get(RegionKey::municipality(80)).unwrap();
assert_eq!(leeuwarden.region().name.as_ref(), "Leeuwarden");
assert_eq!(
leeuwarden.region().superior_region_key,
Some(RegionKey::electoral_district(2))
);
assert!(leeuwarden.region().frysian_export_allowed);
assert!(!leeuwarden.region().roman_numerals);
assert!(tree.contains(RegionKey::state()));
assert!(tree.contains(RegionKey::electoral_district(20)));
// A municipality number that is not part of the tree, and existing
// numbers in the wrong category.
assert!(tree.get(RegionKey::municipality(1)).is_none());
assert!(!tree.contains(RegionKey::municipality(1)));
assert!(!tree.contains(RegionKey::new(RegionCategory::Province, Some(2))));
assert!(!tree.contains(RegionKey::new(RegionCategory::ElectoralDistrict, None)));
}
#[test]
fn test_tk2025_committees_are_preserved() {
let tree = tk2025_hierarchy();
// 's-Gravenhage holds the central electoral committee alongside its own
// main electoral committee.
let the_hague = tree
.get(RegionKey::electoral_district(12))
.unwrap()
.region();
assert_eq!(the_hague.committees.len(), 2);
assert_eq!(the_hague.committees[0].category, CommitteeCategory::CSB);
assert_eq!(the_hague.committees[0].name.as_deref(), Some("De Kiesraad"));
assert_eq!(the_hague.committees[1].category, CommitteeCategory::HSB);
assert_eq!(
the_hague.committees[1].accept_central_submissions,
Some(true)
);
}
#[test]
fn test_tk2025_frysian_regions_are_the_leeuwarden_district() {
let tree = tk2025_hierarchy();
let frysian: Vec<RegionKey> = tree
.iter()
.filter(|node| node.region().frysian_export_allowed)
.map(|node| node.key())
.collect();
let expected: Vec<RegionKey> = TK2025_FRYSIAN_MUNICIPALITIES
.iter()
.copied()
.map(RegionKey::municipality)
.collect();
assert_eq!(frysian, expected);
// The Leeuwarden electoral district itself does not allow Frysian
// exports, but all of the municipalities below it do.
let leeuwarden = tree.get(RegionKey::electoral_district(2)).unwrap();
assert!(!leeuwarden.region().frysian_export_allowed);
let below: Vec<RegionKey> = leeuwarden
.children()
.iter()
.map(|node| node.key())
.collect();
assert_eq!(below, expected);
}
#[test]
fn test_tk2025_node_iteration() {
let tree = tk2025_hierarchy();
let leeuwarden = tree.get(RegionKey::electoral_district(2)).unwrap();
// Iterating a node starts at that node itself, followed by the regions
// below it.
let iterated: Vec<RegionKey> = leeuwarden.iter().map(|node| node.key()).collect();
assert_eq!(iterated.len(), 19);
assert_eq!(iterated[0], RegionKey::electoral_district(2));
assert_eq!(iterated[1..].len(), leeuwarden.children().len());
// Finding is relative to the node it starts from.
assert_eq!(
leeuwarden
.find(RegionKey::municipality(80))
.map(|node| node.key()),
Some(RegionKey::municipality(80))
);
assert_eq!(
leeuwarden
.find(RegionKey::electoral_district(2))
.map(|node| node.key()),
Some(RegionKey::electoral_district(2))
);
assert!(leeuwarden.find(RegionKey::municipality(14)).is_none());
assert!(leeuwarden.find(RegionKey::state()).is_none());
}
#[test]
fn test_tk2025_hierarchy_from_owned_election_tree() {
let tree = ElectionTreeHierarchy::try_from(tk2025_election_tree()).unwrap();
assert_eq!(tree.root().key(), RegionKey::state());
assert_eq!(tree.region_count(), TK2025_REGION_COUNT);
}
#[test]
fn test_tk2025_flattening_round_trips() {
let election_tree = tk2025_election_tree();
let tree = election_tree.hierarchy().unwrap();
// The TK2025 definition lists its regions breadth-first, which is the
// order flattening produces, so the round trip is exact.
let flattened = tree.flattened();
let keys: Vec<RegionKey> = flattened.regions.iter().map(|region| region.key).collect();
let expected: Vec<RegionKey> = election_tree
.regions
.iter()
.map(|region| region.key)
.collect();
assert_eq!(keys, expected);
// Flattening borrows, so the tree is still usable afterwards, and the
// owned conversion produces the same regions.
assert_eq!(tree.region_count(), TK2025_REGION_COUNT);
let owned: Vec<RegionKey> = ElectionTree::from(tree)
.regions
.iter()
.map(|region| region.key)
.collect();
assert_eq!(owned, expected);
// Committees and the attributes of a region survive the round trip.
let the_hague = flattened
.regions
.iter()
.find(|region| region.key == RegionKey::electoral_district(12))
.unwrap();
assert_eq!(the_hague.committees.len(), 2);
assert_eq!(the_hague.superior_region_key, Some(RegionKey::state()));
assert_eq!(the_hague.name.as_ref(), "'s-Gravenhage");
}
#[test]
fn test_hierarchy_error_converts_into_an_eml_error() {
// Every chain of superior regions strictly rises in category and so has
// to end at a region without a superior, which makes an election tree
// with no regions at all the only way to end up without a root.
let error = tree_error(Vec::new());
assert_eq!(error, ElectionTreeHierarchyError::NoRoot);
let eml_error = EMLError::from(error);
assert!(matches!(
eml_error.kind(),
EMLErrorKind::InvalidElectionTree(ElectionTreeHierarchyError::NoRoot)
));
assert_eq!(eml_error.span(), None);
// The conversion is also available through `?` on an EMLError result.
fn hierarchy(tree: &ElectionTree) -> Result<ElectionTreeHierarchy, EMLError> {
Ok(tree.hierarchy()?)
}
let regions = vec![
region(RegionKey::state(), None),
region(RegionKey::municipality(14), Some(RegionKey::state())),
];
assert!(hierarchy(&ElectionTree::new(regions)).is_ok());
let duplicated = vec![
region(RegionKey::state(), None),
region(RegionKey::state(), None),
];
let eml_error = hierarchy(&ElectionTree::new(duplicated)).unwrap_err();
assert!(matches!(
eml_error.kind(),
EMLErrorKind::InvalidElectionTree(ElectionTreeHierarchyError::DuplicateRegion(key))
if *key == RegionKey::state()
));
}
/// A region with the given key, subordinate to the region with the given
/// superior key, if any.
fn region(key: RegionKey, superior: Option<RegionKey>) -> Region {
let region = Region::new("Region", key.category);
let region = match key.number {
Some(number) => region.with_number(number),
None => region,
};
match superior {
Some(superior) => region.with_superior_region_key(superior),
None => region,
}
}
/// The error returned when the given regions do not describe a valid region
/// tree.
fn tree_error(regions: Vec<Region>) -> ElectionTreeHierarchyError {
ElectionTreeHierarchy::try_from(&ElectionTree::new(regions)).unwrap_err()
}
#[test]
fn test_superior_region_must_be_of_a_higher_category() {
// A municipality below an electoral district is fine, the other way
// around is not.
let regions = vec![
region(RegionKey::electoral_district(1), None),
region(
RegionKey::municipality(14),
Some(RegionKey::electoral_district(1)),
),
];
let tree = ElectionTreeHierarchy::try_from(&ElectionTree::new(regions)).unwrap();
assert_eq!(tree.region_count(), 2);
let regions = vec![
region(RegionKey::municipality(14), None),
region(
RegionKey::electoral_district(1),
Some(RegionKey::municipality(14)),
),
];
assert_eq!(
tree_error(regions),
ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::electoral_district(1),
superior: RegionKey::municipality(14),
}
);
}
#[test]
fn test_superior_region_of_the_same_category_is_rejected() {
let regions = vec![
region(RegionKey::state(), None),
region(RegionKey::municipality(14), Some(RegionKey::state())),
region(
RegionKey::municipality(80),
Some(RegionKey::municipality(14)),
),
];
assert_eq!(
tree_error(regions),
ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::municipality(80),
superior: RegionKey::municipality(14),
}
);
}
#[test]
fn test_regions_referring_to_each_other_cannot_form_a_cycle() {
// Every cycle contains at least one region whose superior region does not
// sit at a higher level, so a cycle is always rejected on the category of
// that superior region. Here that is the electoral district, which refers
// downwards to a municipality.
let regions = vec![
region(
RegionKey::electoral_district(1),
Some(RegionKey::municipality(14)),
),
region(
RegionKey::municipality(14),
Some(RegionKey::electoral_district(1)),
),
];
assert_eq!(
tree_error(regions),
ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::electoral_district(1),
superior: RegionKey::municipality(14),
}
);
// Two regions of the same category referring to each other are rejected
// for the same reason.
let regions = vec![
region(
RegionKey::municipality(14),
Some(RegionKey::municipality(80)),
),
region(
RegionKey::municipality(80),
Some(RegionKey::municipality(14)),
),
];
assert_eq!(
tree_error(regions),
ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::municipality(14),
superior: RegionKey::municipality(80),
}
);
}
#[test]
fn test_unknown_superior_region_is_reported() {
// The superior region sits at a higher level, but does not exist.
let regions = vec![
region(RegionKey::state(), None),
region(
RegionKey::municipality(14),
Some(RegionKey::electoral_district(1)),
),
];
assert_eq!(
tree_error(regions),
ElectionTreeHierarchyError::UnknownSuperiorRegion {
region: RegionKey::municipality(14),
superior: RegionKey::electoral_district(1),
}
);
// Comparing the categories is the cheaper of the two checks, so a
// superior region that sits at a lower level and does not exist either is
// reported on its category.
let regions = vec![
region(RegionKey::state(), None),
region(
RegionKey::electoral_district(1),
Some(RegionKey::municipality(14)),
),
];
assert_eq!(
tree_error(regions),
ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::electoral_district(1),
superior: RegionKey::municipality(14),
}
);
}
#[test]
fn test_insert_requires_a_superior_region_of_a_higher_category() {
let mut tree = ElectionTreeHierarchy::new(region(RegionKey::electoral_district(1), None));
let key = tree
.insert(
RegionKey::electoral_district(1),
region(RegionKey::municipality(14), None),
)
.unwrap();
assert_eq!(key, RegionKey::municipality(14));
assert_eq!(
tree.get(key).unwrap().region().superior_region_key,
Some(RegionKey::electoral_district(1))
);
// Neither a region of the same category nor one of a higher category can
// be attached below the municipality.
assert_eq!(
tree.insert(
RegionKey::municipality(14),
region(RegionKey::municipality(80), None)
),
Err(ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::municipality(80),
superior: RegionKey::municipality(14),
})
);
assert_eq!(
tree.insert(
RegionKey::municipality(14),
region(RegionKey::state(), None)
),
Err(ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::state(),
superior: RegionKey::municipality(14),
})
);
// A polling station sits below the municipality, and the failed inserts
// left the tree untouched.
tree.insert(
RegionKey::municipality(14),
region(RegionKey::polling_station(1), None),
)
.unwrap();
assert_eq!(tree.region_count(), 3);
}
#[test]
fn test_insert_reports_an_unknown_parent_region() {
let mut tree = ElectionTreeHierarchy::new(region(RegionKey::electoral_district(1), None));
// The region being inserted is of a lower category than the region it is
// attached to, so it is the missing parent region that is reported.
assert_eq!(
tree.insert(
RegionKey::electoral_district(2),
region(RegionKey::municipality(14), None)
),
Err(ElectionTreeHierarchyError::UnknownRegion(
RegionKey::electoral_district(2)
))
);
}
#[test]
fn test_insert_checks_the_regions_below_the_inserted_region() {
let mut tree = ElectionTreeHierarchy::new(region(RegionKey::state(), None));
let district = RegionKey::electoral_district(1);
let node = |superior| {
RegionNode::new(
region(district, None),
[RegionNode::from(region(
RegionKey::municipality(14),
superior,
))],
)
};
// The municipality below the district being inserted has to refer to that
// district as its superior region.
assert_eq!(
tree.insert(RegionKey::state(), node(None)),
Err(ElectionTreeHierarchyError::InconsistentSuperiorRegion {
region: RegionKey::municipality(14),
superior: district,
})
);
assert_eq!(tree.region_count(), 1);
// Once it does, both regions are inserted.
tree.insert(RegionKey::state(), node(Some(district)))
.unwrap();
assert_eq!(tree.region_count(), 3);
// A region already in the tree cannot be inserted again, wherever below
// the inserted region it appears.
let duplicated = RegionNode::new(
region(RegionKey::electoral_district(2), None),
[RegionNode::from(region(
RegionKey::municipality(14),
Some(RegionKey::electoral_district(2)),
))],
);
assert_eq!(
tree.insert(RegionKey::state(), duplicated),
Err(ElectionTreeHierarchyError::DuplicateRegion(
RegionKey::municipality(14)
))
);
assert_eq!(tree.region_count(), 3);
}
#[test]
fn test_hierarchy_from_a_region_node() {
let mut tk2025 = tk2025_hierarchy();
let leeuwarden = tk2025.remove(RegionKey::electoral_district(2)).unwrap();
// A detached region becomes the root region of an election tree of its
// own, which clears the superior region it used to refer to.
let district = ElectionTreeHierarchy::try_from(leeuwarden).unwrap();
assert_eq!(district.root().key(), RegionKey::electoral_district(2));
assert_eq!(district.root().region().superior_region_key, None);
assert_eq!(district.region_count(), 19);
// The root region of a tree can be taken out and put back unchanged.
let mut root = ElectionTreeHierarchy::try_from(tk2025_hierarchy().into_inner())
.unwrap()
.into_inner();
assert_eq!(root.iter().count(), TK2025_REGION_COUNT);
// Manipulating a region below the root region into disagreeing about
// where it sits is rejected.
root.find_mut(RegionKey::municipality(80))
.unwrap()
.region_mut()
.superior_region_key = Some(RegionKey::state());
assert_eq!(
ElectionTreeHierarchy::try_from(root).unwrap_err(),
ElectionTreeHierarchyError::InconsistentSuperiorRegion {
region: RegionKey::municipality(80),
superior: RegionKey::electoral_district(2),
}
);
}
#[test]
fn test_hierarchy_from_a_region_node_checks_region_categories() {
// The electoral district below the municipality does not sit at a lower
// level of the election tree, so these regions do not form a tree.
let inverted = RegionNode::new(
region(RegionKey::municipality(14), None),
[RegionNode::from(region(
RegionKey::electoral_district(1),
Some(RegionKey::municipality(14)),
))],
);
assert_eq!(
ElectionTreeHierarchy::try_from(inverted).unwrap_err(),
ElectionTreeHierarchyError::InvalidSuperiorRegionCategory {
region: RegionKey::electoral_district(1),
superior: RegionKey::municipality(14),
}
);
}
}