From ab4d2ca345bdc031aa3c94e1566a154d08a882b7 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 27 Nov 2022 23:54:31 +0100 Subject: [PATCH] Merge common attributes --- src/Tools/Tests/ISO_Planschbecken.xml | 4 +-- .../psxcdgen_ex/src/config_reader/mod.rs | 16 +++++++++-- .../psxcdgen_ex/src/config_reader/xml.rs | 28 +++++++++++++------ src/Tools/psxcdgen_ex/src/lib.rs | 6 ++-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Tools/Tests/ISO_Planschbecken.xml b/src/Tools/Tests/ISO_Planschbecken.xml index c5bf55a0..35fc733c 100644 --- a/src/Tools/Tests/ISO_Planschbecken.xml +++ b/src/Tools/Tests/ISO_Planschbecken.xml @@ -13,8 +13,8 @@ ../Tests/ISO_Planschbecken.xml - - + + Miau.txt Wuff/Miau.txt diff --git a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs index 6f167dd5..30746eee 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs @@ -14,11 +14,21 @@ impl Configuration { } } -pub struct File { +pub struct CommonProperties { pub name: String, - pub path: PathBuf, pub is_hidden: bool, - pub padded_size: Option, + pub padded_size: Option, +} + +pub struct File { + pub common: CommonProperties, + pub path: PathBuf, +} + +pub struct LbaFile { + pub common: CommonProperties, + pub header: Option, + pub entries: Vec } pub struct Directory { diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index 48fd5513..44de6e16 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -2,7 +2,13 @@ use std::path::PathBuf; use tool_helper::format_if_error; use crate::config_reader::Directory; -use super::{Configuration, Error, File}; +use super::{CommonProperties, Configuration, Error, File}; + +mod attribute_names { + pub const NAME: &'static str = "name"; + pub const HIDDEN: &'static str = "hidden"; + pub const PADDED_SIZE: &'static str = "padded_size"; +} pub fn parse(xml: String) -> Result { let mut config = Configuration::new(); @@ -47,10 +53,8 @@ fn parse_description(description: roxmltree::Node, config: &mut Configuration) { fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), Error> { fn parse_file(file: roxmltree::Node, is_hidden: bool) -> Result { Ok(File{ - name: String::from(file.attribute("name").unwrap_or_default()), - path: PathBuf::from(file.text().unwrap_or_default()), - is_hidden: is_hidden | parse_boolean_attribute(&file, "hidden")?, - padded_size: read_padded_size(&file)?, + common: read_common_properties(&file, is_hidden)?, + path: PathBuf::from(file.text().unwrap_or_default()), }) } @@ -60,8 +64,8 @@ fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), match node.tag_name().name() { "File" => root.add_file(parse_file(node, is_hidden)?), "Directory" => { - is_hidden |= parse_boolean_attribute(&node, "hidden")?; - let mut new_dir = Directory::new(node.attribute("name").unwrap_or_default(), is_hidden); + is_hidden |= parse_boolean_attribute(&node, attribute_names::HIDDEN)?; + let mut new_dir = Directory::new(node.attribute(attribute_names::NAME).unwrap_or_default(), is_hidden); parse_file_system(node, &mut new_dir, is_hidden)?; root.add_dir(new_dir); @@ -77,8 +81,16 @@ fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), parse_file_system(track, &mut config.root, false) } +fn read_common_properties(xml: &roxmltree::Node, is_hidden: bool) -> Result { + Ok(CommonProperties{ + name: String::from(xml.attribute(attribute_names::NAME).unwrap_or_default()), + is_hidden: is_hidden | parse_boolean_attribute(&xml, attribute_names::HIDDEN)?, + padded_size: read_padded_size(&xml)? + }) +} + fn read_padded_size(xml: &roxmltree::Node) -> Result, Error> { - if let Some(padded_attr) = xml.attribute("padded_size") { + if let Some(padded_attr) = xml.attribute(attribute_names::PADDED_SIZE) { let padded_size = format_if_error!(padded_attr.parse::(), "Failed reading \"{}\" as padded size: {error_text}", padded_attr)?; Ok(Some(padded_size)) } diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index efcdcfb3..2476dd23 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -114,10 +114,10 @@ fn parse_configuration(config: config_reader::Configuration) -> Result { - let mut desc_file = types::File::new_regular(file.name.as_str(), read_file(file.path)?)?; + let mut desc_file = types::File::new_regular(file.common.name.as_str(), read_file(file.path)?)?; - desc_file.properties.padded_size_bytes = file.padded_size; - desc_file.properties.is_hidden = file.is_hidden; + desc_file.properties.padded_size_bytes = file.common.padded_size; + desc_file.properties.is_hidden = file.common.is_hidden; dst_dir.add_file(desc_file); }, }