Convert configuration to CDDesc
This commit is contained in:
parent
852c908a70
commit
1b0fc42e84
|
@ -1,13 +1,13 @@
|
|||
<ISO_Project>
|
||||
<Description>
|
||||
<Publisher>Jaby Wuff</Publisher>
|
||||
<License>C:/../</License>
|
||||
<License>../Tests/ISO_Planschbecken.xml</License>
|
||||
</Description>
|
||||
<Track>
|
||||
<File name="Miau.png">C:/../</File>
|
||||
<Audiofile>C:/../</Audiofile>
|
||||
<File name="Miau.png">../Tests/ISO_Planschbecken.xml</File>
|
||||
<Audiofile>../Tests/ISO_Planschbecken.xml</Audiofile>
|
||||
<Directory name="Wuff">
|
||||
<File name="Miau.png" type="file">C:/../</File>
|
||||
<File name="Miau.png" type="file">../Tests/ISO_Planschbecken.xml</File>
|
||||
</Directory>
|
||||
</Track>
|
||||
</ISO_Project>
|
|
@ -3,14 +3,14 @@ use std::path::PathBuf;
|
|||
mod xml;
|
||||
|
||||
pub struct Configuration {
|
||||
pub publisher: String,
|
||||
pub license_path: PathBuf,
|
||||
pub publisher: Option<String>,
|
||||
pub license_path: Option<PathBuf>,
|
||||
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<DirMember> {
|
||||
self.member.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
enum DirMember {
|
||||
pub enum DirMember {
|
||||
File(File),
|
||||
Directory(Directory),
|
||||
}
|
||||
|
|
|
@ -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())),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,4 +3,47 @@ pub use tool_helper::{Error, ErrorString};
|
|||
pub mod config_reader;
|
||||
pub mod encoder;
|
||||
pub mod file_writer;
|
||||
pub mod types;
|
||||
pub mod types;
|
||||
|
||||
use tool_helper::read_file;
|
||||
use types::CDDesc;
|
||||
|
||||
pub fn process(config: config_reader::Configuration) -> Result<CDDesc, Error> {
|
||||
let cd_desc = parse_configuration(config)?;
|
||||
|
||||
Ok(cd_desc)
|
||||
}
|
||||
|
||||
fn parse_configuration(config: config_reader::Configuration) -> Result<CDDesc, Error> {
|
||||
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)
|
||||
}
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -134,4 +134,15 @@ pub fn input_to_vec(input: Input) -> Result<Vec<u8>, Error> {
|
|||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub fn read_file(file_path: PathBuf) -> Result<Vec<u8>, 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)))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue