From 66b8ec3c2bde915d4a5124f16070296e705cf611 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 19 Nov 2022 03:16:43 +0100 Subject: [PATCH] Parse hidden flag in XML --- .../psxcdgen_ex/src/config_reader/mod.rs | 7 ++- .../psxcdgen_ex/src/config_reader/xml.rs | 57 ++++++++++++++----- src/Tools/psxcdgen_ex/src/lib.rs | 8 ++- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs index 893bae7a..638487a8 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs @@ -15,13 +15,14 @@ impl Configuration { } pub struct File { - pub name: String, - pub path: PathBuf, + pub name: String, + pub path: PathBuf, + pub is_hidden: bool, } impl File { pub fn new() -> File { - File{name: String::new(), path: PathBuf::new()} + File{name: String::new(), path: PathBuf::new(), is_hidden: false} } } diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index 9d36ab86..181c2ddf 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -10,23 +10,25 @@ pub fn parse(xml: String) -> Result { for node in children { if node.is_element() && node.tag_name().name() == "ISO_Project" { - parse_iso_project(node, &mut config); + parse_iso_project(node, &mut config)?; } } Ok(config) } -fn parse_iso_project(iso_project: roxmltree::Node, config: &mut Configuration) { +fn parse_iso_project(iso_project: roxmltree::Node, config: &mut Configuration) -> Result<(), Error> { 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), + "Track" => parse_track(node, config)?, _ => () } } } + + Ok(()) } fn parse_description(description: roxmltree::Node, config: &mut Configuration) { @@ -41,44 +43,69 @@ fn parse_description(description: roxmltree::Node, config: &mut Configuration) { } } -fn parse_track(track: roxmltree::Node, config: &mut Configuration) { - fn parse_file(file: roxmltree::Node) -> File { - File{ +fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), Error> { + fn parse_file(file: roxmltree::Node) -> Result { + Ok(File{ name: String::from(file.attribute("name").unwrap_or_default()), - path: PathBuf::from(file.text().unwrap_or_default()) - } + path: PathBuf::from(file.text().unwrap_or_default()), + is_hidden: parse_boolean_attribute(&file, "hidden")? + }) } - fn parse_file_system(cur_node: roxmltree::Node, root: &mut Directory) { + fn parse_file_system(cur_node: roxmltree::Node, root: &mut Directory) -> Result<(), Error> { for node in cur_node.children() { if node.is_element() { match node.tag_name().name() { - "File" => root.add_file(parse_file(node)), + "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); + parse_file_system(node, &mut new_dir)?; root.add_dir(new_dir); }, _ => (), } } } + + Ok(()) } - parse_file_system(track, &mut config.root); + parse_file_system(track, &mut config.root) } -pub struct Error(roxmltree::Error); +fn parse_boolean_attribute(xml: &roxmltree::Node, attribute_name: &str) -> Result { + if let Some(bool_str) = xml.attribute(attribute_name) { + match bool_str { + "true" | "1" => Ok(true), + "false" | "0" => Ok(false), + _ => { + return Err(Error::Generic(super::Error::from_text(format!("Attribute \"{}\" expects either true,false or 1,0 as valid attributes - found {}", attribute_name, bool_str)))); + } + } + } + + else { + Ok(false) + } +} + +pub enum Error { + XML(roxmltree::Error), + Generic(super::Error) +} impl ErrorString for Error { fn to_string(self) -> String { - self.0.to_string() + match self { + Self::XML(xml) => xml.to_string(), + Self::Generic(generic) => generic.to_string() + } } } impl std::convert::From for Error { fn from(error: roxmltree::Error) -> Error { - Error(error) + Error::XML(error) } } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index 5c202d8a..cf368208 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -86,7 +86,13 @@ fn parse_configuration(config: config_reader::Configuration) -> Result { - dst_dir.add_file(types::File::new_regular(file.name.as_str(), read_file(file.path)?)?); + let desc_file = types::File::new_regular(file.name.as_str(), read_file(file.path)?)?; + + if file.is_hidden { + println!("Hidding not supported yet"); + } + //desc_file.properties.is_hidden = file.is_hidden; + dst_dir.add_file(desc_file); }, } }