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
use crate::{
parser::{parse_subset, Parser},
tokenizer::{Token, Tokenizer},
types::{
custom::UserDefinedTag,
date::change_date::ChangeDate,
event::{detail::Detail, util::HasEvents},
gedcom7::NonEvent,
lds::LdsOrdinance,
multimedia::Multimedia,
note::Note,
source::citation::Citation,
Xref,
},
GedcomError,
};
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
/// Family fact, representing a relationship between `Individual`s
///
/// This data representation understands that HUSB & WIFE are just poorly-named
/// pointers to individuals. no gender "validating" is done on parse.
///
/// # GEDCOM 7.0 Additions
///
/// In GEDCOM 7.0, families can have:
/// - `NO` - Non-event assertions (e.g., "NO CHIL" means no children)
///
/// See <https://gedcom.io/specifications/FamilySearchGEDCOMv7.html#NO>
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
pub struct Family {
pub xref: Option<Xref>,
pub individual1: Option<Xref>, // mapped from HUSB
pub individual2: Option<Xref>, // mapped from WIFE
pub family_event: Vec<Detail>,
pub children: Vec<Xref>,
pub num_children: Option<String>,
pub change_date: Option<ChangeDate>,
pub events: Vec<Detail>,
pub sources: Vec<Citation>,
pub multimedia: Vec<Multimedia>,
pub notes: Vec<Note>,
pub custom_data: Vec<Box<UserDefinedTag>>,
/// Non-event assertions for GEDCOM 7.0.
///
/// These assert that specific events did NOT occur (e.g., "NO CHIL" means
/// no children). This is distinct from omitting an event (which means unknown).
pub non_events: Vec<NonEvent>,
/// LDS (Latter-day Saints) sealing ordinance.
///
/// This includes SLGS (Sealing to spouse) for family records.
pub lds_ordinances: Vec<LdsOrdinance>,
/// Unique identifier (tag: UID).
///
/// A globally unique identifier for this record. In GEDCOM 7.0, this is
/// a URI that uniquely identifies the record across all datasets.
///
/// See <https://gedcom.io/specifications/FamilySearchGEDCOMv7.html#UID>
pub uid: Option<String>,
/// Restriction notice (tag: RESN).
///
/// A flag that indicates access to information has been restricted.
/// Valid values are:
/// - `confidential` - Not for public distribution
/// - `locked` - Cannot be modified
/// - `privacy` - Information is private
///
/// See <https://gedcom.io/specifications/FamilySearchGEDCOMv7.html#RESN>
pub restriction: Option<String>,
/// User reference number (tag: REFN).
///
/// A user-defined number or text that the submitter uses to identify
/// this record. Not guaranteed to be unique.
pub user_reference_number: Option<String>,
/// User reference type (tag: TYPE under REFN).
///
/// A user-defined type for the reference number.
pub user_reference_type: Option<String>,
/// Automated record ID (tag: RIN).
///
/// A unique record identification number assigned to the record by
/// the source system. Used for reconciling differences between systems.
pub automated_record_id: Option<String>,
/// External identifiers (tag: EXID, GEDCOM 7.0).
///
/// Identifiers maintained by external authorities that apply to this family.
pub external_ids: Vec<String>,
}
impl Family {
#[must_use]
fn with_xref(xref: Option<Xref>) -> Self {
Self {
xref,
..Default::default()
}
}
/// Creates a new `Family` from a `Tokenizer`.
///
/// # Errors
///
/// This function will return an error if parsing fails.
#[allow(clippy::double_must_use)]
pub fn new(
tokenizer: &mut Tokenizer,
level: u8,
xref: Option<Xref>,
) -> Result<Family, GedcomError> {
let mut fam = Family::with_xref(xref);
fam.children = Vec::new();
fam.events = Vec::new();
fam.sources = Vec::new();
fam.multimedia = Vec::new();
fam.notes = Vec::new();
fam.custom_data = Vec::new();
fam.parse(tokenizer, level)?;
Ok(fam)
}
/// Sets the first individual (e.g., husband) of the family.
///
/// # Errors
///
/// Returns a `GedcomError::ParseError` if the individual already exists.
pub fn set_individual1(&mut self, xref: Xref, line: u32) -> Result<(), GedcomError> {
if self.individual1.is_some() {
return Err(GedcomError::ParseError {
line,
message: "First individual of family already exists.".to_string(),
});
}
self.individual1 = Some(xref);
Ok(())
}
/// Sets the second individual (e.g., wife) of the family.
///
/// # Errors
///
/// Returns a `GedcomError::ParseError` if the individual already exists.
pub fn set_individual2(&mut self, xref: Xref, line: u32) -> Result<(), GedcomError> {
if self.individual2.is_some() {
return Err(GedcomError::ParseError {
line,
message: "Second individual of family already exists.".to_string(),
});
}
self.individual2 = Some(xref);
Ok(())
}
pub fn add_child(&mut self, xref: Xref) {
self.children.push(xref);
}
pub fn add_event(&mut self, family_event: Detail) {
self.events.push(family_event);
}
pub fn add_source(&mut self, sour: Citation) {
self.sources.push(sour);
}
pub fn add_multimedia(&mut self, media: Multimedia) {
self.multimedia.push(media);
}
pub fn add_note(&mut self, note: Note) {
self.notes.push(note);
}
#[must_use]
pub fn events(&self) -> &[Detail] {
&self.events
}
}
impl Parser for Family {
/// parse handles FAM top-level tag
fn parse(&mut self, tokenizer: &mut Tokenizer, level: u8) -> Result<(), GedcomError> {
// skip over FAM tag name
tokenizer.next_token()?;
let handle_subset = |tag: &str, tokenizer: &mut Tokenizer| -> Result<(), GedcomError> {
let mut pointer: Option<String> = None;
if let Token::Pointer(xref) = &tokenizer.current_token {
pointer = Some(xref.to_string());
tokenizer.next_token()?;
}
match tag {
"MARR" | "ANUL" | "CENS" | "DIV" | "DIVF" | "ENGA" | "MARB" | "MARC" | "MARL"
| "MARS" | "RESI" | "EVEN" | "SEP" => {
self.add_event(Detail::new(tokenizer, level + 1, tag)?);
}
"HUSB" => self.set_individual1(tokenizer.take_line_value()?, tokenizer.line)?,
"WIFE" => self.set_individual2(tokenizer.take_line_value()?, tokenizer.line)?,
"CHIL" => self.add_child(tokenizer.take_line_value()?),
"NCHI" => self.num_children = Some(tokenizer.take_line_value()?),
"CHAN" => self.change_date = Some(ChangeDate::new(tokenizer, level + 1)?),
"SOUR" => self.add_source(Citation::new(tokenizer, level + 1)?),
"NOTE" => self.add_note(Note::new(tokenizer, level + 1)?),
"OBJE" => self.add_multimedia(Multimedia::new(tokenizer, level + 1, pointer)?),
"NO" => self.non_events.push(NonEvent::new(tokenizer, level + 1)?),
// LDS Sealing to Spouse ordinance
"SLGS" => {
self.lds_ordinances
.push(LdsOrdinance::new(tokenizer, level + 1, tag)?);
}
// Unique identifier (GEDCOM 7.0)
"UID" => self.uid = Some(tokenizer.take_line_value()?),
// Restriction notice
"RESN" => self.restriction = Some(tokenizer.take_line_value()?),
// User reference number
"REFN" => {
self.user_reference_number = Some(tokenizer.take_line_value()?);
// Note: TYPE substructure would need to be parsed here
}
// Automated record ID
"RIN" => self.automated_record_id = Some(tokenizer.take_line_value()?),
// External identifier (GEDCOM 7.0)
"EXID" => self.external_ids.push(tokenizer.take_line_value()?),
_ => {
return Err(GedcomError::ParseError {
line: tokenizer.line,
message: format!("Unhandled Family Tag: {tag}"),
})
}
}
Ok(())
};
self.custom_data = parse_subset(tokenizer, level, handle_subset)?;
Ok(())
}
}
impl HasEvents for Family {
fn add_event(&mut self, event: Detail) {
let event_type = &event.event;
for e in &self.events {
assert!(
&e.event == event_type,
"Family already has a {:?} event",
e.event
);
}
self.events.push(event);
}
fn events(&self) -> Vec<Detail> {
self.events.clone()
}
}