boring_parser/
parser.rs

1use crate::boring_structs_110::Boring110;
2use crate::boring_structs_210::Boring210;
3use crate::boring_structs_300::Boring300;
4use crate::boring_structs_400::Boring400;
5use crate::error::ParseError;
6use quick_xml::Reader;
7use quick_xml::de::from_str;
8use quick_xml::events::Event;
9use quick_xml::se::to_string;
10use std::str::{self};
11
12pub trait Parse<T> {
13    fn parse_from_str(xml_str: &str) -> Result<T, Box<dyn std::error::Error>>;
14}
15
16impl Parse<Boring110> for Boring110 {
17    fn parse_from_str(xml_str: &str) -> Result<Boring110, Box<dyn std::error::Error>> {
18        let boring: Boring110 = from_str(xml_str)?;
19        Ok(boring)
20    }
21}
22
23impl Parse<Boring210> for Boring210 {
24    fn parse_from_str(xml_str: &str) -> Result<Boring210, Box<dyn std::error::Error>> {
25        let boring: Boring210 = from_str(xml_str)?;
26        Ok(boring)
27    }
28}
29
30impl Parse<Boring300> for Boring300 {
31    fn parse_from_str(xml_str: &str) -> Result<Boring300, Box<dyn std::error::Error>> {
32        let boring: Boring300 = from_str(xml_str)?;
33        Ok(boring)
34    }
35}
36
37impl Parse<Boring400> for Boring400 {
38    fn parse_from_str(xml_str: &str) -> Result<Boring400, Box<dyn std::error::Error>> {
39        let boring: Boring400 = from_str(xml_str)?;
40        Ok(boring)
41    }
42}
43
44pub trait ToXml {
45    fn to_xml_string(&self) -> Result<String, Box<dyn std::error::Error>>;
46}
47
48impl ToXml for Boring110 {
49    fn to_xml_string(&self) -> Result<String, Box<dyn std::error::Error>> {
50        let xml = to_string(&self)?;
51        Ok(xml)
52    }
53}
54
55impl ToXml for Boring210 {
56    fn to_xml_string(&self) -> Result<String, Box<dyn std::error::Error>> {
57        let xml = to_string(&self)?;
58        Ok(xml)
59    }
60}
61
62impl ToXml for Boring300 {
63    fn to_xml_string(&self) -> Result<String, Box<dyn std::error::Error>> {
64        let xml = to_string(&self)?;
65        Ok(xml)
66    }
67}
68
69impl ToXml for Boring400 {
70    fn to_xml_string(&self) -> Result<String, Box<dyn std::error::Error>> {
71        let xml = to_string(&self)?;
72        Ok(xml)
73    }
74}
75
76pub fn extract_dtd_version(xml: &str) -> Result<String, ParseError> {
77    let mut reader = Reader::from_str(xml);
78    reader.trim_text(true);
79    let mut buf = Vec::new();
80    loop {
81        match reader.read_event_into(&mut buf) {
82            Ok(Event::Start(ref e)) if e.name().as_ref() == "ボーリング情報".as_bytes() => {
83                for attr in e.attributes().flatten() {
84                    if attr.key.as_ref() == b"DTD_version" {
85                        return Ok(attr.unescape_value().unwrap_or_default().to_string());
86                    }
87                }
88                break;
89            }
90            Ok(Event::Eof) => break,
91            _ => {}
92        }
93        buf.clear();
94    }
95    Err(ParseError::ParseError(String::from(
96        "DTD_version was not found. Plese check your XML file.",
97    )))
98}