pub mod date;
pub mod event;
use std::fmt::Write as _;
use oxidged::model::ChildLink;
use oxidged::model::{
Adoption, Association, Citation, CitationSource, Dataset, EnumList, Event, EventFamily,
EventKind, Family, File, Header, HeaderSource, Individual, IndividualRef, MultimediaLink, Name,
NamePiece, NamePieceKind, NameType, Node, Note, Pedigree, Phrased, Place, Record, Restriction,
Sex as GedSex, SpouseLink, Store, TagId, Text, ThinVec, Value, XrefId,
};
use oxidged::GedcomVersion;
use crate::database::{FamilyId, FamilyRecord, GwDatabase, PersonId, ResolvedWitness};
use crate::model::event::{Event as GwEvent, FamilyEventName as F, WitnessKind};
use crate::model::family::{Divorce, RelationKind};
use crate::model::person::{Access, Burial, Death, DeathReason, Person, Sex};
use crate::model::relation::RelationType;
use crate::model::{FamilyEvent, PersonEvent, Title, TitleName};
pub const TAG_OCCURRENCE: &str = "_GWOCC";
pub const TAG_ACCESS: &str = "_GWACCESS";
pub const TAG_IMAGE: &str = "_GWIMAGE";
pub const TAG_DEATH_REASON: &str = "_GWDEATH";
pub const TAG_RELATION_KIND: &str = "_GWRELKIND";
pub const TAG_EVENT: &str = "_GWTAG";
pub const TAG_PAGE: &str = "_GWPAGE";
pub const TAG_WIZARD: &str = "_GWWIZARD";
struct Ids {
persons: Vec<XrefId>,
families: Vec<XrefId>,
occurrence: TagId,
access: TagId,
image: TagId,
death_reason: TagId,
relation_kind: TagId,
event: TagId,
page: TagId,
wizard: TagId,
note: TagId,
}
fn intern(store: &mut Store, xref: &str) -> XrefId {
store
.intern_xref(xref)
.expect("a store holds four billion identifiers")
}
impl Ids {
fn new(store: &mut Store, persons: usize, families: usize) -> Self {
Self {
persons: (1..=persons)
.map(|n| intern(store, &format!("@I{n}@")))
.collect(),
families: (1..=families)
.map(|n| intern(store, &format!("@F{n}@")))
.collect(),
occurrence: store.intern_tag(TAG_OCCURRENCE),
access: store.intern_tag(TAG_ACCESS),
image: store.intern_tag(TAG_IMAGE),
death_reason: store.intern_tag(TAG_DEATH_REASON),
relation_kind: store.intern_tag(TAG_RELATION_KIND),
event: store.intern_tag(TAG_EVENT),
page: store.intern_tag(TAG_PAGE),
wizard: store.intern_tag(TAG_WIZARD),
note: store.intern_tag("NOTE"),
}
}
fn person(&self, id: PersonId) -> XrefId {
self.persons[id]
}
fn family(&self, id: FamilyId) -> XrefId {
self.families[id]
}
}
fn custom(tag: TagId, value: &str) -> Node {
Node {
payload: Value::Text(Text::from(value)),
..Node::new(tag)
}
}
fn notes<C: FromIterator<Note>>(text: &str) -> C {
(!text.is_empty())
.then(|| Note::text(text))
.into_iter()
.collect()
}
fn place(name: &str) -> Option<Place> {
(!name.is_empty()).then(|| Place {
name: Text::from(name),
..Place::default()
})
}
fn citations<C: FromIterator<Citation>>(text: &str) -> C {
(!text.is_empty())
.then(|| Citation {
source: CitationSource::Description(Text::from(text)),
..Citation::default()
})
.into_iter()
.collect()
}
fn set_notes(event: &mut Event, text: &str) {
if !text.is_empty() {
event.detail_mut().notes = notes(text);
}
}
fn name_record(value: String, given: &str, surname: &str, name_type: Option<NameType>) -> Name {
let mut name = Name::new(value);
for (kind, piece) in [
(NamePieceKind::Given, given),
(NamePieceKind::Surname, surname),
] {
if !piece.is_empty() {
name.pieces.push(NamePiece {
kind,
value: Text::from(piece),
});
}
}
if let Some(kind) = name_type {
name.detail_mut().kind = Some(Phrased::new(kind));
}
name
}
fn add_nickname(name: &mut Name, nickname: &str) {
let at = name
.pieces
.iter()
.position(|p| p.kind == NamePieceKind::Surname)
.unwrap_or(name.pieces.len());
let mut pieces = std::mem::take(&mut name.pieces).into_vec();
pieces.insert(
at,
NamePiece {
kind: NamePieceKind::Nickname,
value: Text::from(nickname),
},
);
name.pieces = ThinVec::from(pieces);
}
fn gedcom_name(given: &str, surname: &str) -> String {
format!("{given} /{surname}/")
}
fn access_label(access: Access) -> Option<&'static str> {
match access {
Access::IfTitles => None,
Access::Public => Some("public"),
Access::Private => Some("private"),
Access::SemiPublic => Some("semipublic"),
}
}
fn death_reason_label(reason: DeathReason) -> Option<&'static str> {
match reason {
DeathReason::Unspecified => None,
DeathReason::Killed => Some("killed"),
DeathReason::Murdered => Some("murdered"),
DeathReason::Executed => Some("executed"),
DeathReason::Disappeared => Some("disappeared"),
}
}
fn relation_kind_event(kind: RelationKind) -> event::EventMapping {
let standard = |e: EventKind| event::EventMapping {
event: e,
event_type: None,
};
let generic = |label: &str| event::EventMapping {
event: EventKind::Event,
event_type: Some(label.to_owned()),
};
match kind {
RelationKind::Married | RelationKind::NoSexesCheckMarried => standard(EventKind::Marriage),
RelationKind::Engaged => standard(EventKind::Engagement),
RelationKind::MarriageBann => standard(EventKind::MarriageBann),
RelationKind::MarriageContract => standard(EventKind::MarriageContract),
RelationKind::MarriageLicense => standard(EventKind::MarriageLicense),
RelationKind::Residence => standard(EventKind::Residence),
RelationKind::NotMarried | RelationKind::NoSexesCheckNotMarried => generic("unmarried"),
RelationKind::NoMention => generic("nomen"),
RelationKind::Pacs => generic("pacs"),
}
}
fn relation_kind_label(kind: RelationKind) -> Option<&'static str> {
match kind {
RelationKind::Married => None,
RelationKind::NotMarried => Some("not married"),
RelationKind::Engaged => Some("engaged"),
RelationKind::NoSexesCheckNotMarried => Some("not married, sexes unchecked"),
RelationKind::NoSexesCheckMarried => Some("married, sexes unchecked"),
RelationKind::NoMention => Some("no mention"),
RelationKind::MarriageBann => Some("marriage bann"),
RelationKind::MarriageContract => Some("marriage contract"),
RelationKind::MarriageLicense => Some("marriage licence"),
RelationKind::Pacs => Some("pacs"),
RelationKind::Residence => Some("residence"),
}
}
struct ImpliedFamily<'a> {
child: PersonId,
father: Option<PersonId>,
mother: Option<PersonId>,
pedigree: Pedigree,
sources: &'a str,
}
fn adoption_event(family: XrefId, implied: &ImpliedFamily<'_>) -> Event {
let adopted_by = match (implied.father, implied.mother) {
(Some(_), Some(_)) => Adoption::Both,
(Some(_), None) => Adoption::Husband,
_ => Adoption::Wife,
};
let mut d = Event::new(EventKind::Adoption);
d.detail_mut().family = Some(EventFamily {
family: Some(family),
adopted_by: Some(Phrased::new(adopted_by)),
..EventFamily::default()
});
d
}
fn relation_type_label(relation: RelationType) -> &'static str {
match relation {
RelationType::Adoption => "adoptive parent",
RelationType::Recognition => "recognising parent",
RelationType::CandidateParent => "candidate parent",
RelationType::GodParent => "godparent",
RelationType::FosterParent => "foster parent",
}
}
fn title_value(title: &Title) -> String {
let mut out = title.ident.clone();
if !title.place.is_empty() {
let _ = write!(out, ", {}", title.place);
}
if title.nth != 0 {
let _ = write!(out, ", {}", title.nth);
}
out
}
fn title_period(title: &Title) -> Option<oxidged::model::Date> {
let bound =
|d: &Option<crate::date::GwDate>| d.as_ref().map(date::value).filter(|v| !v.is_empty());
let value = match (bound(&title.date_start), bound(&title.date_end)) {
(None, None) => return None,
(Some(start), None) => format!("FROM {start}"),
(None, Some(end)) => format!("TO {end}"),
(Some(start), Some(end)) => format!("FROM {start} TO {end}"),
};
Some(oxidged::model::Date::new(value))
}
fn title_holder<'a>(title: &'a Title, person: &'a Person) -> &'a str {
match &title.name {
TitleName::Main => &person.public_name,
TitleName::Name(name) => name,
TitleName::None => "",
}
}
impl GwDatabase {
#[must_use]
pub fn to_gedcom(&self) -> Dataset {
let implied = self.implied_families();
let mut data = Dataset::new(GedcomVersion::V5_5_1);
let ids = Ids::new(
data.store_mut(),
self.persons.len(),
self.families.len() + implied.len(),
);
data.set_header(Some(header()));
let mut spouse_of: Vec<Vec<SpouseLink>> = vec![Vec::new(); self.persons.len()];
let mut child_of: Vec<Vec<ChildLink>> = vec![Vec::new(); self.persons.len()];
for (id, family) in self.families.iter().enumerate() {
let xref = ids.family(id);
for parent in [family.father, family.mother] {
spouse_of[parent].push(SpouseLink::new(xref));
}
for &child in &family.children {
child_of[child].push(ChildLink::new(xref));
}
}
for (i, family) in implied.iter().enumerate() {
let xref = ids.family(self.families.len() + i);
for parent in [family.father, family.mother].into_iter().flatten() {
spouse_of[parent].push(SpouseLink::new(xref));
}
let mut link = ChildLink::new(xref);
link.detail_mut().pedigree = Some(Phrased::new(family.pedigree.clone()));
child_of[family.child].push(link);
}
let mut adoptions: Vec<Vec<Event>> = vec![Vec::new(); self.persons.len()];
for (i, family) in implied.iter().enumerate() {
if family.pedigree == Pedigree::Adopted {
let xref = ids.family(self.families.len() + i);
adoptions[family.child].push(adoption_event(xref, family));
}
}
for (id, person) in self.persons.iter().enumerate() {
let mut individual = self.individual(&ids, id, person);
individual.spouse_of = std::mem::take(&mut spouse_of[id]).into();
individual.child_of = std::mem::take(&mut child_of[id]).into();
individual.events.append(&mut adoptions[id]);
data.push(Record::Individual(Box::new(individual)));
}
for (id, family) in self.families.iter().enumerate() {
data.push(Record::Family(Box::new(self.family(&ids, id, family))));
}
for (i, family) in implied.iter().enumerate() {
let xref = ids.family(self.families.len() + i);
let partner = |who: Option<PersonId>| who.map(|p| IndividualRef::new(ids.person(p)));
data.push(Record::Family(Box::new(Family {
xref: Some(xref),
husband: partner(family.father),
wife: partner(family.mother),
children: vec![IndividualRef::new(ids.person(family.child))],
citations: citations(family.sources),
..Family::default()
})));
}
for page in &self.pages {
data.push(Record::Other(Box::new(page_tag(&ids, ids.page, page))));
}
for wizard in &self.wizard_notes {
data.push(Record::Other(Box::new(page_tag(&ids, ids.wizard, wizard))));
}
data
}
fn individual(&self, ids: &Ids, id: PersonId, person: &Person) -> Individual {
let mut individual = Individual {
xref: Some(ids.person(id)),
names: names(person),
sex: gender(person.sex),
notes: notes(&person.notes),
citations: citations(&person.sources),
..Individual::default()
};
individual.events = self.life_events(ids, id, person);
individual.events.extend(attributes(person));
let associations = self.associations(ids, person);
if !associations.is_empty() {
individual.detail_mut().associations = associations;
}
if !person.image.is_empty() {
individual
.detail_mut()
.multimedia
.push(multimedia(&person.image));
}
if person.occ != 0 {
individual
.extra
.push(custom(ids.occurrence, &person.occ.to_string()));
}
if let Some(label) = access_label(person.access) {
individual.extra.push(custom(ids.access, label));
}
if matches!(person.access, Access::Private | Access::SemiPublic) {
individual.detail_mut().restriction = Some(EnumList(vec![Restriction::Confidential]));
}
if !person.image.is_empty() {
individual.extra.push(custom(ids.image, &person.image));
}
individual
}
fn life_events(&self, ids: &Ids, id: PersonId, person: &Person) -> Vec<Event> {
use crate::model::event::PersonEventName as P;
let mut events = Vec::new();
let superseded = |kind: &P| person.events.iter().any(|e| &e.name == kind);
let birth_superseded = superseded(&P::Birth);
let baptism_superseded = superseded(&P::Baptism);
let death_superseded = superseded(&P::Death);
let burial_superseded = superseded(&P::Burial) || superseded(&P::Cremation);
if !birth_superseded
&& (person.birth.is_some()
|| !person.birth_place.is_empty()
|| !person.birth_src.is_empty()
|| !person.birth_note.is_empty())
{
let mut d = Event::new(EventKind::Birth);
d.date = person.birth.as_ref().map(date::to_gedcom);
d.place = place(&person.birth_place);
set_notes(&mut d, &person.birth_note);
d.citations = citations(&person.birth_src);
events.push(d);
}
if !baptism_superseded
&& (person.baptism.is_some()
|| !person.baptism_place.is_empty()
|| !person.baptism_src.is_empty()
|| !person.baptism_note.is_empty())
{
let mut d = Event::new(EventKind::Baptism);
d.date = person.baptism.as_ref().map(date::to_gedcom);
d.place = place(&person.baptism_place);
set_notes(&mut d, &person.baptism_note);
d.citations = citations(&person.baptism_src);
events.push(d);
}
if let Some(mut d) = death_event(ids, &person.death).filter(|_| !death_superseded) {
d.place = place(&person.death_place);
set_notes(&mut d, &person.death_note);
d.citations = citations(&person.death_src);
events.push(d);
}
let burial = match &person.burial {
_ if burial_superseded => None,
Burial::Unknown => None,
Burial::Buried(date) => Some((EventKind::Burial, date)),
Burial::Cremated(date) => Some((EventKind::Cremation, date)),
};
if let Some((kind, when)) = burial {
let mut d = Event::new(kind);
d.date = when.as_ref().map(date::to_gedcom);
d.place = place(&person.burial_place);
set_notes(&mut d, &person.burial_note);
d.citations = citations(&person.burial_src);
events.push(d);
}
for (i, gw_event) in person.events.iter().enumerate() {
let mapping = event::person_event(&gw_event.name);
let witnesses = self.person_event_witnesses(id, i);
events.push(Self::event_detail(
ids,
gw_event,
mapping,
&gw_event.name.tag(),
witnesses,
));
}
if death_superseded {
if let Some(reason) = death_reason(&person.death) {
if let Some(d) = events.iter_mut().find(|d| d.kind == EventKind::Death) {
d.extra.push(custom(ids.death_reason, reason));
}
}
}
events
}
fn event_detail<N>(
ids: &Ids,
gw_event: &GwEvent<N>,
mapping: event::EventMapping,
original_tag: &str,
witnesses: &[ResolvedWitness],
) -> Event {
let mut d = Event::new(mapping.event);
d.date = gw_event.date.as_ref().map(date::to_gedcom);
d.place = place(&gw_event.place);
d.citations = citations(&gw_event.source);
set_notes(&mut d, &gw_event.note);
let associations = witness_associations(ids, witnesses);
if !associations.is_empty() {
d.detail_mut().associations = associations;
}
if !gw_event.cause.is_empty() {
d.detail_mut().cause = Some(Text::from(gw_event.cause.as_str()));
}
if let Some(label) = mapping.event_type {
d.detail_mut().classification = Some(Text::from(label));
d.extra.push(custom(ids.event, original_tag));
}
d
}
fn associations(&self, ids: &Ids, person: &Person) -> Vec<Association> {
let mut out = Vec::new();
for relation in &person.relations {
if matches!(
relation.relation_type,
RelationType::Adoption | RelationType::FosterParent
) {
continue;
}
for (who, slot) in [(relation.father.as_ref(), 0), (relation.mother.as_ref(), 1)] {
let Some(who) = who else { continue };
let label = match (relation.relation_type, slot) {
(RelationType::GodParent, 0) => "GODF",
(RelationType::GodParent, _) => "GODM",
(other, _) => relation_type_label(other),
};
if let Some(id) = self.lookup(&who.key()) {
out.push(association(ids.person(id), label));
}
}
}
out
}
fn implied_families(&self) -> Vec<ImpliedFamily<'_>> {
let mut out = Vec::new();
for (child, person) in self.persons.iter().enumerate() {
for relation in &person.relations {
let pedigree = match relation.relation_type {
RelationType::Adoption => Pedigree::Adopted,
RelationType::FosterParent => Pedigree::Foster,
_ => continue,
};
let resolve = |who: &Option<crate::model::key::Somebody>| {
who.as_ref().and_then(|w| self.lookup(&w.key()))
};
let (father, mother) = (resolve(&relation.father), resolve(&relation.mother));
if father.is_some() || mother.is_some() {
out.push(ImpliedFamily {
child,
father,
mother,
pedigree,
sources: &relation.sources,
});
}
}
}
out
}
fn family(&self, ids: &Ids, id: FamilyId, family: &FamilyRecord) -> Family {
let mut out = Family {
xref: Some(ids.family(id)),
husband: Some(IndividualRef::new(ids.person(family.father))),
wife: Some(IndividualRef::new(ids.person(family.mother))),
children: family
.children
.iter()
.map(|&child| IndividualRef::new(ids.person(child)))
.collect(),
citations: citations(&family.sources),
notes: notes(&family.comment),
..Family::default()
};
let has_event = |kind: &F| family.events.iter().any(|e| &e.name == kind);
let marriage_superseded = has_event(&F::Marriage);
let divorce_superseded = has_event(&F::Divorce);
let separation_superseded = has_event(&F::Separated);
let union = relation_kind_event(family.relation);
if !marriage_superseded
&& (family.marriage.is_some()
|| !family.marriage_place.is_empty()
|| !family.marriage_src.is_empty()
|| !family.marriage_note.is_empty()
|| family.relation != RelationKind::Married
|| !family.witnesses.is_empty())
{
let mut d = Event::new(union.event);
if let Some(label) = union.event_type {
d.detail_mut().classification = Some(Text::from(label));
}
d.date = family.marriage.as_ref().map(date::to_gedcom);
d.place = place(&family.marriage_place);
set_notes(&mut d, &family.marriage_note);
d.citations = citations(&family.marriage_src);
let witnesses = witness_associations(ids, &family.witnesses);
if !witnesses.is_empty() {
d.detail_mut().associations = witnesses;
}
out.events.push(d);
}
let ending = match &family.divorce {
Divorce::Divorced(when) if !divorce_superseded => {
Some((event::family_event(&F::Divorce), when))
}
Divorce::Separated(when) if !separation_superseded => {
Some((event::family_event(&F::Separated), when))
}
_ => None,
};
if let Some((mapping, when)) = ending {
let mut d = Event::new(mapping.event);
if let Some(label) = mapping.event_type {
d.detail_mut().classification = Some(Text::from(label));
}
d.date = when.as_ref().map(date::to_gedcom);
out.events.push(d);
}
for (i, gw_event) in family.events.iter().enumerate() {
let mapping = event::family_event(&gw_event.name);
let witnesses = self.family_event_witnesses(id, i);
out.events.push(Self::event_detail(
ids,
gw_event,
mapping,
&gw_event.name.tag(),
witnesses,
));
}
if marriage_superseded {
if let Some(marriage) = out
.events
.iter_mut()
.find(|d| d.kind == EventKind::Marriage)
{
for witness in witness_associations(ids, &family.witnesses) {
let known = marriage
.detail()
.associations
.iter()
.any(|a| a.individual == witness.individual);
if !known {
marriage.detail_mut().associations.push(witness);
}
}
}
}
if let Some(label) = relation_kind_label(family.relation) {
out.extra.push(custom(ids.relation_kind, label));
}
out
}
}
fn header() -> Header {
Header {
source: Some(HeaderSource {
product: Text::from("GENEWEB"),
name: Some(Text::from(concat!("geneweb ", env!("CARGO_PKG_VERSION")))),
..HeaderSource::default()
}),
..Header::default()
}
}
fn names(person: &Person) -> Vec<Name> {
let mut names = vec![name_record(
gedcom_name(&person.first_name, &person.surname),
&person.first_name,
&person.surname,
None,
)];
let mut nicknames = person.qualifiers.iter();
if let Some(nickname) = nicknames.next() {
add_nickname(&mut names[0], nickname);
}
for nickname in nicknames {
let mut name = name_record(
gedcom_name(&person.first_name, &person.surname),
&person.first_name,
&person.surname,
Some(NameType::Aka),
);
add_nickname(&mut name, nickname);
names.push(name);
}
if !person.public_name.is_empty() {
names.push(name_record(
gedcom_name(&person.public_name, &person.surname),
&person.public_name,
&person.surname,
Some(NameType::Aka),
));
}
for given in &person.first_names_aliases {
names.push(name_record(
gedcom_name(given, &person.surname),
given,
&person.surname,
Some(NameType::Aka),
));
}
for surname in &person.surnames_aliases {
names.push(name_record(
gedcom_name(&person.first_name, surname),
&person.first_name,
surname,
Some(NameType::Aka),
));
}
for alias in &person.aliases {
names.push(name_record(alias.clone(), "", "", Some(NameType::Aka)));
}
names
}
fn association(individual: XrefId, relation: &str) -> Association {
Association {
individual: Some(individual),
relation: Some(Text::from(relation)),
..Association::default()
}
}
fn witness_associations(ids: &Ids, witnesses: &[ResolvedWitness]) -> Vec<Association> {
witnesses
.iter()
.map(|w| association(ids.person(w.person), event::witness_relationship(w.kind)))
.collect()
}
fn gender(sex: Sex) -> Option<GedSex> {
match sex {
Sex::Male => Some(GedSex::Male),
Sex::Female => Some(GedSex::Female),
Sex::Neuter => None,
}
}
fn death_event(ids: &Ids, death: &Death) -> Option<Event> {
let mut d = Event::new(EventKind::Death);
match death {
Death::NotDead | Death::DontKnowIfDead => return None,
Death::Dead { date, .. } => d.date = Some(date::to_gedcom(date)),
Death::DeadDontKnowWhen | Death::DeadYoung | Death::OfCourseDead => {
d.value = Text::from("Y");
}
}
if let Some(reason) = death_reason(death) {
d.extra.push(custom(ids.death_reason, reason));
}
Some(d)
}
fn death_reason(death: &Death) -> Option<&'static str> {
match death {
Death::Dead { reason, .. } => death_reason_label(*reason),
Death::DeadYoung => Some("died young"),
Death::OfCourseDead => Some("presumed dead"),
Death::NotDead | Death::DontKnowIfDead | Death::DeadDontKnowWhen => None,
}
}
fn multimedia(path: &str) -> MultimediaLink {
MultimediaLink {
files: std::iter::once(File {
path: Text::from(path),
..File::default()
})
.collect(),
..MultimediaLink::default()
}
}
fn attributes(person: &Person) -> Vec<Event> {
let mut out = Vec::new();
if !person.occupation.is_empty() {
out.push(attribute(EventKind::Occupation, &person.occupation));
}
for title in &person.titles {
let mut a = attribute(EventKind::Title, &title_value(title));
a.date = title_period(title);
set_notes(&mut a, title_holder(title, person));
out.push(a);
}
out
}
fn attribute(kind: EventKind, value: &str) -> Event {
Event {
value: Text::from(value),
..Event::new(kind)
}
}
fn page_tag(ids: &Ids, tag: TagId, page: &crate::database::Page) -> Node {
let mut node = Node::new(tag);
if !page.name.is_empty() {
node.payload = Value::Text(Text::from(page.name.as_str()));
}
node.children.push(Node {
payload: if page.text.is_empty() {
Value::None
} else {
Value::Text(Text::from(page.text.as_str()))
},
..Node::new(ids.note)
});
node
}
const _: Option<(&PersonEvent, &FamilyEvent, WitnessKind)> = None;
#[cfg(test)]
mod tests {
use super::*;
use oxidged::model::{Items, View};
fn convert(input: &str) -> Dataset {
GwDatabase::read(input.as_bytes(), "t.gw")
.expect("parses")
.to_gedcom()
}
fn xref(data: &Dataset, id: Option<XrefId>) -> Option<&str> {
id.map(|id| data.store().xref(id))
}
fn individual(data: &Dataset, i: usize) -> View<'_, Individual> {
data.individuals().get(i).expect("an individual")
}
fn family(data: &Dataset, i: usize) -> View<'_, Family> {
data.families().get(i).expect("a family")
}
fn extensions(nodes: Items<'_, Node>) -> Vec<(&str, Option<String>)> {
nodes
.into_iter()
.map(|n| (n.tag(), n.text().map(|t| t.to_string())))
.collect()
}
fn note_texts(notes: Items<'_, Note>) -> Vec<String> {
notes
.into_iter()
.filter_map(|n| n.text().map(|t| t.to_string()))
.collect()
}
fn event_of<'d>(individual: View<'d, Individual>, kind: &EventKind) -> View<'d, Event> {
individual
.events_of(kind.clone())
.next()
.unwrap_or_else(|| panic!("a {kind:?}"))
}
fn associated<'d>(data: &'d Dataset, event: View<'_, Event>) -> Vec<&'d str> {
event
.associations()
.into_iter()
.filter_map(|a| xref(data, a.individual()))
.collect()
}
#[test]
fn event_witnesses_are_not_repeated_on_the_individual() {
let data = convert(concat!(
"fam Doe John + Roe Jane\n",
"pevt Doe John\n",
"#birt 1900\n",
"#deat 1970\n",
"wit m: Poe Paul\n",
"end pevt\n",
"fam Poe Paul + Moe Mary\n",
));
let john = individual(&data, 0);
let death = event_of(john, &EventKind::Death);
assert_eq!(associated(&data, death), ["@I3@"]);
assert_eq!(john.associations().len(), 0);
assert!(john
.events()
.into_iter()
.all(|e| e.kind() == EventKind::Death || e.associations().is_empty()));
}
#[test]
fn a_family_becomes_two_individuals_and_a_family_record() {
let data = convert("fam Dupont Jean + Martin Marie\nbeg\n- m Paul 1930\nend\n");
assert_eq!(data.individuals().len(), 3);
assert_eq!(data.families().len(), 1);
let family = family(&data, 0);
assert_eq!(xref(&data, family.xref()), Some("@F1@"));
assert_eq!(xref(&data, family.husband_id()), Some("@I1@"));
assert_eq!(xref(&data, family.wife_id()), Some("@I2@"));
let children: Vec<_> = family
.children()
.into_iter()
.filter_map(|c| xref(&data, c.individual()))
.collect();
assert_eq!(children, ["@I3@"]);
}
#[test]
fn names_use_gedcom_slash_syntax() {
let data = convert("fam Dupont Jean + Martin Marie\n");
let name = individual(&data, 0).name().expect("a name");
assert_eq!(name.value(), "Jean /Dupont/");
assert_eq!(
name.surname().map(|s| s.to_string()).as_deref(),
Some("Dupont")
);
assert_eq!(name.given().map(|g| g.to_string()).as_deref(), Some("Jean"));
}
#[test]
fn aliases_become_additional_name_records() {
let data = convert("fam Dupont Jean {Jeannot} #salias Dupond (Le_Grand) 1900 + A B\n");
let values: Vec<_> = individual(&data, 0)
.names()
.into_iter()
.map(|n| n.value().to_string())
.collect();
assert!(values.iter().any(|v| v == "Jean /Dupont/"));
assert!(values.iter().any(|v| v == "Le Grand /Dupont/"));
assert!(values.iter().any(|v| v == "Jeannot /Dupont/"));
assert!(values.iter().any(|v| v == "Jean /Dupond/"));
}
fn nicknames(name: View<'_, Name>) -> Vec<String> {
name.pieces_of(NamePieceKind::Nickname)
.map(|n| n.to_string())
.collect()
}
#[test]
fn every_nickname_is_kept() {
let data = convert("fam Doe John #nick Johnny #nick Jacko + Roe Jane\n");
let names = individual(&data, 0).names();
assert_eq!(nicknames(names.first().expect("a name")), ["Johnny"]);
let second = names
.into_iter()
.find(|&n| nicknames(n) == ["Jacko"])
.expect("the second nickname");
assert_eq!(second.value(), "John /Doe/");
assert_eq!(
second.kind().map(View::<Phrased<NameType>>::value),
Some(NameType::Aka)
);
}
#[test]
fn a_nickname_is_a_piece_between_the_given_name_and_the_surname() {
let data = convert("fam Doe John #nick Johnny + Roe Jane\n");
let kinds: Vec<_> = individual(&data, 0)
.name()
.expect("a name")
.pieces()
.into_iter()
.map(|p| p.kind)
.collect();
assert_eq!(
kinds,
[
NamePieceKind::Given,
NamePieceKind::Nickname,
NamePieceKind::Surname
]
);
}
#[test]
fn parents_get_a_spouse_link_and_children_a_child_link() {
let data = convert("fam Dupont Jean + Martin Marie\nbeg\n- m Paul\nend\n");
let father = individual(&data, 0);
let spouse_of = father.spouse_of().first().expect("a spouse link");
assert_eq!(xref(&data, spouse_of.family()), Some("@F1@"));
assert!(father.child_of().is_empty());
let child = individual(&data, 2);
let link = child.child_of().first().expect("a child link");
assert_eq!(xref(&data, link.family()), Some("@F1@"));
assert!(child.spouse_of().is_empty());
}
#[test]
fn access_restrictions_are_also_a_resn() {
let data = convert(concat!(
"fam Doe John #apriv + Roe Jane #semipub\n",
"fam Poe Paul #apubl + Moe Mary\n",
));
let restriction = |i: usize| individual(&data, i).restriction();
let confidential = Some(EnumList(vec![Restriction::Confidential]));
assert_eq!(restriction(0), confidential);
assert_eq!(restriction(1), confidential);
assert_eq!(restriction(2), None);
assert_eq!(restriction(3), None);
}
#[test]
fn sex_is_written_only_when_recorded() {
let data = convert("fam Dupont Jean + Martin Marie\nbeg\n- Paul\nend\n");
assert_eq!(individual(&data, 0).sex(), Some(GedSex::Male));
assert_eq!(individual(&data, 1).sex(), Some(GedSex::Female));
assert_eq!(individual(&data, 2).sex(), None);
}
#[test]
fn life_events_carry_dates_places_and_sources() {
let data =
convert("fam Dupont Jean 7/9/1830 #bp Reims #bs acte 12/5/1900 #dp Lyon + A B\n");
let individual = individual(&data, 0);
let birth = event_of(individual, &EventKind::Birth);
assert_eq!(birth.date().unwrap().value(), "07 SEP 1830");
assert_eq!(birth.place().unwrap().name(), "Reims");
assert_eq!(birth.citations().len(), 1);
let death = event_of(individual, &EventKind::Death);
assert_eq!(death.place().unwrap().name(), "Lyon");
}
#[test]
fn a_living_person_gets_no_death_event() {
let data = convert("fam Dupont Jean 1990 + A B\n");
assert!(individual(&data, 0).death().is_none());
}
#[test]
fn occupations_and_titles_become_attributes() {
let data = convert("fam Dupont Jean [*:duc:Bretagne:::] #occu Marchand 1900 + A B\n");
let individual = individual(&data, 0);
let value = |kind| event_of(individual, &kind).value().to_string();
assert_eq!(value(EventKind::Occupation), "Marchand");
assert_eq!(value(EventKind::Title), "duc, Bretagne");
}
#[test]
fn titles_are_written_as_gwb2ged_writes_them() {
let data = convert(concat!(
"fam Doe John [Samplename:Count:Sampleshire:1800:1810:2] ",
"[:Baron:Sampleton:::] + A B\n",
));
let titles: Vec<_> = individual(&data, 0).events_of(EventKind::Title).collect();
assert_eq!(titles.len(), 2);
let count = titles[0];
assert_eq!(count.value(), "Count, Sampleshire, 2");
assert!(count.place().is_none());
assert_eq!(count.date().unwrap().value(), "FROM 1800 TO 1810");
assert_eq!(note_texts(count.notes()), ["Samplename"]);
let baron = titles[1];
assert_eq!(baron.value(), "Baron, Sampleton");
let owned = baron.into_owned();
assert!(owned.date.is_none() && owned.detail.is_none() && owned.place.is_none());
}
#[test]
fn geneweb_only_concepts_ride_on_custom_tags() {
let data = convert("fam Dupont Jean.2 #image p.jpg #apriv 1900 + A B\n");
let individual = individual(&data, 0);
let portrait = individual
.multimedia()
.first()
.expect("the portrait becomes GEDCOM multimedia");
assert_eq!(portrait.files().first().unwrap().path(), "p.jpg");
let tags = extensions(individual.extra());
assert!(tags.contains(&(TAG_OCCURRENCE, Some("2".to_owned()))));
assert!(tags.contains(&(TAG_ACCESS, Some("private".to_owned()))));
assert!(tags.contains(&(TAG_IMAGE, Some("p.jpg".to_owned()))));
}
fn death_reason_and_note(data: &Dataset) -> (Vec<String>, Vec<String>) {
let death = individual(data, 0).death().expect("a death");
let reasons = extensions(death.extra())
.into_iter()
.filter(|(tag, _)| *tag == TAG_DEATH_REASON)
.filter_map(|(_, value)| value)
.collect();
(reasons, note_texts(death.notes()))
}
#[test]
fn the_death_reason_is_a_tag_of_the_death_beside_its_note() {
let data = convert("fam Doe John k1900 + Roe Jane\n");
assert_eq!(
death_reason_and_note(&data),
(vec!["killed".to_owned()], Vec::new())
);
let mut db = GwDatabase::read(b"fam Doe John k1900 + Roe Jane\n", "t.gw").expect("parses");
db.persons[0].death_note = "Fell at the front".to_owned();
assert_eq!(
death_reason_and_note(&db.to_gedcom()),
(
vec!["killed".to_owned()],
vec!["Fell at the front".to_owned()]
)
);
}
#[test]
fn the_death_reason_survives_a_pevt_death() {
let data = convert("fam Doe John k1900 + Roe Jane\npevt Doe John\n#deat 1900\nend pevt\n");
assert_eq!(individual(&data, 0).events_of(EventKind::Death).count(), 1);
assert_eq!(
death_reason_and_note(&data),
(vec!["killed".to_owned()], Vec::new())
);
}
#[test]
fn a_pacs_keeps_its_kind_because_gedcom_has_no_event_for_it() {
let data = convert("fam A B + #pacs mf C D\n");
assert!(extensions(family(&data, 0).extra())
.contains(&(TAG_RELATION_KIND, Some("pacs".to_owned()))));
}
#[test]
fn family_events_and_divorce() {
let data = convert("fam A B +1850 -1860 C D\n");
let events = family(&data, 0).events();
assert!(events.into_iter().any(|e| e.kind() == EventKind::Marriage));
let divorce = events
.into_iter()
.find(|e| e.kind() == EventKind::Divorce)
.expect("a divorce");
assert_eq!(divorce.date().unwrap().value(), "1860");
}
#[test]
fn a_separation_is_a_generic_event() {
let data = convert("fam A B +1850 #sep C D\n");
let separation = family(&data, 0)
.events()
.into_iter()
.find(|e| e.kind() == EventKind::Event)
.expect("a separation");
assert_eq!(separation.classification().unwrap(), "Separation");
}
#[test]
fn witnesses_known_only_from_their_event_are_kept() {
let data = convert(concat!(
"fam Doe John + Roe Jane\n",
"fevt\n#marr 1925\nwit m: Poe Paul\nend fevt\n",
"pevt Doe John\n#deat 1970\nwit m: Moe Mark 1850\nend pevt\n",
));
let marriage = family(&data, 0).events().first().expect("a marriage");
assert_eq!(associated(&data, marriage), ["@I3@"]);
let death = event_of(individual(&data, 0), &EventKind::Death);
assert_eq!(associated(&data, death), ["@I4@"]);
assert_eq!(data.individuals().len(), 4);
}
#[test]
fn generic_events_keep_their_label() {
let data = convert("fam A B + C D\npevt A B\n#hosp 1914\nend pevt\n");
let event = event_of(individual(&data, 0), &EventKind::Event);
assert_eq!(event.classification().unwrap(), "Hospitalization");
assert!(extensions(event.extra()).contains(&(TAG_EVENT, Some("#hosp".to_owned()))));
}
#[test]
fn family_witnesses_survive_a_union_without_details() {
let data = convert("fam Doe John + Roe Jane\nwit m: Poe Paul\nfam Poe Paul + Moe Mary\n");
let union = family(&data, 0).events();
assert_eq!(union.len(), 1);
let event = union.first().expect("an event");
assert_eq!(event.kind(), EventKind::Marriage);
assert_eq!(associated(&data, event), ["@I3@"]);
}
#[test]
fn family_witnesses_join_a_superseding_fevt_marriage() {
let data = convert(concat!(
"fam Doe John + Roe Jane\n",
"wit m: Poe Paul\n",
"fevt\n#marr 1925\nwit f: Moe Mary\nend fevt\n",
"fam Poe Paul + Moe Mary\n",
));
let events = family(&data, 0).events();
assert_eq!(events.len(), 1);
assert_eq!(
associated(&data, events.first().expect("an event")),
["@I4@", "@I3@"]
);
}
#[test]
fn event_witnesses_become_associations() {
let data = convert(concat!(
"fam Dupont Jean + Martin Marie\n",
"pevt Dupont Jean\n",
"#birt 1850\n",
"wit m: #godp Martin Marie\n",
"end pevt\n",
));
let birth = event_of(individual(&data, 0), &EventKind::Birth);
assert_eq!(associated(&data, birth), ["@I2@"]);
let association = birth.associations().first().expect("an association");
assert_eq!(association.relation().unwrap(), "GODP");
}
#[test]
fn relations_without_a_gedcom_structure_become_associations() {
let data = convert(concat!(
"fam Dupont Jean + A B\n",
"rel Dupont Jean\nbeg\n",
"- reco fath: Martin Paul\n",
"- godp: Durand Luc + Durand Anne\n",
"end\n",
));
let labels: Vec<_> = individual(&data, 0)
.associations()
.into_iter()
.map(|a| a.relation().unwrap().to_string())
.collect();
assert_eq!(labels, ["recognising parent", "GODF", "GODM"]);
}
#[test]
fn an_adoption_becomes_an_adoptive_family() {
let data = convert(concat!(
"fam Doe John + Roe Jane\nbeg\n- h Paul 1930\nend\n",
"rel Doe Paul\nbeg\n- adop fath: Poe Peter\nend\n",
));
assert!(data
.individuals()
.into_iter()
.all(|i| i.associations().is_empty()));
let adoptive = family(&data, 1);
assert_eq!(xref(&data, adoptive.xref()), Some("@F2@"));
assert_eq!(xref(&data, adoptive.husband_id()), Some("@I4@"));
assert!(adoptive.wife().is_none());
let child = adoptive.children().first().expect("a child");
assert_eq!(xref(&data, child.individual()), Some("@I3@"));
let paul = individual(&data, 2);
let link = paul
.child_of()
.into_iter()
.find(|l| xref(&data, l.family()) == Some("@F2@"))
.expect("a link to the adoptive family");
assert_eq!(
link.pedigree().map(View::<Phrased<Pedigree>>::value),
Some(Pedigree::Adopted)
);
let adop = event_of(paul, &EventKind::Adoption);
let adop_family = adop.family().expect("its family");
assert_eq!(xref(&data, adop_family.family()), Some("@F2@"));
assert_eq!(
adop_family
.adopted_by()
.map(View::<Phrased<Adoption>>::value),
Some(Adoption::Husband)
);
assert!(individual(&data, 3)
.spouse_of()
.into_iter()
.any(|l| xref(&data, l.family()) == Some("@F2@")));
}
#[test]
fn foster_parents_become_a_foster_family() {
let data = convert(concat!(
"fam Doe John + Roe Jane\nbeg\n- h Paul 1930\nend\n",
"rel Doe Paul\nbeg\n- fost: Poe Peter + Poe Mary\nend\n",
));
let foster = family(&data, 1);
assert!(foster.husband().is_some() && foster.wife().is_some());
let paul = individual(&data, 2);
assert!(paul.child_of().into_iter().any(|l| {
xref(&data, l.family()) == Some("@F2@")
&& l.pedigree().map(View::<Phrased<Pedigree>>::value) == Some(Pedigree::Foster)
}));
assert!(paul.events_of(EventKind::Adoption).next().is_none());
}
#[test]
fn pages_and_wizard_notes_are_kept_at_record_level() {
let data = convert(concat!(
"page-ext Gallery\n content\nend page-ext\n",
"wizard-note henri\n 1234\nend wizard-note\n",
));
let tags: Vec<_> = extensions(data.extra())
.into_iter()
.map(|(tag, _)| tag)
.collect();
assert!(tags.contains(&TAG_PAGE));
assert!(tags.contains(&TAG_WIZARD));
}
#[test]
fn a_page_of_several_lines_reads_back_as_written() {
use oxidged::GedcomWriter;
let page = crate::database::Page {
name: "Sample".to_owned(),
text: "first\n\nthird".to_owned(),
};
let mut data = Dataset::new(GedcomVersion::V5_5_1);
let ids = Ids::new(data.store_mut(), 0, 0);
data.push(Record::Other(Box::new(page_tag(&ids, ids.page, &page))));
let written = GedcomWriter::new().write_to_string(&data).expect("writes");
assert!(
written.contains("0 _GWPAGE Sample\n1 NOTE first\n2 CONT\n2 CONT third\n"),
"{written}"
);
let read = Dataset::parse(written);
assert_eq!(
read.extra().first().unwrap().to_structure(),
data.extra().first().unwrap().to_structure()
);
}
#[test]
fn the_header_records_the_producer() {
let data = convert("fam A B + C D\n");
let source = data.header().unwrap().source.as_ref().unwrap();
assert_eq!(source.product.to_str(&data), "GENEWEB");
assert!(source
.name
.as_ref()
.unwrap()
.to_str(&data)
.starts_with("geneweb "));
}
#[test]
fn the_writer_repairs_only_what_a_gw_file_cannot_say() {
use oxidged::GedcomWriter;
let data = convert(concat!(
"fam Doe John #image p.jpg #nick Johnny [:Baron:Sampleton:1800::] #apriv k1900 ",
"+1850 #sep Roe Jane\n",
"wit m: Poe Paul\n",
"beg\n- h Paul 0(around_midsummer)\nend\n",
"pevt Doe John\n#hosp 1914\nwit m: Poe Paul\nend pevt\n",
"rel Doe Paul\nbeg\n- adop fath: Poe Peter\n- godp fath: Moe Mark\nend\n",
"page-ext Gallery\n first\n second\nend page-ext\n",
));
let repairs = |version| {
let (_, report) = GedcomWriter::new()
.gedcom_version(version)
.write_to_string_with_report(&data)
.expect("writes");
report
.repairs
.iter()
.map(|r| r.detail.to_string())
.collect::<Vec<_>>()
};
assert_eq!(
repairs(GedcomVersion::V5_5_1),
[
"INDI.EVEN.ASSO: not permitted here, written as _ASSO",
"INDI.OBJE.FILE lacks FORM: FORM jpg added",
"FAM.MARR.ASSO: not permitted here, written as _ASSO",
]
);
assert_eq!(
repairs(GedcomVersion::V7_0),
["OBJE.FILE lacks FORM: FORM image/jpeg added"]
);
}
}