use super::key::Somebody;
use super::person::Sex;
use crate::date::GwDate;
macro_rules! event_names {
(
$(#[$meta:meta])*
$name:ident { $($tag:literal => $variant:ident),* $(,)? }
) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum $name {
$(
#[doc = concat!("Written `", $tag, "`.")]
$variant,
)*
Named(String),
}
impl $name {
pub const TAGS: &'static [&'static str] = &[$($tag),*];
#[must_use]
pub fn from_tag(token: &str) -> Option<Self> {
match token {
$($tag => Some(Self::$variant),)*
other => other.strip_prefix('#').map(|t| Self::Named(t.to_owned())),
}
}
#[must_use]
pub fn tag(&self) -> String {
match self {
$(Self::$variant => $tag.to_owned(),)*
Self::Named(name) => format!("#{name}"),
}
}
}
};
}
event_names! {
PersonEventName {
"#birt" => Birth,
"#bapt" => Baptism,
"#deat" => Death,
"#buri" => Burial,
"#crem" => Cremation,
"#acco" => Accomplishment,
"#acqu" => Acquisition,
"#adhe" => Adhesion,
"#awar" => Decoration,
"#bapl" => BaptismLds,
"#barm" => BarMitzvah,
"#basm" => BatMitzvah,
"#bles" => Benediction,
"#cens" => Census,
"#chgn" => ChangeName,
"#circ" => Circumcision,
"#conf" => Confirmation,
"#conl" => ConfirmationLds,
"#degr" => Diploma,
"#demm" => MilitaryDemobilisation,
"#dist" => Distinction,
"#dotl" => DotationLds,
"#educ" => Education,
"#elec" => Election,
"#emig" => Emigration,
"#endl" => Dotation,
"#exco" => Excommunication,
"#fcom" => FirstCommunion,
"#flkl" => FamilyLinkLds,
"#fune" => Funeral,
"#grad" => Graduate,
"#hosp" => Hospitalisation,
"#illn" => Illness,
"#immi" => Immigration,
"#lpas" => PassengerList,
"#mdis" => MilitaryDistinction,
"#mobm" => MilitaryMobilisation,
"#mpro" => MilitaryPromotion,
"#mser" => MilitaryService,
"#natu" => Naturalisation,
"#occu" => Occupation,
"#ordn" => Ordination,
"#prop" => Property,
"#resi" => Residence,
"#reti" => Retired,
"#slgc" => ScellentChildLds,
"#slgp" => ScellentParentLds,
"#slgs" => ScellentSpouseLds,
"#vteb" => PropertySale,
"#will" => Will,
}
}
event_names! {
FamilyEventName {
"#marr" => Marriage,
"#nmar" => NoMarriage,
"#nmen" => NoMention,
"#enga" => Engagement,
"#div" => Divorce,
"#sep" => Separated,
"#anul" => Annulment,
"#marb" => MarriageBann,
"#marc" => MarriageContract,
"#marl" => MarriageLicense,
"#pacs" => Pacs,
"#resi" => Residence,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum WitnessKind {
#[default]
Witness,
GodParent,
CivilOfficer,
ReligiousOfficer,
Informant,
Attending,
Mentioned,
Other,
}
impl WitnessKind {
pub const TAGS: &'static [&'static str] = &[
"#godp", "#offi", "#reli", "#info", "#atte", "#ment", "#othe",
];
#[must_use]
pub fn from_tag(tag: &str) -> Option<Self> {
match tag {
"#godp" => Some(Self::GodParent),
"#offi" => Some(Self::CivilOfficer),
"#reli" => Some(Self::ReligiousOfficer),
"#info" => Some(Self::Informant),
"#atte" => Some(Self::Attending),
"#ment" => Some(Self::Mentioned),
"#othe" => Some(Self::Other),
_ => None,
}
}
#[must_use]
pub fn tag(self) -> Option<&'static str> {
match self {
Self::Witness => None,
Self::GodParent => Some("#godp"),
Self::CivilOfficer => Some("#offi"),
Self::ReligiousOfficer => Some("#reli"),
Self::Informant => Some("#info"),
Self::Attending => Some("#atte"),
Self::Mentioned => Some("#ment"),
Self::Other => Some("#othe"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Witness {
pub person: Somebody,
pub sex: Sex,
pub kind: WitnessKind,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Event<N> {
pub name: N,
pub date: Option<GwDate>,
pub place: String,
pub cause: String,
pub source: String,
pub note: String,
pub witnesses: Vec<Witness>,
}
impl<N> Event<N> {
pub fn new(name: N) -> Self {
Self {
name,
date: None,
place: String::new(),
cause: String::new(),
source: String::new(),
note: String::new(),
witnesses: Vec::new(),
}
}
}
pub type PersonEvent = Event<PersonEventName>;
pub type FamilyEvent = Event<FamilyEventName>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_tags_round_trip() {
for tag in PersonEventName::TAGS {
let name = PersonEventName::from_tag(tag).expect("known tag must parse");
assert_eq!(&name.tag(), tag);
assert!(
!matches!(name, PersonEventName::Named(_)),
"{tag} fell through"
);
}
for tag in FamilyEventName::TAGS {
let name = FamilyEventName::from_tag(tag).expect("known tag must parse");
assert_eq!(&name.tag(), tag);
}
for tag in WitnessKind::TAGS {
assert_eq!(
WitnessKind::from_tag(tag).and_then(WitnessKind::tag),
Some(*tag)
);
}
}
#[test]
fn the_tag_set_is_open() {
assert_eq!(
PersonEventName::from_tag("#nope"),
Some(PersonEventName::Named("nope".into()))
);
assert_eq!(PersonEventName::from_tag("#nope").unwrap().tag(), "#nope");
}
#[test]
fn a_token_without_a_hash_is_not_an_event() {
assert_eq!(PersonEventName::from_tag("birt"), None);
assert_eq!(FamilyEventName::from_tag(""), None);
}
#[test]
fn tag_counts_match_geneweb() {
assert_eq!(PersonEventName::TAGS.len(), 50);
assert_eq!(FamilyEventName::TAGS.len(), 12);
}
}