geneweb 0.3.0

A parser for GeneWeb .gw genealogy files, with conversion to GEDCOM via ged_io
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
//! Turning a flat sequence of blocks into a connected genealogy.
//!
//! A `.gw` file has no identifiers. A person *is* their `(first name, surname,
//! occurrence)` key, they may be defined in one block and referred to from several
//! others, and their notes, events and relations arrive in blocks of their own. This
//! module interns people by key, resolves every reference to an index, and stitches the
//! stray blocks onto the person they name.
//!
//! The one exception is the anonymous person, written `? ?`. Two of those are not the
//! same person, so they are never interned and never merged.

use std::collections::HashMap;

use crate::date::GwDate;
use crate::error::Result;
use crate::model::block::GwBlock;
use crate::model::event::Witness;
use crate::model::family::{Divorce, RelationKind};
use crate::model::key::{Key, Somebody};
use crate::model::person::{Person, Sex};
use crate::model::{FamilyEvent, Title};
use crate::parser::block::BlockReader;

/// An index into [`GwDatabase::persons`].
pub type PersonId = usize;

/// An index into [`GwDatabase::families`].
pub type FamilyId = usize;

/// A witness with their person resolved to an index.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedWitness {
    /// Who witnessed.
    pub person: PersonId,
    /// Their sex, as written on the `wit` line.
    pub sex: Sex,
    /// The capacity in which they attended.
    pub kind: crate::model::event::WitnessKind,
}

/// A family with every person resolved to an index.
#[derive(Debug, Clone, PartialEq)]
pub struct FamilyRecord {
    /// The first parent.
    pub father: PersonId,
    /// The second parent.
    pub mother: PersonId,
    /// The nature of the union.
    pub relation: RelationKind,
    /// Date of the union.
    pub marriage: Option<GwDate>,
    /// Place of the union.
    pub marriage_place: String,
    /// Note on the union.
    pub marriage_note: String,
    /// Source for the union.
    pub marriage_src: String,
    /// How the union ended.
    pub divorce: Divorce,
    /// Witnesses to the union.
    pub witnesses: Vec<ResolvedWitness>,
    /// Source for the family.
    pub sources: String,
    /// Free-text comment.
    pub comment: String,
    /// Structured family events.
    pub events: Vec<FamilyEvent>,
    /// The children.
    pub children: Vec<PersonId>,
    /// Basename of the file this family came from.
    pub origin_file: String,
}

/// A named free-text page: the base's presentation notes, an extended page, or a wizard's
/// note.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Page {
    /// The page name, or the wizard's identifier. Empty for the base's own notes.
    pub name: String,
    /// The page text, in GeneWeb's wiki syntax.
    pub text: String,
}

/// A whole `.gw` file, with references resolved.
#[derive(Debug, Clone, Default)]
pub struct GwDatabase {
    /// Every person, in the order they were first seen.
    pub persons: Vec<Person>,
    /// Every family, in file order.
    pub families: Vec<FamilyRecord>,
    /// The base's presentation notes and extended pages.
    pub pages: Vec<Page>,
    /// Notes attached to contributors rather than to people.
    pub wizard_notes: Vec<Page>,
    /// Whether the file declared `gwplus`.
    pub gwplus: bool,
    index: HashMap<Key, PersonId>,
    /// The witnesses of each person's `pevt` events, resolved, per event.
    person_event_witnesses: HashMap<PersonId, Vec<Vec<ResolvedWitness>>>,
    /// The witnesses of each family's `fevt` events, resolved, per event; parallel to
    /// `families`.
    family_event_witnesses: Vec<Vec<Vec<ResolvedWitness>>>,
}

/// Copies `new` over `existing` wherever `existing` has nothing.
///
/// A person can be written more than once — defined as a child in one family and again
/// as a spouse in another — and the two spellings need not carry the same detail. Filling
/// only the gaps keeps whichever block was more complete without letting a later, sparser
/// mention erase it.
fn merge_person(existing: &mut Person, new: Person) {
    fn fill(target: &mut String, value: String) {
        if target.is_empty() {
            *target = value;
        }
    }
    fn fill_vec<T>(target: &mut Vec<T>, value: Vec<T>) {
        if target.is_empty() {
            *target = value;
        }
    }
    fn fill_date(target: &mut Option<GwDate>, value: Option<GwDate>) {
        if target.is_none() {
            *target = value;
        }
    }
    fn fill_titles(target: &mut Vec<Title>, value: Vec<Title>) {
        if target.is_empty() {
            *target = value;
        }
    }

    if existing.sex == Sex::Neuter {
        existing.sex = new.sex;
    }
    fill_vec(&mut existing.first_names_aliases, new.first_names_aliases);
    fill_vec(&mut existing.surnames_aliases, new.surnames_aliases);
    fill(&mut existing.public_name, new.public_name);
    fill(&mut existing.image, new.image);
    fill_vec(&mut existing.qualifiers, new.qualifiers);
    fill_vec(&mut existing.aliases, new.aliases);
    fill_titles(&mut existing.titles, new.titles);
    if existing.access == crate::model::person::Access::IfTitles {
        existing.access = new.access;
    }
    fill(&mut existing.occupation, new.occupation);
    fill(&mut existing.sources, new.sources);
    fill_date(&mut existing.birth, new.birth);
    fill(&mut existing.birth_place, new.birth_place);
    fill(&mut existing.birth_note, new.birth_note);
    fill(&mut existing.birth_src, new.birth_src);
    fill_date(&mut existing.baptism, new.baptism);
    fill(&mut existing.baptism_place, new.baptism_place);
    fill(&mut existing.baptism_note, new.baptism_note);
    fill(&mut existing.baptism_src, new.baptism_src);
    if existing.death == crate::model::person::Death::DontKnowIfDead {
        existing.death = new.death;
    }
    fill(&mut existing.death_place, new.death_place);
    fill(&mut existing.death_note, new.death_note);
    fill(&mut existing.death_src, new.death_src);
    if existing.burial == crate::model::person::Burial::Unknown {
        existing.burial = new.burial;
    }
    fill(&mut existing.burial_place, new.burial_place);
    fill(&mut existing.burial_note, new.burial_note);
    fill(&mut existing.burial_src, new.burial_src);
    fill(&mut existing.notes, new.notes);
    fill_vec(&mut existing.events, new.events);
    fill_vec(&mut existing.relations, new.relations);
}

impl GwDatabase {
    /// Reads a whole `.gw` file.
    ///
    /// `origin_file` is recorded on every family, as GeneWeb records it.
    ///
    /// # Errors
    ///
    /// Returns the first parse error. Use [`GwDatabase::read_lenient`] to skip malformed
    /// blocks instead.
    pub fn read(input: &[u8], origin_file: &str) -> Result<Self> {
        Self::build(BlockReader::new(input, origin_file))
    }

    /// Reads a whole `.gw` file, skipping blocks that fail to parse.
    ///
    /// Returns the database and the errors that were skipped.
    ///
    /// # Errors
    ///
    /// Never returns `Err`; the signature matches [`GwDatabase::read`] for symmetry.
    #[must_use]
    pub fn read_lenient(input: &[u8], origin_file: &str) -> (Self, Vec<crate::GwError>) {
        let mut errors = Vec::new();
        let mut reader = BlockReader::new(input, origin_file).lenient(true);
        let mut db = Self::default();
        loop {
            match reader.next() {
                None => break,
                Some(Ok(block)) => db.add_block(block),
                Some(Err(e)) => errors.push(e),
            }
        }
        db.gwplus = reader.is_gwplus();
        (db, errors)
    }

    fn build(mut reader: BlockReader<'_>) -> Result<Self> {
        let mut db = Self::default();
        // `by_ref` so the reader survives the loop and can report its directives.
        for block in reader.by_ref() {
            db.add_block(block?);
        }
        db.gwplus = reader.is_gwplus();
        Ok(db)
    }

    /// The witnesses of a person's `pevt` event, by the event's position in
    /// [`Person::events`], with every person resolved to an index.
    ///
    /// Empty for an event without witnesses or a position out of range.
    #[must_use]
    pub fn person_event_witnesses(&self, person: PersonId, event: usize) -> &[ResolvedWitness] {
        self.person_event_witnesses
            .get(&person)
            .and_then(|events| events.get(event))
            .map_or(&[], Vec::as_slice)
    }

    /// The witnesses of a family's `fevt` event, by the event's position in
    /// [`FamilyRecord::events`], with every person resolved to an index.
    ///
    /// Empty for an event without witnesses or a position out of range.
    #[must_use]
    pub fn family_event_witnesses(&self, family: FamilyId, event: usize) -> &[ResolvedWitness] {
        self.family_event_witnesses
            .get(family)
            .and_then(|events| events.get(event))
            .map_or(&[], Vec::as_slice)
    }

    /// The person a key names, if the file defines or mentions one.
    #[must_use]
    pub fn lookup(&self, key: &Key) -> Option<PersonId> {
        self.index.get(key).copied()
    }

    /// Interns a person by key, creating a stub if only referenced so far.
    fn intern_key(&mut self, key: Key) -> PersonId {
        if let Some(&id) = self.index.get(&key) {
            return id;
        }
        let id = self.persons.len();
        self.persons
            .push(Person::new(&key.first_name, &key.surname, key.occ));
        self.index.insert(key, id);
        id
    }

    /// Adds a person defined inline, merging into an existing entry for the same key.
    fn intern_definition(&mut self, person: Person) -> PersonId {
        let key = Key::new(&person.first_name, &person.surname, person.occ);
        // Two `? ?` are two people. They get no index entry, so nothing can ever be
        // merged into them or resolved to them.
        if key.is_anonymous() {
            let id = self.persons.len();
            self.persons.push(person);
            return id;
        }
        if let Some(id) = self.index.get(&key).copied() {
            merge_person(&mut self.persons[id], person);
            return id;
        }
        let id = self.persons.len();
        self.persons.push(person);
        self.index.insert(key, id);
        id
    }

    fn intern(&mut self, who: Somebody) -> PersonId {
        match who {
            Somebody::Reference(key) => self.intern_key(key),
            Somebody::Definition(person) => self.intern_definition(*person),
        }
    }

    fn intern_witnesses(&mut self, witnesses: Vec<Witness>) -> Vec<ResolvedWitness> {
        witnesses
            .into_iter()
            .map(|w| ResolvedWitness {
                person: self.intern(w.person),
                sex: w.sex,
                kind: w.kind,
            })
            .collect()
    }

    /// Resolves the witnesses of `gwplus` events, interning each of them as the
    /// witnesses of a family line are: a witness defined inline becomes a person, one
    /// only referenced gets a stub, and a `? ?` is a person of their own. The syntax
    /// tree keeps its witnesses as written.
    fn intern_event_witnesses<'a, N: 'a>(
        &mut self,
        events: impl IntoIterator<Item = &'a crate::model::event::Event<N>>,
    ) -> Vec<Vec<ResolvedWitness>> {
        events
            .into_iter()
            .map(|event| self.intern_witnesses(event.witnesses.clone()))
            .collect()
    }

    fn set_sex(&mut self, id: PersonId, sex: Sex) {
        if sex != Sex::Neuter && self.persons[id].sex == Sex::Neuter {
            self.persons[id].sex = sex;
        }
    }

    fn add_block(&mut self, block: GwBlock) {
        match block {
            GwBlock::Family(family) => {
                let family = *family;
                let father = self.intern(family.father);
                let mother = self.intern(family.mother);
                self.set_sex(father, family.father_sex);
                self.set_sex(mother, family.mother_sex);
                let witnesses = self.intern_witnesses(family.witnesses);
                let event_witnesses = self.intern_event_witnesses(&family.events);
                let children = family
                    .children
                    .into_iter()
                    .map(|c| self.intern_definition(c))
                    .collect();
                self.families.push(FamilyRecord {
                    father,
                    mother,
                    relation: family.relation,
                    marriage: family.marriage,
                    marriage_place: family.marriage_place,
                    marriage_note: family.marriage_note,
                    marriage_src: family.marriage_src,
                    divorce: family.divorce,
                    witnesses,
                    sources: family.sources,
                    comment: family.comment,
                    events: family.events,
                    children,
                    origin_file: family.origin_file,
                });
                self.family_event_witnesses.push(event_witnesses);
            }
            GwBlock::PersonNotes { key, text } => {
                let id = self.intern_key(key);
                self.persons[id].notes = text;
            }
            GwBlock::Relations {
                person,
                sex,
                relations,
            } => {
                // Resolve the relations' own people before borrowing the subject.
                let resolved = relations
                    .into_iter()
                    .map(|mut r| {
                        r.father = r.father.map(|f| Somebody::Reference(self.resolve_key(f)));
                        r.mother = r.mother.map(|m| Somebody::Reference(self.resolve_key(m)));
                        r
                    })
                    .collect();
                let id = self.intern(person);
                self.set_sex(id, sex);
                self.persons[id].relations = resolved;
            }
            GwBlock::PersonEvents { person, events, .. } => {
                let id = self.intern(person);
                let witnesses = self.intern_event_witnesses(&events);
                self.persons[id].events = events;
                self.person_event_witnesses.insert(id, witnesses);
            }
            GwBlock::DatabaseNotes { page, text } => {
                self.pages.push(Page { name: page, text });
            }
            GwBlock::WizardNotes { wizard, text } => {
                self.wizard_notes.push(Page { name: wizard, text });
            }
        }
    }

    /// Interns a person mentioned inside a relation and returns their key.
    ///
    /// Relations keep keys rather than indices so that [`Person`] stays free of database
    /// concerns; interning here is what guarantees the key resolves later.
    fn resolve_key(&mut self, who: Somebody) -> Key {
        let key = who.key();
        self.intern(who);
        key
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn db(input: &str) -> GwDatabase {
        GwDatabase::read(input.as_bytes(), "t.gw").expect("should parse")
    }

    #[test]
    fn a_person_defined_once_and_referenced_twice_is_one_person() {
        let db = db(concat!(
            "fam Dupont Jean 1900 + Martin Marie\n",
            "beg\n- m Paul 1930\nend\n",
            "fam Dupont Paul + Durand Anne\n",
        ));
        // Jean, Marie, Paul, Anne.
        assert_eq!(db.persons.len(), 4);
        let paul = db.lookup(&Key::new("Paul", "Dupont", 0)).expect("Paul");
        assert_eq!(db.families[0].children, vec![paul]);
        assert_eq!(db.families[1].father, paul);
        // The child definition carried the birth date; the later reference did not
        // erase it.
        assert!(db.persons[paul].birth.is_some());
    }

    #[test]
    fn two_anonymous_people_are_two_people() {
        let db = db("fam Doe John 0 + ? ?\nfam Roe Jane 0 + ? ?\n");
        assert_eq!(db.persons.len(), 4);
        assert!(db.lookup(&Key::new("?", "?", 0)).is_none());
    }

    #[test]
    fn occurrence_numbers_separate_homonyms() {
        let db = db("fam Dupont Jean + A B\nfam Dupont Jean.1 + C D\n");
        assert_eq!(db.persons.len(), 4);
        assert!(db.lookup(&Key::new("Jean", "Dupont", 0)).is_some());
        assert!(db.lookup(&Key::new("Jean", "Dupont", 1)).is_some());
    }

    #[test]
    fn notes_and_events_attach_to_the_person_they_name() {
        let db = db(concat!(
            "fam Dupont Jean + Martin Marie\n",
            "notes Dupont Jean\nbeg\nsome notes\nend notes\n",
            "pevt Dupont Jean\n#birt 1850\nend pevt\n",
        ));
        let jean = db.lookup(&Key::new("Jean", "Dupont", 0)).expect("Jean");
        assert_eq!(db.persons[jean].notes, "some notes");
        assert_eq!(db.persons[jean].events.len(), 1);
    }

    #[test]
    fn a_block_may_name_a_person_before_any_family_does() {
        let db = db("notes Dupont Jean\nbeg\nearly\nend notes\nfam Dupont Jean + A B\n");
        // Jean Dupont, plus the spouse whose surname is `A` and given name `B`.
        assert_eq!(db.persons.len(), 2);
        let jean = db.lookup(&Key::new("Jean", "Dupont", 0)).expect("Jean");
        assert_eq!(db.persons[jean].notes, "early");
        assert_eq!(db.families[0].father, jean);
    }

    #[test]
    fn the_family_line_supplies_parent_sexes() {
        let db = db("fam Dupont Jean + Martin Marie\n");
        assert_eq!(db.persons[db.families[0].father].sex, Sex::Male);
        assert_eq!(db.persons[db.families[0].mother].sex, Sex::Female);
    }

    #[test]
    fn relation_targets_are_interned_too() {
        let db = db("rel Dupont Jean\nbeg\n- adop fath: Martin Paul\nend\n");
        assert!(db.lookup(&Key::new("Paul", "Martin", 0)).is_some());
        let jean = db.lookup(&Key::new("Jean", "Dupont", 0)).expect("Jean");
        assert_eq!(db.persons[jean].relations.len(), 1);
    }

    #[test]
    fn event_witnesses_are_interned_like_family_witnesses() {
        let db = db(concat!(
            "fam Doe John + Roe Jane\n",
            "fevt\n#marr 1925\nwit m: Poe Paul\nend fevt\n",
            "pevt Doe John\n",
            "#deat 1970\n",
            "wit m: Moe Mark 1850\n",
            "wit f: ? ?\n",
            "end pevt\n",
        ));
        // John, Jane, Paul (referenced only), Mark (defined inline) and the anonymous
        // witness.
        assert_eq!(db.persons.len(), 5);

        let paul = db.lookup(&Key::new("Paul", "Poe", 0)).expect("Paul");
        let marriage_witnesses = db.family_event_witnesses(0, 0);
        assert_eq!(marriage_witnesses.len(), 1);
        assert_eq!(marriage_witnesses[0].person, paul);

        let john = db.lookup(&Key::new("John", "Doe", 0)).expect("John");
        let mark = db.lookup(&Key::new("Mark", "Moe", 0)).expect("Mark");
        // The inline definition carried a birth date.
        assert!(db.persons[mark].birth.is_some());
        let death_witnesses = db.person_event_witnesses(john, 0);
        assert_eq!(death_witnesses.len(), 2);
        assert_eq!(death_witnesses[0].person, mark);
        // The `? ?` witness is a person of their own, not dropped.
        assert_eq!(db.persons[death_witnesses[1].person].first_name, "?");
        // The syntax tree keeps the witnesses as written.
        assert_eq!(db.persons[john].events[0].witnesses.len(), 2);
    }

    #[test]
    fn pages_and_wizard_notes_are_collected() {
        let db = db(concat!(
            "notes-db\n  presentation\nend notes-db\n",
            "page-ext Gallery\n  content\nend page-ext\n",
            "wizard-note henri\n  1234\nend wizard-note\n",
        ));
        assert_eq!(db.pages.len(), 2);
        assert_eq!(db.pages[0].name, "");
        assert_eq!(db.pages[1].name, "Gallery");
        assert_eq!(db.wizard_notes.len(), 1);
        assert_eq!(db.wizard_notes[0].name, "henri");
    }

    #[test]
    fn lenient_reading_reports_errors_and_keeps_going() {
        let (db, errors) = GwDatabase::read_lenient(b"nonsense\nfam A B + C D\n", "t.gw");
        assert_eq!(errors.len(), 1);
        assert_eq!(db.families.len(), 1);
    }
}