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
//! Document variant for the EML_NL Result (`520`) document.

use std::{ops::Deref, str::FromStr};

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

pub(crate) const EML_ELECTION_RESULT_ID: &str = "520";

/// Representing a `110a` document, containing an election definition.
#[derive(Debug, Clone)]
pub struct ElectionResult {
    /// Transaction id of the document.
    pub transaction_id: TransactionId,

    /// Managing authority of the election, if present.
    pub managing_authority: ManagingAuthority,

    /// Time this document was created.
    pub creation_date_time: CreationDateTime,

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

    /// The result data.
    pub result: ElectionResultResult,
}

impl ElectionResult {
    /// Builder for creating a new instance.
    pub fn builder() -> ElectionResultBuilder {
        ElectionResultBuilder::new()
    }
}

impl FromStr for ElectionResult {
    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 ElectionResult {
    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<ElectionResult> for String {
    type Error = EMLError;

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

/// Builder for [`ElectionResult`].
#[derive(Debug, Clone)]
pub struct ElectionResultBuilder {
    transaction_id: Option<TransactionId>,
    managing_authority: Option<ManagingAuthority>,
    creation_date_time: Option<CreationDateTime>,
    canonicalization_method: Option<CanonicalizationMethod>,
    result: Option<ElectionResultResult>,
    election_identifier: Option<ElectionResultElectionIdentifier>,
    contests: Vec<ElectionResultContest>,
}

impl ElectionResultBuilder {
    /// Create a new builder for [`ElectionResult`].
    pub fn new() -> Self {
        Self {
            transaction_id: None,
            managing_authority: None,
            creation_date_time: None,
            canonicalization_method: None,
            result: None,
            election_identifier: None,
            contests: vec![],
        }
    }

    /// Set the transaction id of the election result 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 of the election result 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 of the election result 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 used in the election result document.
    pub fn canonicalization_method(
        mut self,
        canonicalization_method: impl Into<CanonicalizationMethod>,
    ) -> Self {
        self.canonicalization_method = Some(canonicalization_method.into());
        self
    }

    /// Set the result data of the election result document.
    ///
    /// You can use this to set the entire result at once, or you can use the
    /// [`Self::election_identifier`], [`Self::contests`] and/or [`Self::push_contest`]
    /// methods to set the election identifier and contests separately and let
    /// this builder build the result data for you.
    pub fn result(mut self, result: impl Into<ElectionResultResult>) -> Self {
        self.result = Some(result.into());
        self
    }

    /// Set the election identifier for the election result document.
    ///
    /// Has no effect if the result is already set using the [`Self::result`] method.
    pub fn election_identifier(
        mut self,
        election_identifier: impl Into<ElectionResultElectionIdentifier>,
    ) -> Self {
        self.election_identifier = Some(election_identifier.into());
        self
    }

    /// Set the contests for the election result document, this overrides any previously set contests.
    ///
    /// Has no effect if the result is already set using the [`Self::result`] method.
    pub fn contests(mut self, contests: impl Into<Vec<ElectionResultContest>>) -> Self {
        self.contests = contests.into();
        self
    }

    /// Add a contest to the election result document.
    ///
    /// Has no effect if the result is already set using the [`Self::result`] method.
    pub fn push_contest(mut self, contest: impl Into<ElectionResultContest>) -> Self {
        self.contests.push(contest.into());
        self
    }

    /// Build the [`ElectionResult`] from the provided data, returning an error if any required fields are missing.
    pub fn build(self) -> Result<ElectionResult, EMLError> {
        Ok(ElectionResult {
            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,
            result: self.result.map_or_else(
                || {
                    let election_identifier = self.election_identifier.ok_or_else(|| {
                        EMLErrorKind::MissingBuildProperty("election_identifier").without_span()
                    })?;

                    if self.contests.is_empty() {
                        return Err(EMLErrorKind::MissingBuildProperty("contests").without_span());
                    }
                    Ok(ElectionResultResult::new(ElectionResultElection::new(
                        election_identifier,
                        self.contests,
                    )))
                },
                Ok,
            )?,
        })
    }
}

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

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

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        // TODO: parse the rest of the document
        accepted_root(elem)?;

        let document_id = elem.attribute_value_req(("Id", None))?;
        if document_id != EML_ELECTION_RESULT_ID {
            return Err(EMLErrorKind::InvalidDocumentType(
                EML_ELECTION_RESULT_ID,
                document_id.to_string(),
            ))
            .with_span(elem.span());
        }

        Ok(collect_struct!(elem, ElectionResult {
            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)?,
            result: ElectionResultResult::EML_NAME => |elem| ElectionResultResult::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .attr(("Id", None), EML_ELECTION_RESULT_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(ElectionResultResult::EML_NAME, &self.result)?
            .finish()
    }
}

/// The result data of an election result document.
#[derive(Debug, Clone)]
pub struct ElectionResultResult {
    /// The election for which the result applies.
    pub election: ElectionResultElection,
}

impl ElectionResultResult {
    /// Create a new result from the given election.
    pub fn new(election: impl Into<ElectionResultElection>) -> Self {
        ElectionResultResult {
            election: election.into(),
        }
    }
}

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

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, ElectionResultResult {
            election: ElectionResultElection::EML_NAME => |elem| ElectionResultElection::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elem(ElectionResultElection::EML_NAME, &self.election)?
            .finish()
    }
}

/// The election for which the result applies.
#[derive(Debug, Clone)]
pub struct ElectionResultElection {
    /// Identifier for the election.
    pub identifier: ElectionResultElectionIdentifier,

    /// Contests within the election.
    pub contests: Vec<ElectionResultContest>,
}

impl ElectionResultElection {
    /// Create a new election result election from the given identifier and contests.
    pub fn new(
        identifier: impl Into<ElectionResultElectionIdentifier>,
        contests: impl Into<Vec<ElectionResultContest>>,
    ) -> Self {
        ElectionResultElection {
            identifier: identifier.into(),
            contests: contests.into(),
        }
    }
}

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

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, ElectionResultElection {
            identifier: ElectionResultElectionIdentifier::EML_NAME => |elem| ElectionResultElectionIdentifier::read_eml(elem)?,
            contests as Vec: ElectionResultContest::EML_NAME => |elem| ElectionResultContest::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elem(ElectionResultElectionIdentifier::EML_NAME, &self.identifier)?
            .child_elems(ElectionResultContest::EML_NAME, &self.contests)?
            .finish()
    }
}

/// Identifier for the election for which the result applies.
#[derive(Debug, Clone)]
pub struct ElectionResultElectionIdentifier {
    /// 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 ElectionResultElectionIdentifier {
    /// Builder for creating a new instance.
    pub fn builder() -> ElectionIdentifierBuilder {
        ElectionIdentifierBuilder::new()
    }
}

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

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(
            elem,
            ElectionResultElectionIdentifier {
                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 an election result.
#[derive(Debug, Clone)]
pub struct ElectionResultContest {
    /// Identifier of the contest.
    pub identifier: ContestIdentifier,

    /// Selections within the contest.
    pub selections: Vec<ElectionResultSelection>,
}

impl ElectionResultContest {
    /// Create a new election result contest from the given identifier and selections.
    pub fn new(
        identifier: impl Into<ContestIdentifier>,
        selections: impl Into<Vec<ElectionResultSelection>>,
    ) -> Self {
        ElectionResultContest {
            identifier: identifier.into(),
            selections: selections.into(),
        }
    }
}

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

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        Ok(collect_struct!(elem, ElectionResultContest {
            identifier: ContestIdentifier::EML_NAME => |elem| ContestIdentifier::read_eml(elem)?,
            selections as Vec: ElectionResultSelection::EML_NAME => |elem| ElectionResultSelection::read_eml(elem)?,
        }))
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        writer
            .child_elem(ContestIdentifier::EML_NAME, &self.identifier)?
            .child_elems(ElectionResultSelection::EML_NAME, &self.selections)?
            .finish()
    }
}

/// The ranking of a selection.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RankingType {
    /// First ranked selection.
    First,
    /// Second ranked selection.
    Second,
}

impl RankingType {
    fn from_str(s: &str) -> Option<Self> {
        match s {
            "1" => Some(RankingType::First),
            "2" => Some(RankingType::Second),
            _ => None,
        }
    }

    fn as_str(&self) -> &'static str {
        match self {
            RankingType::First => "1",
            RankingType::Second => "2",
        }
    }
}

/// Error type for invalid ranking type values.
#[derive(Debug, Clone, thiserror::Error)]
#[error("Invalid ranking type value: {0}")]
pub struct InvalidRankingTypeError(String);

impl StringValueData for RankingType {
    type Error = InvalidRankingTypeError;

    fn parse_from_str(s: &str) -> Result<Self, Self::Error> {
        RankingType::from_str(s).ok_or_else(|| InvalidRankingTypeError(s.to_string()))
    }

    fn to_raw_value(&self) -> String {
        self.as_str().to_string()
    }
}

/// Yes/no type, represented as a boolean.
/// In the EML, this is represented as "yes" or "no".
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct YesNoType(bool);

impl Deref for YesNoType {
    type Target = bool;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl YesNoType {
    /// Create a new YesNoType from a boolean value.
    pub fn new(value: bool) -> Self {
        YesNoType(value)
    }

    /// Create a new YesNoType from an EML string ("yes" or "no").
    pub fn from_eml_value(s: &str) -> Result<Self, InvalidYesNoTypeError> {
        match s {
            "yes" => Ok(YesNoType(true)),
            "no" => Ok(YesNoType(false)),
            _ => Err(InvalidYesNoTypeError(s.to_string())),
        }
    }

    /// Returns the string representation of the value ("yes" or "no").
    pub fn to_eml_value(&self) -> &'static str {
        if self.0 { "yes" } else { "no" }
    }

    /// Returns `true` if the value is "yes".
    pub fn is_yes(&self) -> bool {
        self.0
    }

    /// Returns `true` if the value is "no".
    pub fn is_no(&self) -> bool {
        !self.0
    }
}

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

/// Error type for invalid yes/no type values.
#[derive(Debug, Clone, thiserror::Error)]
#[error("Invalid yes/no type value: {0}")]
pub struct InvalidYesNoTypeError(String);

impl StringValueData for YesNoType {
    type Error = InvalidYesNoTypeError;

    fn parse_from_str(s: &str) -> Result<Self, Self::Error> {
        Self::from_eml_value(s)
    }

    fn to_raw_value(&self) -> String {
        self.to_eml_value().to_string()
    }
}

/// A selection within an election contest.
#[derive(Debug, Clone)]
pub struct ElectionResultSelection {
    /// The type of selection.
    pub selection_type: ElectionResultSelectionType,

    /// Number of votes received.
    pub votes: Option<StringValue<u64>>,

    /// Ranking of the selection, if applicable.
    pub ranking: Option<StringValue<RankingType>>,

    /// Whether the selection was elected.
    pub elected: StringValue<YesNoType>,
}

impl ElectionResultSelection {
    /// Create a builder for creating a new [`ElectionResultSelection`] instance.
    pub fn builder() -> ElectionResultSelectionBuilder {
        ElectionResultSelectionBuilder::new()
    }
}

/// Builder for [`ElectionResultSelection`].
#[derive(Debug, Clone)]
pub struct ElectionResultSelectionBuilder {
    selection_type: Option<ElectionResultSelectionType>,
    votes: Option<StringValue<u64>>,
    ranking: Option<StringValue<RankingType>>,
    elected: Option<StringValue<YesNoType>>,
}

impl ElectionResultSelectionBuilder {
    /// Create a new builder for [`ElectionResultSelection`].
    pub fn new() -> Self {
        Self {
            selection_type: None,
            votes: None,
            ranking: None,
            elected: 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(ElectionResultSelectionType::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(ElectionResultSelectionType::Affiliation(Box::new(
            affiliation.into(),
        )));
        self
    }

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

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

    /// Set whether this selection was elected.
    pub fn elected(mut self, elected: impl Into<YesNoType>) -> Self {
        self.elected = Some(StringValue::from_value(elected.into()));
        self
    }

    /// Build the [`ElectionResultSelection`] from the provided data, returning an error if any required fields are missing.
    pub fn build(self) -> Result<ElectionResultSelection, EMLError> {
        Ok(ElectionResultSelection {
            selection_type: self.selection_type.ok_or_else(|| {
                EMLErrorKind::MissingBuildProperty("selection_type").without_span()
            })?,
            votes: self.votes,
            ranking: self.ranking,
            elected: self
                .elected
                .ok_or_else(|| EMLErrorKind::MissingBuildProperty("elected").without_span())?,
        })
    }
}

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

const VOTES_EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Votes", Some(NS_EML));
const RANKING_EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Ranking", Some(NS_EML));
const ELECTED_EML_NAME: QualifiedName<'_, '_> = QualifiedName::from_static("Elected", Some(NS_EML));

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

    fn read_eml(elem: &mut EMLElementReader<'_, '_>) -> Result<Self, EMLError> {
        let mut selection_type = None;
        let mut votes = None;
        let mut ranking = None;
        let mut elected = None;

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

            match name {
                n if n == CandidateSelection::EML_NAME => {
                    selection_type = Some(ElectionResultSelectionType::Candidate(Box::new(
                        CandidateSelection::read_eml(&mut child)?,
                    )));
                }
                n if n == AffiliationSelection::EML_NAME => {
                    selection_type = Some(ElectionResultSelectionType::Affiliation(Box::new(
                        AffiliationSelection::read_eml(&mut child)?,
                    )));
                }
                n if n == VOTES_EML_NAME => {
                    votes = Some(child.string_value()?);
                }
                n if n == RANKING_EML_NAME => {
                    ranking = Some(child.string_value()?);
                }
                n if n == ELECTED_EML_NAME => {
                    elected = 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()?;
                    }
                }
            }
        }

        let elected = elected.unwrap_or_else(|| StringValue::from_value(YesNoType::new(false)));

        Ok(ElectionResultSelection {
            selection_type: selection_type
                .ok_or_else(|| EMLErrorKind::MissingSelectionType.with_span(elem.inner_span()))?,
            votes,
            ranking,
            elected,
        })
    }

    fn write_eml(&self, writer: EMLElementWriter) -> Result<(), EMLError> {
        let writer = match &self.selection_type {
            ElectionResultSelectionType::Candidate(candidate_selection) => {
                writer.child_elem(CandidateSelection::EML_NAME, candidate_selection.as_ref())?
            }
            ElectionResultSelectionType::Affiliation(affiliation_selection) => writer.child_elem(
                AffiliationSelection::EML_NAME,
                affiliation_selection.as_ref(),
            )?,
        };
        let writer = writer
            .child_option(VOTES_EML_NAME, self.votes.as_ref(), |elem, value| {
                elem.text(value.raw().as_ref())?.finish()
            })?
            .child_option(RANKING_EML_NAME, self.ranking.as_ref(), |elem, value| {
                elem.text(value.raw().as_ref())?.finish()
            })?;

        if self.elected.copied_value().ok() == Some(YesNoType(true)) {
            writer
                .child(ELECTED_EML_NAME, |elem| {
                    elem.text(self.elected.raw().as_ref())?.finish()
                })?
                .finish()
        } else {
            writer.finish()
        }
    }
}

/// The type of selection.
#[derive(Debug, Clone)]
pub enum ElectionResultSelectionType {
    /// Selection of a candidate.
    Candidate(Box<CandidateSelection>),
    /// Selection of an affiliation.
    Affiliation(Box<AffiliationSelection>),
}

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

    /// Name of the candidate.
    pub name: PersonNameStructure,

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

    /// The minimal qualifying address of the candidate, if present.
    pub qualifying_address: MinimalQualifyingAddress,
}

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

/// 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
                .ok_or_else(|| EMLErrorKind::MissingBuildProperty("name").without_span())?,
            gender: self.gender,
            qualifying_address: self.qualifying_address.map_or_else(
                || {
                    let locality_name = self.locality_name.ok_or_else(|| {
                        EMLErrorKind::MissingBuildProperty("locality_name").without_span()
                    })?;

                    if let Some(country_name_code) = self.country_name_code {
                        Ok(MinimalQualifyingAddress::new_country(
                            country_name_code,
                            locality_name,
                        ))
                    } else {
                        Ok(MinimalQualifyingAddress::new_locality(locality_name))
                    }
                },
                Ok,
            )?,
        })
    }
}

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: ("CandidateFullName", NS_EML) => |elem| PersonNameStructure::read_eml_element(elem)?,
            gender as Option: ("Gender", NS_EML) => |elem| elem.string_value()?,
            qualifying_address: 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(("CandidateFullName", NS_EML), |elem| {
                self.name.write_eml_element(elem)
            })?
            .child_option(("Gender", NS_EML), self.gender.as_ref(), |elem, value| {
                elem.text(value.raw().as_ref())?.finish()
            })?
            .child_elem(MinimalQualifyingAddress::EML_NAME, &self.qualifying_address)?
            .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()
    }
}

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

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

    use super::*;

    #[test]
    fn test_election_result_construction() {
        let election_result = ElectionResult::builder()
            .transaction_id(TransactionId::new(1))
            .managing_authority(
                AuthorityIdentifier::new(AuthorityId::new("1234").unwrap()).with_name("Place"),
            )
            .creation_date_time(chrono::Utc.with_ymd_and_hms(2024, 3, 17, 12, 0, 0).unwrap())
            .election_identifier(
                ElectionResultElectionIdentifier::builder()
                    .id(ElectionId::new("GR2024_Place").unwrap())
                    .name("Gemeenteraadsverkiezingen 2024")
                    .category(ElectionCategory::GR)
                    .election_date(XsDate::from_date(2024, 3, 17).unwrap())
                    .build_for_result()
                    .unwrap(),
            )
            .contests([ElectionResultContest::new(
                ContestIdentifier::geen(),
                [
                    ElectionResultSelection::builder()
                        .affiliation(AffiliationSelection::new(
                            AffiliationId::new("1").unwrap(),
                            "Example",
                        ))
                        .elected(true)
                        .build()
                        .unwrap(),
                    ElectionResultSelection::builder()
                        .candidate(
                            CandidateSelection::builder()
                                .identifier(CandidateId::new("1").unwrap())
                                .name(PersonName::new("Smid").with_first_name("Example"))
                                .locality_name("Locality")
                                .country_name_code("NL")
                                .build()
                                .unwrap(),
                        )
                        .elected(true)
                        .votes(100u64)
                        .build()
                        .unwrap(),
                    ElectionResultSelection::builder()
                        .candidate(
                            CandidateSelection::builder()
                                .identifier(CandidateId::new("2").unwrap())
                                .name(PersonName::new("Test").with_first_name("Example"))
                                .locality_name("Locality")
                                .country_name_code("NL")
                                .build()
                                .unwrap(),
                        )
                        .elected(false)
                        .votes(0u64)
                        .build()
                        .unwrap(),
                ],
            )])
            .build()
            .unwrap();
        let xml = election_result.write_eml_root_str(true, true).unwrap();
        assert_eq!(
            xml,
            include_str!("../../test-emls/election_result/eml520_construction_output.eml.xml")
        );

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