1use super::Line;
2#[derive(Debug, Default, PartialEq)]
5pub 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 line = Line::peek(&mut buffer).unwrap();
33 match line.tag {
34 "VERS" => {
35 line = Line::parse(&mut buffer).unwrap();
37 form.version = Some(line.value.to_string());
38 }
39 _ => {
40 break;
42 }
43 }
44 }
45 }
46
47 (buffer, Some(form))
48 }
49}
50
51#[derive(Debug, Default, PartialEq)]
52pub struct Gedc {
53 pub version: Option<String>,
55
56 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 line = Line::peek(&mut buffer).unwrap();
75 match line.tag {
76 "FORM" => {
77 (buffer, gedc.form) = Form::parse(buffer);
78 }
79 "VERS" => {
80 line = Line::parse(&mut buffer).unwrap();
82 gedc.version = Some(line.value.to_string());
83 }
84 _ => {
85 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 }
121}