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
use std::{fmt, str::FromStr};

use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use yaserde_derive::{YaDeserialize, YaSerialize};

use crate::{
    Attribution, ConfidenceLevel, Date, EnumAsString, GedcomxError, Id, Lang, Note, Qualifier,
    ResourceReference, Result, SourceReference, Uri,
};

/// A name of a person.
///
/// A Name is intended to represent a single variant of a person's name. This
/// means that nicknames, spelling variations, or other names
/// (often distinguishable by a name type) should be modeled with separate
/// instances of Name.
///
/// The name forms of a name contain alternate representations of the name. A
/// Name MUST contain at least one name form, presumably a representation of the
/// name that is considered proper and well formed in the person's native,
/// historical cultural context. Other name forms MAY be included, which can be
/// used to represent this name in contexts where the native name form is not
/// easily recognized and interpreted. Alternate forms are more likely in
/// situations where conclusions are being analyzed across cultural context
/// boundaries that have both language and writing script differences.
///
/// For example, a Korean name has a native Korean form, but can also have a
/// Chinese form and a Roman/Latin form—three different name forms,
/// but each representing the same name.
///
/// If more than one name form is provided, included name forms are presumed to
/// be given in order of preference, with the most preferred name form in the
/// first position in the list.
///
/// # Examples
/// Consider the following: a Russian person with the birth name "Александр"
/// (rendered as "Alexander" in English and in a Latin script) that also went by
/// this name's common nickname, "Саша" (rendered as "Sasha" in English).
///
/// It is tempting to think that this situation should be modeled with one Name
/// instance that has several alternate NameForms. The model is not designed to
/// be used in this way. Instead, this person's names ought to be modeled such
/// that the birth name and the nickname are modeled as two separate Name
/// instances: one instance for the birth name, and one for the nickname. The
/// type property MAY be provided to distinguish the birth name from the
/// nickname. Each Name instance MAY have two NameForm instances: one with the
/// native form of the name and another with the
// alternate form. Using an informal pseudo code, it might look something like the following:
/// ```text
/// Name1.type=http://gedcomx.org/BirthName
/// Name1.nameForms[0].fullText=Александр
/// Name1.nameForms[1].fullText=Alexander
///
/// Name2.type=http://gedcomx.org/Nickname
/// Name2.nameForms[0].fullText=Саша
/// Name2.nameForms[1].fullText=Sasha
/// ```
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, YaSerialize, YaDeserialize, PartialEq, Clone, Default)]
#[yaserde(
    prefix = "gx",
    default_namespace = "gx",
    namespace = "gx: http://gedcomx.org/v1/"
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Name {
    /// An identifier for the conclusion data. The id is to be used as a "fragment identifier" as defined by [RFC 3986, Section 3.5](https://tools.ietf.org/html/rfc3986#section-3.5).
    #[yaserde(attribute)]
    pub id: Option<Id>,

    /// The locale identifier for the conclusion.
    #[yaserde(attribute, prefix = "xml")]
    pub lang: Option<Lang>,

    /// The list of references to the sources of related to this conclusion.
    /// Note that the sources referenced from conclusions are also considered
    /// to be sources of the entities that contain them. For example, a source
    /// associated with the `Name` of a `Person` is also source for the
    /// `Person`.
    #[yaserde(rename = "source", prefix = "gx")]
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub sources: Vec<SourceReference>,

    /// A reference to the analysis document explaining the analysis that went
    /// into this conclusion. If provided, MUST resolve to an instance of
    /// [Document](crate::Document) of type
    /// [Analysis](crate::DocumentType::Analysis).
    #[yaserde(prefix = "gx")]
    pub analysis: Option<ResourceReference>,

    /// A list of notes about this conclusion.
    #[yaserde(rename = "note", prefix = "gx")]
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub notes: Vec<Note>,

    /// The level of confidence the contributor has about the data.
    #[yaserde(attribute)]
    pub confidence: Option<ConfidenceLevel>,

    /// The attribution of this conclusion.
    /// If not provided, the attribution of the containing data set (e.g. file)
    /// of the conclusion is assumed.
    #[yaserde(prefix = "gx")]
    pub attribution: Option<Attribution>,

    /// The name type.
    #[yaserde(rename = "type", attribute)]
    #[serde(rename = "type")]
    pub name_type: Option<NameType>,

    /// The name form(s) that best express this name, usually representations
    /// considered proper and well formed in the person's native, historical
    /// cultural context.
    ///
    /// At least one name form MUST be provided. All included name forms SHOULD
    /// be representations of the same name, and NOT variants of
    /// the name (i.e., not nicknames or spelling variations).
    #[yaserde(rename = "nameForm")]
    pub name_forms: Vec<NameForm>,

    /// The date of applicability of the name.
    pub date: Option<Date>,
}

impl Name {
    pub fn new(
        id: Option<Id>,
        lang: Option<Lang>,
        sources: Vec<SourceReference>,
        analysis: Option<ResourceReference>,
        notes: Vec<Note>,
        confidence: Option<ConfidenceLevel>,
        attribution: Option<Attribution>,
        name_type: Option<NameType>,
        name_forms: Vec<NameForm>,
        date: Option<Date>,
    ) -> Self {
        Self {
            id,
            lang,
            sources,
            analysis,
            notes,
            confidence,
            attribution,
            name_type,
            name_forms,
            date,
        }
    }

    pub fn part_for_type(&self, name_type: &NamePartType) -> Option<&str> {
        self.name_forms.get(0)?.parts.iter().find_map(|n| {
            if n.part_type == Some(name_type.clone()) {
                Some(n.value.as_ref())
            } else {
                None
            }
        })
    }

    pub fn builder(name_form: NameForm) -> NameBuilder {
        NameBuilder::new(name_form)
    }
}

pub struct NameBuilder(Name);

impl NameBuilder {
    conclusion_builder_functions!(Name);

    pub(crate) fn new(name_form: NameForm) -> Self {
        Self(Name {
            name_forms: vec![name_form],
            ..Name::default()
        })
    }

    pub fn name_type(&mut self, name_type: NameType) -> &mut Self {
        self.0.name_type = Some(name_type);
        self
    }

    pub fn name_form(&mut self, name_form: NameForm) -> &mut Self {
        self.0.name_forms.push(name_form);
        self
    }

    pub fn date(&mut self, date: Date) -> &mut Self {
        self.0.date = Some(date);
        self
    }

    pub fn build(&self) -> Name {
        Name::new(
            self.0.id.clone(),
            self.0.lang.clone(),
            self.0.sources.clone(),
            self.0.analysis.clone(),
            self.0.notes.clone(),
            self.0.confidence.clone(),
            self.0.attribution.clone(),
            self.0.name_type.clone(),
            self.0.name_forms.clone(),
            self.0.date.clone(),
        )
    }
}

impl From<&str> for Name {
    fn from(s: &str) -> Self {
        Self {
            name_forms: vec![NameForm {
                full_text: Some(s.to_string()),
                ..NameForm::default()
            }],
            ..Self::default()
        }
    }
}

/// Standard name types.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[non_exhaustive]
#[serde(from = "EnumAsString", into = "EnumAsString")]
pub enum NameType {
    /// Name given at birth.
    BirthName,

    /// Name accepted at marriage.
    MarriedName,

    /// "Also known as" name.
    AlsoKnownAs,

    /// Nickname.
    Nickname,

    /// Name given at adoption.
    AdoptiveName,

    /// A formal name, usually given to distinguish it from a name more commonly
    /// used.
    FormalName,

    /// A name given at a religious rite or ceremony.
    ReligiousName,
    Custom(Uri),
}

impl_enumasstring_yaserialize_yadeserialize!(NameType, "NameType");

impl From<EnumAsString> for NameType {
    fn from(f: EnumAsString) -> Self {
        match f.0.as_ref() {
            "http://gedcomx.org/BirthName" => Self::BirthName,
            "http://gedcomx.org/MarriedName" => Self::MarriedName,
            "http://gedcomx.org/AlsoKnownAs" => Self::AlsoKnownAs,
            "http://gedcomx.org/Nickname" => Self::Nickname,
            "http://gedcomx.org/AdoptiveName" => Self::AdoptiveName,
            "http://gedcomx.org/FormalName" => Self::FormalName,
            "http://gedcomx.org/ReligiousName" => Self::ReligiousName,
            _ => Self::Custom(f.0.into()),
        }
    }
}

impl fmt::Display for NameType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        match self {
            Self::BirthName => write!(f, "http://gedcomx.org/BirthName"),
            Self::MarriedName => write!(f, "http://gedcomx.org/MarriedName"),
            Self::AlsoKnownAs => write!(f, "http://gedcomx.org/AlsoKnownAs"),
            Self::Nickname => write!(f, "http://gedcomx.org/Nickname"),
            Self::AdoptiveName => write!(f, "http://gedcomx.org/AdoptiveName"),
            Self::FormalName => write!(f, "http://gedcomx.org/FormalName"),
            Self::ReligiousName => write!(f, "http://gedcomx.org/ReligiousName"),
            Self::Custom(c) => write!(f, "{}", c),
        }
    }
}

impl Default for NameType {
    fn default() -> Self {
        Self::Custom(Uri::default())
    }
}

/// A representation of a name (a "name form") within a given cultural context,
/// such as a given language and script.
///
/// As names are captured (both in records or in applications), the terms in the
/// name are sometimes classified by type. For example, a certificate of death
/// might prompt for "given name(s)" and "surname". The parts list can be used
/// to represent the terms in the name that have been classified.
///
/// If both a full rendering of the name and a list of parts are provided, it
/// NOT REQUIRED that every term in the fully rendered name appear in the list
/// of parts.
///
/// Name parts in the parts list SHOULD be ordered in the natural order they
/// would be used in the applicable cultural context.
///
/// If a full rendering of the name is not provided (i.e., the name has only
/// been expressed in parts), a full rendering of the name MAY be derived (sans
/// punctuation) by concatenating, in order, each name part value in the list of
/// parts, separating each part with the name part separator appropriate for the
/// applicable cultural context.
///
/// # Examples
/// Consider the following: the Russian name "Пётр Ильи́ч Чайко́вский" in the
/// Cyrillic script, its Latin-script equivalent "Pyotr Ilyich Tchaikovsky", and
/// its anglicised equivalent "Peter Ilyich Tchaikovsky". Using an informal
/// pseudo code, these name forms might be modeled as follows:
/// ```text
/// NameForm1.locale=ru-Cyrl
/// NameForm1.fullText=Пётр Ильи́ч Чайко́вский
/// NameForm1.parts[0].type=http://gedcomx.org/Given
/// NameForm1.parts[0].value=Пётр
/// NameForm1.parts[0].qualifiers[0]=http://gedcomx.org/First
/// NameForm1.parts[1].type=http://gedcomx.org/Middle
/// NameForm1.parts[1].value=Ильи́ч
/// NameForm1.parts[1].qualifiers[0]=http://gedcomx.org/Middle
/// NameForm1.parts[2].type=http://gedcomx.org/Surname
/// NameForm1.parts[2].value=Чайко́вский
///
/// NameForm2.locale=ru-Latn
/// NameForm2.fullText=Pyotr Ilyich Tchaikovsky
/// NameForm2.parts[0].type=http://gedcomx.org/Given
/// NameForm2.parts[0].value=Pyotr
/// NameForm2.parts[0].qualifiers[0]=http://gedcomx.org/First
/// NameForm2.parts[1].type=http://gedcomx.org/Given
/// NameForm2.parts[1].value=Ilyich
/// NameForm2.parts[1].qualifiers[0]=http://gedcomx.org/Middle
/// NameForm2.parts[2].type=http://gedcomx.org/Surname
/// NameForm2.parts[2].value=Tchaikovsky
///
/// NameForm3.locale=en-Latn
/// NameForm3.fullText=Peter Ilyich Tchaikovsky
/// NameForm3.parts[0].type=http://gedcomx.org/Given
/// NameForm3.parts[0].value=Peter
/// NameForm3.parts[0].qualifiers[0]=http://gedcomx.org/First
/// NameForm3.parts[1].type=http://gedcomx.org/Given
/// NameForm3.parts[1].value=Ilyich
/// NameForm3.parts[1].qualifiers[0]=http://gedcomx.org/Middle
/// NameForm3.parts[2].type=http://gedcomx.org/Surname
/// NameForm3.parts[2].value=Tchaikovsky
/// ```
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, YaSerialize, YaDeserialize, PartialEq, Clone, Default)]
#[non_exhaustive]
#[yaserde(
    prefix = "gx",
    default_namespace = "gx",
    namespace = "gx: http://gedcomx.org/v1/"
)]
#[serde(rename_all = "camelCase")]
pub struct NameForm {
    /// The locale identifier for the name form.
    #[yaserde(attribute, prefix = "xml")]
    pub lang: Option<Lang>,

    /// A full rendering of the name (or as much of the name as is known).
    #[yaserde(rename = "fullText", prefix = "gx")]
    pub full_text: Option<String>,

    /// Any identified name parts from the name.
    #[yaserde(rename = "part", prefix = "gx")]
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub parts: Vec<NamePart>,
}

impl NameForm {
    pub fn new(lang: Option<Lang>, full_text: Option<String>, parts: Vec<NamePart>) -> Self {
        Self {
            lang,
            full_text,
            parts,
        }
    }

    pub fn builder() -> NameFormBuilder {
        NameFormBuilder::new()
    }
}

pub struct NameFormBuilder(NameForm);

impl NameFormBuilder {
    pub(crate) fn new() -> Self {
        Self(NameForm::default())
    }

    pub fn full_text<I: Into<String>>(&mut self, full_text: I) -> &mut Self {
        self.0.full_text = Some(full_text.into());
        self
    }

    pub fn parts(&mut self, parts: Vec<NamePart>) -> &mut Self {
        self.0.parts = parts;
        self
    }

    pub fn part(&mut self, part: NamePart) -> &mut Self {
        self.0.parts.push(part);
        self
    }

    pub fn lang<I: Into<Lang>>(&mut self, lang: I) -> &mut Self {
        self.0.lang = Some(lang.into());
        self
    }

    pub fn build(&self) -> NameForm {
        NameForm::new(
            self.0.lang.clone(),
            self.0.full_text.clone(),
            self.0.parts.clone(),
        )
    }
}

/// A portion of a full name, including the terms that make up that portion.
///
/// Some name parts may have qualifiers to provide additional semantic meaning
/// to the name part (e.g., "given name" or "surname").
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, YaSerialize, YaDeserialize, PartialEq, Clone, Default)]
#[yaserde(
    prefix = "gx",
    default_namespace = "gx",
    namespace = "gx: http://gedcomx.org/v1/"
)]
#[non_exhaustive]
pub struct NamePart {
    /// The type of the name part.
    #[yaserde(rename = "type", attribute)]
    #[serde(rename = "type")]
    pub part_type: Option<NamePartType>,

    /// The term(s) from the name that make up this name part.
    ///
    /// A name part value MAY contain more than one term from the full name,
    /// such as in the name part "John Fitzgerald" from the full name "John
    /// Fitzgerald Kennedy". If multiple terms are detailed in a single
    /// NamePart, these terms SHOULD be separated using the name separator
    /// appropriate to the locale applicable to the containing name form.
    #[yaserde(attribute)]
    pub value: String,

    /// Qualifiers to add additional semantic meaning to the name part.
    ///
    /// If present, use of a
    /// [`NamePartQualifier`](crate::NamePartQualifier) is
    /// RECOMMENDED.
    #[yaserde(rename = "qualifier", prefix = "gx")]
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub qualifiers: Vec<Qualifier>,
}

impl NamePart {
    pub fn new(part_type: Option<NamePartType>, value: String, qualifiers: Vec<Qualifier>) -> Self {
        Self {
            part_type,
            value,
            qualifiers,
        }
    }

    pub fn builder<I: Into<String>>(value: I) -> NamePartBuilder {
        NamePartBuilder::new(value)
    }
}

pub struct NamePartBuilder(NamePart);

impl NamePartBuilder {
    pub(crate) fn new<I: Into<String>>(value: I) -> Self {
        Self(NamePart {
            value: value.into(),
            ..NamePart::default()
        })
    }

    pub fn value<I: Into<String>>(&mut self, value: I) -> &mut Self {
        self.0.value = value.into();
        self
    }

    pub fn part_type(&mut self, part_type: NamePartType) -> &mut Self {
        self.0.part_type = Some(part_type);
        self
    }

    pub fn qualifier<I: Into<Qualifier>>(&mut self, qualifier: I) -> &mut Self {
        self.0.qualifiers.push(qualifier.into());
        self
    }

    pub fn build(&self) -> NamePart {
        NamePart::new(
            self.0.part_type.clone(),
            self.0.value.clone(),
            self.0.qualifiers.clone(),
        )
    }
}

/// Standard name part types.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[non_exhaustive]
#[serde(from = "EnumAsString", into = "EnumAsString")]
pub enum NamePartType {
    /// A name prefix.
    Prefix,

    /// A name suffix.
    Suffix,

    /// A given name.
    Given,

    /// A surname.
    Surname,
    Custom(Uri),
}

impl_enumasstring_yaserialize_yadeserialize!(NamePartType, "NamePartType");

impl From<EnumAsString> for NamePartType {
    fn from(f: EnumAsString) -> Self {
        match f.0.as_ref() {
            "http://gedcomx.org/Prefix" => Self::Prefix,
            "http://gedcomx.org/Suffix" => Self::Suffix,
            "http://gedcomx.org/Given" => Self::Given,
            "http://gedcomx.org/Surname" => Self::Surname,
            _ => Self::Custom(f.0.into()),
        }
    }
}

impl fmt::Display for NamePartType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        match self {
            Self::Prefix => write!(f, "http://gedcomx.org/Prefix"),
            Self::Suffix => write!(f, "http://gedcomx.org/Suffix"),
            Self::Given => write!(f, "http://gedcomx.org/Given"),
            Self::Surname => write!(f, "http://gedcomx.org/Surname"),
            Self::Custom(c) => write!(f, "{}", c),
        }
    }
}

impl Default for NamePartType {
    fn default() -> Self {
        Self::Custom(Uri::default())
    }
}

/// Name part qualifiers.
///
/// Identify how the name part was used by the person to which the name applies.
/// For example, a name part qualifier may specify that a given name part was
/// used by the person as a Title.
#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub enum NamePartQualifier {
    /// A designation for honorifics (e.g. Dr., Rev., His Majesty, Haji), ranks
    /// (e.g. Colonel, General, Knight, Esquire), positions (e.g. Count, Chief,
    /// Father, King) or other titles (e.g., PhD, MD).
    Title,

    /// A designation for the name of most prominent in importance among the
    /// names of that type (e.g., the primary given name).
    Primary,

    /// A designation for a name that is not primary in its importance among the
    /// names of that type (e.g., a secondary given name).
    Secondary,

    /// A designation useful for cultures that designate a middle name that is
    /// distinct from a given name and a surname.
    Middle,

    /// A designation for one's familiar name.
    Familiar,

    /// A designation for a name given for religious purposes.
    Religious,

    /// A name that associates a person with a group, such as a clan, tribe, or
    /// patriarchal hierarchy.
    Family,

    /// A designation given by women to their original surname after they adopt
    /// a new surname upon marriage.
    Maiden,

    /// A name derived from a father or paternal ancestor.
    Patronymic,

    /// A name derived from a mother or maternal ancestor.
    Matronymic,

    /// A name derived from associated geography.
    Geographic,

    /// A name derived from one's occupation.
    Occupational,

    /// A name derived from a characteristic.
    Characteristic,

    /// A name mandated by law for populations from Congo Free State / Belgian
    /// Congo / Congo / Democratic Republic of Congo (formerly Zaire).
    Postnom,

    /// A grammatical designation for articles (a, the, dem, las, el, etc.),
    /// prepositions (of, from, aus, zu, op, etc.), initials, annotations (e.g.
    /// twin, wife of, infant, unknown), comparators (e.g. Junior, Senior,
    /// younger, little), ordinals (e.g. III, eighth), descendancy words (e.g.
    /// ben, ibn, bat, bin, bint, bar), and conjunctions (e.g. and, or, nee, ou,
    /// y, o, ne, &).
    Particle,

    /// The "root" of a name part as distinguished from prefixes or suffixes.
    /// For example, the root of the Polish name "Wilkówna" is "Wilk". A
    /// RootName qualifier MUST provide a value property.
    RootName { value: String },
}

impl From<NamePartQualifier> for Qualifier {
    fn from(name_part_qualifier: NamePartQualifier) -> Self {
        match name_part_qualifier {
            NamePartQualifier::RootName { ref value } => {
                Self::new(name_part_qualifier.clone(), Some(value))
            }
            _ => Self::new(name_part_qualifier, None::<String>),
        }
    }
}

impl FromStr for NamePartQualifier {
    type Err = GedcomxError;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "http://gedcomx.org/Title" => Ok(Self::Title),
            "http://gedcomx.org/Primary" => Ok(Self::Primary),
            "http://gedcomx.org/Secondary" => Ok(Self::Secondary),
            "http://gedcomx.org/Middle" => Ok(Self::Middle),
            "http://gedcomx.org/Familiar" => Ok(Self::Familiar),
            "http://gedcomx.org/Religious" => Ok(Self::Religious),
            "http://gedcomx.org/Family" => Ok(Self::Family),
            "http://gedcomx.org/Maiden" => Ok(Self::Maiden),
            "http://gedcomx.org/Patronymic" => Ok(Self::Patronymic),
            "http://gedcomx.org/Matronymic" => Ok(Self::Matronymic),
            "http://gedcomx.org/Geographic" => Ok(Self::Geographic),
            "http://gedcomx.org/Occupational" => Ok(Self::Occupational),
            "http://gedcomx.org/Characteristic" => Ok(Self::Characteristic),
            "http://gedcomx.org/Postnom" => Ok(Self::Postnom),
            "http://gedcomx.org/Particle" => Ok(Self::Particle),
            "http://gedcomx.org/RootName" => Ok(Self::RootName {
                value: String::default(),
            }),
            _ => Err(GedcomxError::QualifierParse {
                parsed_string: s.to_string(),
            }),
        }
    }
}

impl fmt::Display for NamePartQualifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        match self {
            Self::Title => write!(f, "http://gedcomx.org/Title"),
            Self::Primary => write!(f, "http://gedcomx.org/Primary"),
            Self::Secondary => write!(f, "http://gedcomx.org/Secondary"),
            Self::Middle => write!(f, "http://gedcomx.org/Middle"),
            Self::Familiar => write!(f, "http://gedcomx.org/Familiar"),
            Self::Religious => write!(f, "http://gedcomx.org/Religious"),
            Self::Family => write!(f, "http://gedcomx.org/Family"),
            Self::Maiden => write!(f, "http://gedcomx.org/Maiden"),
            Self::Patronymic => write!(f, "http://gedcomx.org/Patronymic"),
            Self::Matronymic => write!(f, "http://gedcomx.org/Matronymic"),
            Self::Geographic => write!(f, "http://gedcomx.org/Geographic"),
            Self::Occupational => write!(f, "http://gedcomx.org/Occupational"),
            Self::Characteristic => write!(f, "http://gedcomx.org/Characteristic"),
            Self::Postnom => write!(f, "http://gedcomx.org/Postnom"),
            Self::Particle => write!(f, "http://gedcomx.org/Particle"),
            Self::RootName { .. } => write!(f, "http://gedcomx.org/RootName"),
        }
    }
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;

    use super::*;
    use crate::TestData;

    #[test]
    fn json_deserialize() {
        let data = TestData::new();

        let json = r#"{              
            "type" : "http://gedcomx.org/BirthName",
            "nameForms" : [ {
                "lang" : "en",
                "fullText" : "full text of the name form",
                "parts" : [ {
                    "type" : "http://gedcomx.org/Surname",
                    "value" : "value of the name part",
                    "qualifiers" : [ { "name" : "http://gedcomx.org/Family" }, { "name" : "http://gedcomx.org/Patronymic" } ]                  
                }]                
            }],
            "date":{"original":"date"},

            "id" : "local_id",
            "lang" : "en",
            "sources" : [ {
                "description" : "SD-1",
                "descriptionId" : "Description id of the target source",
                "attribution" : {
                    "contributor" : {
                    "resource" : "A-1"
                    },
                    "modified" : 1394175600000
                },
                "qualifiers" : [ { "name" : "http://gedcomx.org/RectangleRegion", "value" : "rectangle region value" } ]          
            }],
            "analysis" : {
              "resource" : "http://identifier/for/analysis/document"
            },
            "notes" : [ {
                "lang" : "en",
                "subject" : "subject",
                "text" : "This is a note",
                "attribution" : {
                    "contributor" : {
                    "resource" : "A-1"
                    },
                    "modified" : 1394175600000
                }        
            } ],
            "confidence" : "http://gedcomx.org/High",
            "attribution" : {
                "contributor" : {
                "resource" : "A-1"
                },
                "modified" : 1394175600000
            }  
        }"#;

        let name: Name = serde_json::from_str(json).unwrap();

        assert_eq!(
            name,
            Name {
                id: data.conclusion_data.id,
                lang: data.conclusion_data.lang,
                sources: data.conclusion_data.sources,
                analysis: data.conclusion_data.analysis,
                notes: data.conclusion_data.notes,
                confidence: data.conclusion_data.confidence,
                attribution: data.conclusion_data.attribution,
                name_type: Some(NameType::BirthName),
                date: Some(Date::new(Some("date"), None)),
                name_forms: vec![NameForm {
                    lang: Some("en".into()),
                    full_text: Some("full text of the name form".to_string()),
                    parts: vec![NamePart {
                        part_type: Some(NamePartType::Surname),
                        value: "value of the name part".to_string(),
                        qualifiers: vec![
                            Qualifier {
                                name: NamePartQualifier::Family.into(),
                                value: None
                            },
                            Qualifier {
                                name: NamePartQualifier::Patronymic.into(),
                                value: None
                            }
                        ]
                    }]
                }]
            }
        )
    }

    #[test]
    fn xml_deserialize() {
        let xml = "<Name xmlns=\"http://gedcomx.org/v1/\" type=\"http://gedcomx.org/BirthName\"><nameForm><fullText>Ephraim Howard Kunz</fullText><part type=\"http://gedcomx.org/Given\" value=\"Ephraim\"><qualifier name=\"http://gedcomx.org/Familiar\"></qualifier></part><part type=\"http://gedcomx.org/Given\" value=\"Howard\"><qualifier name=\"http://gedcomx.org/Middle\"></qualifier></part><part type=\"http://gedcomx.org/Surname\" value=\"Kunz\"><qualifier name=\"http://gedcomx.org/RootName\">Test value</qualifier></part></nameForm></Name>";

        let name_form = NameForm::builder()
            .full_text("Ephraim Howard Kunz")
            .part(
                NamePart::builder("Ephraim")
                    .part_type(NamePartType::Given)
                    .qualifier(NamePartQualifier::Familiar)
                    .build(),
            )
            .part(
                NamePart::builder("Howard")
                    .part_type(NamePartType::Given)
                    .qualifier(NamePartQualifier::Middle)
                    .build(),
            )
            .part(
                NamePart::builder("Kunz")
                    .part_type(NamePartType::Surname)
                    .qualifier(NamePartQualifier::RootName {
                        value: "Test value".to_string(),
                    })
                    .build(),
            )
            .build();
        let name = Name::builder(name_form)
            .name_type(NameType::BirthName)
            .build();

        let deser: Name = yaserde::de::from_str(xml).unwrap();

        assert_eq!(deser, name)
    }

    #[test]
    fn json_deserialize_optional_fields() {
        let data = TestData::new();

        let json = r#"{              
            "nameForms" : [ {            
            }],

            "id" : "local_id",
            "lang" : "en",
            "sources" : [ {
                "description" : "SD-1",
                "descriptionId" : "Description id of the target source",
                "attribution" : {
                    "contributor" : {
                    "resource" : "A-1"
                    },
                    "modified" : 1394175600000
                },
                "qualifiers" : [ { "name" : "http://gedcomx.org/RectangleRegion", "value" : "rectangle region value" } ]          
            }],
            "analysis" : {
              "resource" : "http://identifier/for/analysis/document"
            },
            "notes" : [ {
                "lang" : "en",
                "subject" : "subject",
                "text" : "This is a note",
                "attribution" : {
                    "contributor" : {
                    "resource" : "A-1"
                    },
                    "modified" : 1394175600000
                }        
            } ],
            "confidence" : "http://gedcomx.org/High",
            "attribution" : {
                "contributor" : {
                "resource" : "A-1"
                },
                "modified" : 1394175600000
            }  
        }"#;

        let name: Name = serde_json::from_str(json).unwrap();

        assert_eq!(
            name,
            Name {
                id: data.conclusion_data.id,
                lang: data.conclusion_data.lang,
                sources: data.conclusion_data.sources,
                analysis: data.conclusion_data.analysis,
                notes: data.conclusion_data.notes,
                confidence: data.conclusion_data.confidence,
                attribution: data.conclusion_data.attribution,
                name_type: None,
                date: None,
                name_forms: vec![NameForm {
                    lang: None,
                    full_text: None,
                    parts: vec![]
                }]
            }
        )
    }

    #[test]
    fn json_serialize() {
        let data = TestData::new();

        let name = Name {
            id: data.conclusion_data.id,
            lang: data.conclusion_data.lang,
            sources: data.conclusion_data.sources,
            analysis: data.conclusion_data.analysis,
            notes: data.conclusion_data.notes,
            confidence: data.conclusion_data.confidence,
            attribution: data.conclusion_data.attribution,
            name_type: Some(NameType::BirthName),
            date: Some(Date::new(Some("date"), None)),
            name_forms: vec![NameForm {
                lang: Some("en".into()),
                full_text: Some("full text of the name form".to_string()),
                parts: vec![NamePart {
                    part_type: Some(NamePartType::Surname),
                    value: "value of the name part".to_string(),
                    qualifiers: vec![
                        Qualifier {
                            name: NamePartQualifier::Family.into(),
                            value: None,
                        },
                        Qualifier {
                            name: NamePartQualifier::Patronymic.into(),
                            value: None,
                        },
                    ],
                }],
            }],
        };

        let json = serde_json::to_string(&name).unwrap();

        assert_eq!(
            json,
            r#"{"id":"local_id","lang":"en","sources":[{"description":"SD-1","descriptionId":"Description id of the target source","attribution":{"contributor":{"resource":"A-1"},"modified":1394175600000},"qualifiers":[{"name":"http://gedcomx.org/RectangleRegion","value":"rectangle region value"}]}],"analysis":{"resource":"http://identifier/for/analysis/document"},"notes":[{"lang":"en","subject":"subject","text":"This is a note","attribution":{"contributor":{"resource":"A-1"},"modified":1394175600000}}],"confidence":"http://gedcomx.org/High","attribution":{"contributor":{"resource":"A-1"},"modified":1394175600000},"type":"http://gedcomx.org/BirthName","nameForms":[{"lang":"en","fullText":"full text of the name form","parts":[{"type":"http://gedcomx.org/Surname","value":"value of the name part","qualifiers":[{"name":"http://gedcomx.org/Family"},{"name":"http://gedcomx.org/Patronymic"}]}]}],"date":{"original":"date"}}"#
        )
    }

    #[test]
    fn json_serialize_optional_fields() {
        let data = TestData::new();

        let name = Name {
            id: data.conclusion_data.id,
            lang: data.conclusion_data.lang,
            sources: data.conclusion_data.sources,
            analysis: data.conclusion_data.analysis,
            notes: data.conclusion_data.notes,
            confidence: data.conclusion_data.confidence,
            attribution: data.conclusion_data.attribution,
            name_type: None,
            date: None,
            name_forms: vec![NameForm {
                lang: None,
                full_text: None,
                parts: vec![],
            }],
        };

        let json = serde_json::to_string(&name).unwrap();

        assert_eq!(
            json,
            r#"{"id":"local_id","lang":"en","sources":[{"description":"SD-1","descriptionId":"Description id of the target source","attribution":{"contributor":{"resource":"A-1"},"modified":1394175600000},"qualifiers":[{"name":"http://gedcomx.org/RectangleRegion","value":"rectangle region value"}]}],"analysis":{"resource":"http://identifier/for/analysis/document"},"notes":[{"lang":"en","subject":"subject","text":"This is a note","attribution":{"contributor":{"resource":"A-1"},"modified":1394175600000}}],"confidence":"http://gedcomx.org/High","attribution":{"contributor":{"resource":"A-1"},"modified":1394175600000},"nameForms":[{}]}"#
        )
    }

    #[test]
    fn xml_serialize() {
        let name_form = NameForm::builder()
            .full_text("Ephraim Howard Kunz")
            .part(
                NamePart::builder("Ephraim")
                    .part_type(NamePartType::Given)
                    .qualifier(NamePartQualifier::Familiar)
                    .build(),
            )
            .part(
                NamePart::builder("Howard")
                    .part_type(NamePartType::Given)
                    .qualifier(NamePartQualifier::Middle)
                    .build(),
            )
            .part(
                NamePart::builder("Kunz")
                    .part_type(NamePartType::Surname)
                    .qualifier(NamePartQualifier::RootName {
                        value: "Test value".to_string(),
                    })
                    .build(),
            )
            .build();
        let name = Name::builder(name_form)
            .name_type(NameType::BirthName)
            .build();

        let config = yaserde::ser::Config {
            write_document_declaration: false,
            ..yaserde::ser::Config::default()
        };
        let xml = yaserde::ser::to_string_with_config(&name, &config).unwrap();

        assert_eq!(
            xml,
            "<Name xmlns=\"http://gedcomx.org/v1/\" type=\"http://gedcomx.org/BirthName\"><nameForm><fullText>Ephraim Howard Kunz</fullText><part type=\"http://gedcomx.org/Given\" value=\"Ephraim\"><qualifier name=\"http://gedcomx.org/Familiar\"></qualifier></part><part type=\"http://gedcomx.org/Given\" value=\"Howard\"><qualifier name=\"http://gedcomx.org/Middle\"></qualifier></part><part type=\"http://gedcomx.org/Surname\" value=\"Kunz\"><qualifier name=\"http://gedcomx.org/RootName\">Test value</qualifier></part></nameForm></Name>"
        )
    }

    #[test]
    fn name_part_qualifier_to_qualifier() {
        {
            let qualifier: Qualifier = NamePartQualifier::Family.into();
            assert_eq!(
                qualifier,
                Qualifier::new(NamePartQualifier::Family.to_string(), None::<String>)
            );
        }

        {
            let npq = NamePartQualifier::RootName {
                value: "Kunz".to_string(),
            };
            let qualifier: Qualifier = npq.clone().into();
            assert_eq!(
                qualifier,
                Qualifier::new(npq.to_string(), Some("Kunz".to_string()))
            );
        }
    }
}