gedcom_rs/types/
gedc.rs

1use super::Line;
2// use crate::parse;
3
4#[derive(Debug, Default, PartialEq)]
5// pub enum Form {
6//     LineageLinked,
7//     EventLineageLinked,
8//     Unknown,
9// }
10pub struct Form {
11    pub name: Option<String>,
12    pub version: Option<String>,
13}
14impl Form {
15    pub fn parse(mut buffer: &str) -> (&str, Option<Form>) {
16        let mut form = Form {
17            name: None,
18            version: None,
19        };
20
21        let mut line: Line;
22
23        line = Line::peek(&mut buffer).unwrap();
24
25        if line.tag == "FORM" {
26            line = Line::parse(&mut buffer).unwrap();
27
28            form.name = Some(line.value.to_string());
29
30            while !buffer.is_empty() {
31                // Peek the next line
32                line = Line::peek(&mut buffer).unwrap();
33                match line.tag {
34                    "VERS" => {
35                        // consume the line
36                        line = Line::parse(&mut buffer).unwrap();
37                        form.version = Some(line.value.to_string());
38                    }
39                    _ => {
40                        // panic!("Unknown tag: {}", line.tag);
41                        break;
42                    }
43                }
44            }
45        }
46
47        (buffer, Some(form))
48    }
49}
50
51#[derive(Debug, Default, PartialEq)]
52pub struct Gedc {
53    /// The version of this Gedcom file.
54    pub version: Option<String>,
55
56    /// The GEDCOM form used to construct this transmission.
57    pub form: Option<Form>,
58}
59impl Gedc {
60    pub fn parse(mut buffer: &str) -> (&str, Option<Gedc>) {
61        let mut gedc = Gedc {
62            version: None,
63            form: None,
64        };
65        let mut line: Line;
66
67        line = Line::peek(&mut buffer).unwrap();
68
69        if line.tag == "GEDC" {
70            Line::parse(&mut buffer).unwrap();
71
72            while !buffer.is_empty() {
73                // Peek the next line
74                line = Line::peek(&mut buffer).unwrap();
75                match line.tag {
76                    "FORM" => {
77                        (buffer, gedc.form) = Form::parse(buffer);
78                    }
79                    "VERS" => {
80                        // consume the line
81                        line = Line::parse(&mut buffer).unwrap();
82                        gedc.version = Some(line.value.to_string());
83                    }
84                    _ => {
85                        // panic!("Unknown tag: {}", line.tag);
86                        break;
87                    }
88                }
89            }
90        }
91
92        (buffer, Some(gedc))
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::Gedc;
99
100    #[test]
101    fn parse() {
102        let data = vec![
103            "1 GEDC",
104            "2 VERS 5.5.5",
105            "2 FORM LINEAGE-LINKED",
106            "3 VERS 5.5.5",
107        ];
108
109        let (_data, _gedc) = Gedc::parse(&data.join("\n"));
110        let gedc = _gedc.unwrap();
111        let form = gedc.form.unwrap();
112
113        assert!(gedc.version == Some("5.5.5".to_string()));
114
115        assert!(form.name == Some("LINEAGE-LINKED".to_string()));
116        assert!(form.version == Some("5.5.5".to_string()));
117
118        // // verify that the entire buffer has been consumed
119        // assert!(_data.is_empty())
120    }
121}