Skip to main content

boring_parser/
parser.rs

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