From 877c047b0d169fe024bf33e65deeeff12649f328 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 8 Nov 2022 21:28:05 +0100 Subject: [PATCH] Convert configuration to CDDesc --- src/Tools/Tests/ISO_Planschbecken.xml | 8 ++-- .../psxcdgen_ex/src/config_reader/mod.rs | 12 +++-- .../psxcdgen_ex/src/config_reader/xml.rs | 4 +- src/Tools/psxcdgen_ex/src/lib.rs | 45 ++++++++++++++++++- src/Tools/psxcdgen_ex/src/main.rs | 2 +- src/Tools/tool_helper/src/lib.rs | 11 +++++ 6 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/Tools/Tests/ISO_Planschbecken.xml b/src/Tools/Tests/ISO_Planschbecken.xml index 8da7a166..24aaf321 100644 --- a/src/Tools/Tests/ISO_Planschbecken.xml +++ b/src/Tools/Tests/ISO_Planschbecken.xml @@ -1,13 +1,13 @@ Jaby Wuff - C:/../ + ../Tests/ISO_Planschbecken.xml - C:/../ - C:/../ + ../Tests/ISO_Planschbecken.xml + ../Tests/ISO_Planschbecken.xml - C:/../ + ../Tests/ISO_Planschbecken.xml \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs index e322a240..893bae7a 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs @@ -3,14 +3,14 @@ use std::path::PathBuf; mod xml; pub struct Configuration { - pub publisher: String, - pub license_path: PathBuf, + pub publisher: Option, + pub license_path: Option, pub root: Directory, } impl Configuration { pub fn new() -> Configuration { - Configuration{publisher: String::new(), license_path: PathBuf::new(), root: Directory::new("root")} + Configuration{publisher: None, license_path: None, root: Directory::new("root")} } } @@ -42,9 +42,13 @@ impl Directory { pub fn add_dir(&mut self, dir: Directory) { self.member.push(DirMember::Directory(dir)); } + + pub fn into_iter(self) -> std::vec::IntoIter { + self.member.into_iter() + } } -enum DirMember { +pub enum DirMember { File(File), Directory(Directory), } diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index 04b9b1d4..9d36ab86 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -33,8 +33,8 @@ 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()), + "Publisher" => config.publisher = Some(String::from(node.text().unwrap_or_default())), + "License" => config.license_path = Some(PathBuf::from(node.text().unwrap_or_default())), _ => () } } diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index 87eadffc..c2310989 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -3,4 +3,47 @@ pub use tool_helper::{Error, ErrorString}; pub mod config_reader; pub mod encoder; pub mod file_writer; -pub mod types; \ No newline at end of file +pub mod types; + +use tool_helper::read_file; +use types::CDDesc; + +pub fn process(config: config_reader::Configuration) -> Result { + let cd_desc = parse_configuration(config)?; + + Ok(cd_desc) +} + +fn parse_configuration(config: config_reader::Configuration) -> Result { + fn parse_dir(dst_dir: &mut types::Directory, src_dir: config_reader::Directory) -> Result<(), Error> { + for member in src_dir.into_iter() { + match member { + config_reader::DirMember::Directory(dir) => { + let mut new_dir = types::Directory::new(dir.name.as_str())?; + + parse_dir(&mut new_dir, dir)?; + dst_dir.add_dir(new_dir); + }, + + config_reader::DirMember::File(file) => { + dst_dir.add_file(types::File::new_regular(file.name.as_str(), read_file(file.path)?)?); + }, + } + } + + Ok(()) + } + + let cd_desc = CDDesc::new(); + + if let Some(publisher) = config.publisher { + cd_desc.pvd.borrow_mut().publisher = publisher; + } + + if let Some(_) = config.license_path { + println!("Warning: Ignoring License path for now"); + } + + parse_dir(&mut cd_desc.root.borrow_mut(), config.root)?; + Ok(cd_desc) +} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index eecb1186..6990014c 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -59,7 +59,7 @@ fn _run_main() -> Result<(), Error> { } fn run_main_xml() -> Result<(), Error> { - let _ = config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?); + let _cd_desc = psxcdgen_ex::process(config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?)?)?; Ok(()) } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 548208d7..b5395f4d 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -134,4 +134,15 @@ pub fn input_to_vec(input: Input) -> Result, Error> { } Ok(data) +} + +pub fn read_file(file_path: PathBuf) -> Result, Error> { + match std::fs::read(&file_path) { + Ok(data) => { + Ok(data) + }, + Err(error) => { + Err(Error::from_text(format!("Failed reading file {} with error: \"{}\"", file_path.display(), error))) + } + } } \ No newline at end of file