dsn-visualizer/src/dsn/mod.rs

70 lines
2.3 KiB
Rust

use crate::dsn::models::{Dish, ParseError, Signal, Station, Target};
use std::convert::TryFrom;
use std::io::BufReader;
use xml::reader::XmlEvent;
use xml::EventReader;
pub mod models;
pub fn parse_dsn_resp<T>(reader: BufReader<T>) -> Result<Vec<Station>, ParseError>
where
T: std::io::Read,
{
let mut stations = Vec::new();
let mut station: Option<Station> = None;
let mut dish: Option<Dish> = None;
let parser = EventReader::new(reader).into_iter();
for e in parser {
match e {
Ok(XmlEvent::StartElement {
name, attributes, ..
}) => {
if name.local_name.contains("station") {
if let Some(station) = station {
stations.push(station);
}
station = Some(Station::try_from(attributes).unwrap());
} else if name.local_name.contains("dish") {
dish = Some(Dish::try_from(attributes).unwrap());
} else if name.local_name.contains("downSignal") {
dish = Some(
dish.unwrap()
.set_down_signal(Signal::try_from(attributes).unwrap()),
);
} else if name.local_name.contains("upSignal") {
dish = Some(
dish.unwrap()
.set_up_signal(Signal::try_from(attributes).unwrap()),
);
} else if name.local_name.contains("target") {
dish = Some(
dish.unwrap()
.set_target(Target::try_from(attributes).unwrap()),
);
}
}
Ok(XmlEvent::EndElement { name }) => {
if name.local_name.contains("dish") && station.is_some() && dish.is_some() {
station = Some(station.unwrap().add_dish(dish.unwrap()));
dish = None;
}
}
Ok(XmlEvent::EndDocument) => {
if let Some(station) = station {
stations.push(station);
break;
}
}
Err(e) => {
println!("Error: {}", e);
break;
}
_ => {}
}
}
Ok(stations)
}