1use crate::Error;
2use egml_core::model::base::{AbstractGml, Id};
3use quick_xml::Reader;
4use quick_xml::events::Event;
5
6pub fn parse_abstract_gml(source_text: &str, id: Id) -> Result<AbstractGml, Error> {
7 let mut reader = Reader::from_str(source_text);
8 reader.config_mut().trim_text(true);
9
10 let mut names = Vec::new();
11
12 let mut buf = Vec::new();
13
14 loop {
15 match reader.read_event_into(&mut buf) {
16 Ok(Event::Start(e)) => match e.name().as_ref() {
17 b"gml:name" => {
18 names.push(reader.read_text(e.name())?.to_string());
19 }
20 _ => {
21 reader.read_to_end(e.name())?;
22 }
23 },
24 Ok(Event::Eof) => break,
25 Err(e) => return Err(Error::from(e)),
26 _ => (),
27 }
28 }
29
30 Ok(AbstractGml { id, name: names })
31}
32
33#[cfg(test)]
34mod tests {
35 use crate::base::gml_name::parse_abstract_gml;
36 use egml_core::model::base::Id;
37
38 #[test]
39 fn parsing_abstract_gml_with_two_names() {
40 let id = Id::from_hashed_string("test_id");
41 let source_text = "<gml:name>my_name_1</gml:name><gml:name>my_name_2</gml:name>";
42
43 let abstract_gml = parse_abstract_gml(source_text, id).expect("");
44 assert_eq!(abstract_gml.name, vec!["my_name_1", "my_name_2"]);
45 }
46
47 #[test]
48 fn parsing_abstract_gml_with_other_elements() {
49 let id = Id::from_hashed_string("test_id");
50 let source_text = "
51 <gml:name>0507</gml:name>
52 <gml:boundedBy>
53 <gml:Envelope srsName=\"urn:ogc:def:crs:EPSG::25832\" srsDimension=\"3\">
54 <gml:lowerCorner>690984.6702564997 5336061.499479238 507.40999999999997</gml:lowerCorner>
55 <gml:upperCorner>691088.7286507211 5336156.516425779 537.924940696475</gml:upperCorner>
56 </gml:Envelope>
57 </gml:boundedBy>";
58
59 let abstract_gml = parse_abstract_gml(source_text, id).expect("");
60 assert_eq!(abstract_gml.name, vec!["0507"]);
61 }
62
63 #[test]
64 fn parsing_nested_abstract_gml() {
65 let id = Id::from_hashed_string("test_id");
66 let source_text = "
67 <gml:name>0507</gml:name>
68 <con:Window>
69 <gml:name>window 23</gml:name>
70 </con:Window>";
71
72 let abstract_gml = parse_abstract_gml(source_text, id).expect("");
73 assert_eq!(abstract_gml.name.len(), 1);
74 }
75}