geneweb 0.1.2

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
//! The `fam` block.
//!
//! Port of `get_mar_date` and the `fam` arm of `read_family`.
//!
//! A family is one dense header line followed by optional sections in a fixed order:
//!
//! ```text
//! fam <father> +<date> [#tag <sexes>] [#mp place] [#mn note] [#ms src] [-<date>|#sep] <mother>
//! wit [m:|f:] <person>          (repeated)
//! src <family source>
//! csrc <default source for children>
//! cbp <default birth place for children>
//! comm <free text>
//! fevt … end fevt
//! beg
//! - [h|m|f] <child>             (repeated)
//! end
//! ```

use crate::date::{self, GwDate};
use crate::error::{GwErrorKind, Result};
use crate::lexer::{rest_after, Cursor};
use crate::model::event::{FamilyEventName, Witness, WitnessKind};
use crate::model::family::{Divorce, Family, RelationKind};
use crate::model::person::Person;
use crate::model::person::Sex;
use crate::parser::events::read_events;
use crate::parser::person::{get_optional_sex, parse_child, parse_parent, FamilyDefaults, LineCtx};
use crate::parser::{Line, LineReader};

/// Everything the `+…` middle of a family line carries.
struct Marriage {
    relation: RelationKind,
    father_sex: Sex,
    mother_sex: Sex,
    date: Option<GwDate>,
    place: String,
    note: String,
    src: String,
    divorce: Divorce,
}

/// The relation tags that carry an explicit two-letter sex code.
const SEXED_RELATIONS: &[(&str, RelationKind)] = &[
    ("#noment", RelationKind::NoMention),
    ("#nsck", RelationKind::NoSexesCheckNotMarried),
    ("#nsckm", RelationKind::NoSexesCheckMarried),
    ("#banns", RelationKind::MarriageBann),
    ("#contract", RelationKind::MarriageContract),
    ("#license", RelationKind::MarriageLicense),
    ("#pacs", RelationKind::Pacs),
    ("#residence", RelationKind::Residence),
];

/// Decodes a two-letter sex code such as `mf`, `f?` or `??`.
fn decode_sex_pair(code: &str) -> Option<(Sex, Sex)> {
    let letter = |c: u8| match c {
        b'm' => Some(Sex::Male),
        b'f' => Some(Sex::Female),
        b'?' => Some(Sex::Neuter),
        _ => None,
    };
    let b = code.as_bytes();
    if b.len() != 2 {
        return None;
    }
    Some((letter(b[0])?, letter(b[1])?))
}

/// Reads the `+…` portion of a family line, as `get_mar_date` does.
fn get_mar_date(cur: &mut Cursor<'_>, ctx: LineCtx<'_>) -> Result<Marriage> {
    let token = cur
        .advance()
        .ok_or_else(|| ctx.syntax("expected `+` to open the union"))?;
    if !token.starts_with('+') {
        return Err(ctx.syntax(format!("expected `+`, found `{token}`")));
    }
    let date = if token.len() > 1 {
        date::parse_from(token, 1).map_err(|kind| ctx.err(kind))?
    } else {
        None
    };

    let (relation, father_sex, mother_sex) = if cur.eat("#nm") {
        (RelationKind::NotMarried, Sex::Male, Sex::Female)
    } else if cur.eat("#eng") {
        (RelationKind::Engaged, Sex::Male, Sex::Female)
    } else {
        let matched = SEXED_RELATIONS
            .iter()
            .find(|(tag, _)| cur.peek() == Some(*tag));
        match matched {
            // `#noment` is the one tag that is also valid without a sex code.
            Some(("#noment", kind)) if cur.peek_at(1).is_none_or(|c| c.len() != 2) => {
                cur.advance();
                (*kind, Sex::Male, Sex::Female)
            }
            Some((_, kind)) if cur.peek_at(1).is_some_and(|c| c.len() == 2) => {
                let kind = *kind;
                cur.advance();
                // A code of the right length but the wrong letters keeps the relation
                // kind and leaves the token to be read as part of the mother's name.
                match decode_sex_pair(cur.peek().unwrap_or_default()) {
                    Some((f, m)) => {
                        cur.advance();
                        (kind, f, m)
                    }
                    None => (kind, Sex::Male, Sex::Female),
                }
            }
            // A sexed tag without its code is not recognised at all: the tag stays put
            // and will fail when the mother's name is parsed. This mirrors GeneWeb.
            _ => (RelationKind::Married, Sex::Male, Sex::Female),
        }
    };

    let place = cur.field("#mp");
    let note = cur.field("#mn");
    let src = cur.field("#ms");

    let divorce = match cur.peek() {
        Some("#sep") => {
            cur.advance();
            Divorce::Separated(None)
        }
        Some(token) if token.starts_with('-') => {
            let token = cur.advance().unwrap_or_default();
            if token.len() > 1 {
                Divorce::Divorced(date::parse_from(token, 1).map_err(|kind| ctx.err(kind))?)
            } else {
                Divorce::Divorced(None)
            }
        }
        _ => Divorce::NotDivorced,
    };

    Ok(Marriage {
        relation,
        father_sex,
        mother_sex,
        date,
        place,
        note,
        src,
        divorce,
    })
}

/// Reads a one-token section such as `src X`, which must have exactly two tokens.
fn read_tagged_section(reader: &mut LineReader<'_>, keyword: &str) -> Result<String> {
    let Some(line) = reader.peek_block_line() else {
        return Ok(String::new());
    };
    if line.keyword() != keyword {
        return Ok(String::new());
    }
    reader.next_block_line();
    match line.tokens.len() {
        2 => Ok(crate::lexer::cut_space(&line.tokens[1]).to_owned()),
        _ => Err(line
            .ctx()
            .syntax(format!("`{keyword}` takes exactly one value"))),
    }
}

/// Reads the run of `wit` lines under a family line.
///
/// Unlike event witnesses these carry no kind: the family line's `wit` syntax predates
/// event witnesses and admits only a sex marker.
fn read_family_witnesses(reader: &mut LineReader<'_>) -> Result<Vec<Witness>> {
    let mut witnesses = Vec::new();
    while let Some(line) = reader.peek_block_line() {
        if !matches!(line.keyword(), "wit" | "wit:") {
            break;
        }
        reader.next_block_line();
        let ctx = line.ctx();
        let mut cur = line.cursor();
        cur.advance();
        let sex = match cur.eat_any(&["m:", "f:"]) {
            Some("m:") => Sex::Male,
            Some("f:") => Sex::Female,
            _ => Sex::Neuter,
        };
        let (person, _) = parse_parent(&mut cur, ctx)?;
        if !cur.is_empty() {
            return Err(ctx.err(GwErrorKind::TrailingTokens(cur.remaining().to_vec())));
        }
        witnesses.push(Witness {
            person,
            sex,
            kind: WitnessKind::Witness,
        });
    }
    Ok(witnesses)
}

/// Reads a run of `comm` lines, joined back into the multi-line comment they came from.
///
/// `gwc` reads exactly one, and `gwu` writes exactly one because it flattens newlines on
/// the way out. Files in the wild — from other exporters — put each line of a comment on
/// its own `comm` line, and stopping after the first desynchronises the reader for the
/// rest of the file. Accepting the run is a strict superset: a single `comm` still parses
/// identically.
fn read_comment(reader: &mut LineReader<'_>) -> String {
    let mut lines: Vec<String> = Vec::new();
    while let Some(line) = reader.peek_block_line() {
        if line.keyword() != "comm" {
            break;
        }
        reader.next_block_line();
        // The raw remainder of the line, so that prose keeps its underscores. GeneWeb
        // slices unconditionally here and would crash on a bare `comm`; this yields an
        // empty comment instead.
        lines.push(rest_after(&line.raw, "comm").to_owned());
    }
    lines.join("\n")
}

/// Reads the `beg`…`end` block of children, if there is one.
fn read_children(
    reader: &mut LineReader<'_>,
    surname: &str,
    defaults: FamilyDefaults<'_>,
) -> Result<Vec<Person>> {
    let opens = matches!(reader.peek_block_line(), Some(line) if line.tokens == ["beg"]);
    if !opens {
        return Ok(Vec::new());
    }
    reader.next_block_line();

    let mut children = Vec::new();
    loop {
        let line = reader.next_block_line().ok_or_else(|| {
            LineCtx { no: 0, raw: "" }.err(GwErrorKind::UnexpectedEof { expected: "end" })
        })?;
        if line.tokens == ["end"] {
            return Ok(children);
        }
        let ctx = line.ctx();
        let mut cur = line.cursor();
        if !cur.eat("-") {
            return Err(ctx.syntax("expected a child line starting with `-`"));
        }
        let sex = get_optional_sex(&mut cur);
        let child = parse_child(&mut cur, surname, sex, defaults, ctx)?;
        if !cur.is_empty() {
            return Err(ctx.err(GwErrorKind::TrailingTokens(cur.remaining().to_vec())));
        }
        children.push(child);
    }
}

/// Reads a `fam` block, given its opening line.
///
/// # Errors
///
/// Returns an error when any part of the block is malformed.
pub fn read_family(
    reader: &mut LineReader<'_>,
    line: &Line,
    origin_file: &str,
) -> Result<Box<Family>> {
    let ctx = line.ctx();
    let mut cur = line.cursor();
    if !cur.eat("fam") {
        return Err(ctx.syntax("expected `fam`"));
    }

    let (father, surname) = parse_parent(&mut cur, ctx)?;
    let marriage = get_mar_date(&mut cur, ctx)?;
    let (mother, _) = parse_parent(&mut cur, ctx)?;
    if !cur.is_empty() {
        return Err(ctx.err(GwErrorKind::TrailingTokens(cur.remaining().to_vec())));
    }

    let witnesses = read_family_witnesses(reader)?;

    let sources = read_tagged_section(reader, "src")?;
    let children_sources = read_tagged_section(reader, "csrc")?;
    let children_birth_place = read_tagged_section(reader, "cbp")?;

    let comment = read_comment(reader);

    let events = match reader.peek_block_line() {
        Some(line) if line.keyword() == "fevt" => {
            reader.next_block_line();
            read_events(reader, "end fevt", FamilyEventName::from_tag)?
        }
        _ => Vec::new(),
    };

    let children = read_children(
        reader,
        &surname,
        FamilyDefaults {
            sources: &children_sources,
            birth_place: &children_birth_place,
        },
    )?;

    Ok(Box::new(Family {
        father,
        mother,
        father_sex: marriage.father_sex,
        mother_sex: marriage.mother_sex,
        relation: marriage.relation,
        marriage: marriage.date,
        marriage_place: marriage.place,
        marriage_note: marriage.note,
        marriage_src: marriage.src,
        divorce: marriage.divorce,
        witnesses,
        sources,
        children_sources,
        children_birth_place,
        comment,
        events,
        children,
        origin_file: origin_file.to_owned(),
    }))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::key::Somebody;

    fn family(input: &str) -> Box<Family> {
        let mut r = LineReader::new(input.as_bytes());
        // Test sources are UTF-8; a real file would say so with an `encoding: utf-8`
        // directive, without which the format is ISO-8859-1.
        r.set_encoding(crate::encoding::Encoding::Utf8);
        let line = r.next_block_line().expect("a fam line");
        read_family(&mut r, &line, "test.gw").expect("should parse")
    }

    #[test]
    fn a_minimal_family() {
        let f = family("fam Dupont Jean + Martin Marie\n");
        assert_eq!(f.relation, RelationKind::Married);
        assert!(matches!(f.father, Somebody::Reference(_)));
        assert!(matches!(f.mother, Somebody::Reference(_)));
        assert!(f.marriage.is_none());
        assert_eq!(f.divorce, Divorce::NotDivorced);
        assert_eq!(f.origin_file, "test.gw");
    }

    #[test]
    fn the_full_header_line() {
        let f = family(
            "fam Dupont Jean +3/6/1850 #nsckm mf #mp Reims #mn note #ms acte -1860 Martin Marie\n",
        );
        assert_eq!(f.relation, RelationKind::NoSexesCheckMarried);
        assert_eq!(f.father_sex, Sex::Male);
        assert_eq!(f.mother_sex, Sex::Female);
        assert_eq!(f.marriage_place, "Reims");
        assert_eq!(f.marriage_note, "note");
        assert_eq!(f.marriage_src, "acte");
        assert!(matches!(f.divorce, Divorce::Divorced(Some(_))));
    }

    #[test]
    fn sex_codes_can_be_swapped_or_unknown() {
        let f = family("fam A B + #nsck fm C D\n");
        assert_eq!(f.father_sex, Sex::Female);
        assert_eq!(f.mother_sex, Sex::Male);

        let f = family("fam A B + #pacs ?? C D\n");
        assert_eq!(f.father_sex, Sex::Neuter);
        assert_eq!(f.mother_sex, Sex::Neuter);
    }

    #[test]
    fn noment_is_the_only_sexed_tag_valid_without_a_code() {
        let f = family("fam A B + #noment C D\n");
        assert_eq!(f.relation, RelationKind::NoMention);
        assert_eq!(f.father_sex, Sex::Male);
    }

    #[test]
    fn separation_and_divorce_without_a_date() {
        assert_eq!(
            family("fam A B + #sep C D\n").divorce,
            Divorce::Separated(None)
        );
        assert_eq!(family("fam A B + - C D\n").divorce, Divorce::Divorced(None));
    }

    #[test]
    fn witnesses_sources_and_comment() {
        let f = family(concat!(
            "fam A B + C D\n",
            "wit m: Témoin Paul\n",
            "wit f: Témoin Anne\n",
            "src source_famille\n",
            "csrc source_enfants\n",
            "cbp Reims\n",
            "comm un commentaire avec des espaces\n",
        ));
        assert_eq!(f.witnesses.len(), 2);
        assert_eq!(f.witnesses[0].sex, Sex::Male);
        assert_eq!(f.witnesses[1].sex, Sex::Female);
        // Family witnesses never carry a kind.
        assert_eq!(f.witnesses[0].kind, WitnessKind::Witness);
        assert_eq!(f.sources, "source famille");
        assert_eq!(f.children_sources, "source enfants");
        assert_eq!(f.children_birth_place, "Reims");
        assert_eq!(f.comment, "un commentaire avec des espaces");
    }

    #[test]
    fn children_inherit_the_father_surname_and_family_defaults() {
        let f = family(concat!(
            "fam Galichet Jean + Loche Marie\n",
            "csrc défaut\n",
            "cbp Yzernay\n",
            "beg\n",
            "- m Pierre 1814\n",
            "- f Thérèse 1830 #bp Reims\n",
            "- h Paul Martin 1816\n",
            "end\n",
        ));
        assert_eq!(f.children.len(), 3);
        assert_eq!(f.children[0].surname, "Galichet");
        assert_eq!(f.children[0].sex, Sex::Male);
        assert_eq!(f.children[0].sources, "défaut");
        assert_eq!(f.children[0].birth_place, "Yzernay");
        assert_eq!(f.children[1].sex, Sex::Female);
        // An explicit birth place wins over `cbp`.
        assert_eq!(f.children[1].birth_place, "Reims");
        // An explicit surname wins over the father's.
        assert_eq!(f.children[2].surname, "Martin");
    }

    #[test]
    fn family_events() {
        let f = family(concat!(
            "fam A B + C D\n",
            "fevt\n",
            "#marr 1850 #p Reims\n",
            "note une note\n",
            "#div 1860\n",
            "end fevt\n",
        ));
        assert_eq!(f.events.len(), 2);
        assert_eq!(f.events[0].name, FamilyEventName::Marriage);
        assert_eq!(f.events[0].place, "Reims");
        assert_eq!(f.events[0].note, "une note");
        assert_eq!(f.events[1].name, FamilyEventName::Divorce);
    }

    #[test]
    fn every_section_at_once_in_order() {
        let f = family(concat!(
            "fam A B + C D\n",
            "wit Témoin Paul\n",
            "src s\n",
            "csrc cs\n",
            "cbp cb\n",
            "comm c\n",
            "fevt\n",
            "#marr\n",
            "end fevt\n",
            "beg\n",
            "- m Enfant\n",
            "end\n",
        ));
        assert_eq!(f.witnesses.len(), 1);
        assert_eq!(f.sources, "s");
        assert_eq!(f.children_sources, "cs");
        assert_eq!(f.children_birth_place, "cb");
        assert_eq!(f.comment, "c");
        assert_eq!(f.events.len(), 1);
        assert_eq!(f.children.len(), 1);
    }

    #[test]
    fn a_defined_parent_carries_its_own_details() {
        let f = family("fam Corno John 1935 #bp Soisy 1997 + Rempp Zabeth\n");
        let father = f.father.definition().expect("an inline definition");
        assert_eq!(father.first_name, "John");
        assert_eq!(father.birth_place, "Soisy");
        assert!(matches!(f.mother, Somebody::Reference(_)));
    }

    #[test]
    fn the_anonymous_spouse() {
        let f = family("fam Doe John 0 + ? ?\n");
        assert!(f.mother.definition().is_some());
        assert_eq!(f.mother.key().first_name, "?");
    }

    #[test]
    fn a_run_of_comm_lines_is_one_multi_line_comment() {
        // `gwu` flattens a comment onto a single `comm` line, but other exporters emit
        // one line each. Stopping after the first used to leave the remaining lines
        // looking like top-level blocks, which desynchronised the reader for the whole
        // rest of the file.
        let f = family(concat!(
            "fam A B + C D\n",
            "comm première ligne\n",
            "comm deuxième ligne\n",
            "comm troisième ligne\n",
            "beg\n",
            "- m Enfant\n",
            "end\n",
        ));
        assert_eq!(f.comment, "première ligne\ndeuxième ligne\ntroisième ligne");
        // The block after the comment still parses, which is the point.
        assert_eq!(f.children.len(), 1);
    }

    #[test]
    fn a_multivalued_src_line_is_an_error() {
        let mut r = LineReader::new(b"fam A B + C D\nsrc one two\n");
        let line = r.next_block_line().unwrap();
        assert!(read_family(&mut r, &line, "t.gw").is_err());
    }

    #[test]
    fn an_unterminated_children_block_is_an_error() {
        let mut r = LineReader::new(b"fam A B + C D\nbeg\n- m X\n");
        let line = r.next_block_line().unwrap();
        assert!(read_family(&mut r, &line, "t.gw").is_err());
    }
}