Parse XML
This commit is contained in:
parent
75c32c98eb
commit
aa4da7bbca
|
@ -3,7 +3,7 @@
|
||||||
<Publisher>Jaby Wuff</Publisher>
|
<Publisher>Jaby Wuff</Publisher>
|
||||||
<License>C:/../</License>
|
<License>C:/../</License>
|
||||||
</Description>
|
</Description>
|
||||||
<Track type="data">
|
<Track>
|
||||||
<File name="Miau.png">C:/../</File>
|
<File name="Miau.png">C:/../</File>
|
||||||
<Audiofile>C:/../</Audiofile>
|
<Audiofile>C:/../</Audiofile>
|
||||||
<Directory name="Wuff">
|
<Directory name="Wuff">
|
||||||
|
|
|
@ -1,6 +1,54 @@
|
||||||
use super::{Error, ErrorString};
|
use super::{Error, ErrorString};
|
||||||
|
use std::path::PathBuf;
|
||||||
mod xml;
|
mod xml;
|
||||||
|
|
||||||
pub fn parse_xml(xml: String) -> Result<(), Error> {
|
pub struct Configuration {
|
||||||
|
pub publisher: String,
|
||||||
|
pub license_path: PathBuf,
|
||||||
|
pub root: Directory,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Configuration {
|
||||||
|
pub fn new() -> Configuration {
|
||||||
|
Configuration{publisher: String::new(), license_path: PathBuf::new(), root: Directory::new("root")}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct File {
|
||||||
|
pub name: String,
|
||||||
|
pub path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl File {
|
||||||
|
pub fn new() -> File {
|
||||||
|
File{name: String::new(), path: PathBuf::new()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Directory {
|
||||||
|
pub name: String,
|
||||||
|
member: Vec<DirMember>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Directory {
|
||||||
|
pub fn new(name: &str) -> Directory {
|
||||||
|
Directory{name: String::from(name), member: Vec::new()}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_file(&mut self, file: File) {
|
||||||
|
self.member.push(DirMember::File(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_dir(&mut self, dir: Directory) {
|
||||||
|
self.member.push(DirMember::Directory(dir));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum DirMember {
|
||||||
|
File(File),
|
||||||
|
Directory(Directory),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_xml(xml: String) -> Result<Configuration, Error> {
|
||||||
Ok(xml::parse(xml)?)
|
Ok(xml::parse(xml)?)
|
||||||
}
|
}
|
|
@ -1,9 +1,72 @@
|
||||||
use super::ErrorString;
|
use std::path::PathBuf;
|
||||||
|
use crate::config_reader::Directory;
|
||||||
|
|
||||||
pub fn parse(xml: String) -> Result<(), Error> {
|
use super::{Configuration, ErrorString, File};
|
||||||
roxmltree::Document::parse(xml.as_str())?;
|
|
||||||
|
|
||||||
Ok(())
|
pub fn parse(xml: String) -> Result<Configuration, Error> {
|
||||||
|
let mut config = Configuration::new();
|
||||||
|
let parser = roxmltree::Document::parse(xml.as_str())?;
|
||||||
|
let children = parser.root().children();
|
||||||
|
|
||||||
|
for node in children {
|
||||||
|
if node.is_element() && node.tag_name().name() == "ISO_Project" {
|
||||||
|
parse_iso_project(node, &mut config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_iso_project(iso_project: roxmltree::Node, config: &mut Configuration) {
|
||||||
|
for node in iso_project.children() {
|
||||||
|
if node.is_element() {
|
||||||
|
match node.tag_name().name() {
|
||||||
|
"Description" => parse_description(node, config),
|
||||||
|
"Track" => parse_track(node, config),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_description(description: roxmltree::Node, config: &mut Configuration) {
|
||||||
|
for node in description.descendants() {
|
||||||
|
if node.is_element() {
|
||||||
|
match node.tag_name().name() {
|
||||||
|
"Publisher" => config.publisher = String::from(node.text().unwrap_or_default()),
|
||||||
|
"License" => config.license_path = PathBuf::from(node.text().unwrap_or_default()),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_track(track: roxmltree::Node, config: &mut Configuration) {
|
||||||
|
fn parse_file(file: roxmltree::Node) -> File {
|
||||||
|
File{
|
||||||
|
name: String::from(file.attribute("name").unwrap_or_default()),
|
||||||
|
path: PathBuf::from(file.text().unwrap_or_default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_file_system(cur_node: roxmltree::Node, root: &mut Directory) {
|
||||||
|
for node in cur_node.children() {
|
||||||
|
if node.is_element() {
|
||||||
|
match node.tag_name().name() {
|
||||||
|
"File" => root.add_file(parse_file(node)),
|
||||||
|
"Directory" => {
|
||||||
|
let mut new_dir = Directory::new(node.attribute("name").unwrap_or_default());
|
||||||
|
|
||||||
|
parse_file_system(node, &mut new_dir);
|
||||||
|
root.add_dir(new_dir);
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_file_system(track, &mut config.root);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Error(roxmltree::Error);
|
pub struct Error(roxmltree::Error);
|
||||||
|
|
|
@ -36,7 +36,7 @@ fn populate() -> Result<CDDesc, Error> {
|
||||||
Ok(desc)
|
Ok(desc)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_main() -> Result<(), Error> {
|
fn _run_main() -> Result<(), Error> {
|
||||||
let desc = populate()?;
|
let desc = populate()?;
|
||||||
|
|
||||||
println!("\n<== Planschbecken ==>");
|
println!("\n<== Planschbecken ==>");
|
||||||
|
@ -59,7 +59,9 @@ fn run_main() -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_main_xml() -> Result<(), Error> {
|
fn run_main_xml() -> Result<(), Error> {
|
||||||
config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?)
|
let _ = config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in New Issue