From d687898c54b5e61e930583689a75e9e6718d402b Mon Sep 17 00:00:00 2001 From: jaby Date: Fri, 7 Oct 2022 16:12:12 +0200 Subject: [PATCH] Dump CDDesc --- src/Tools/psxcdgen_ex/src/main.rs | 32 ++++++++++++--- src/Tools/psxcdgen_ex/src/types/iterator.rs | 5 ++- src/Tools/psxcdgen_ex/src/types/mod.rs | 43 ++++++++++++++++++++- 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 1290d203..570e09a7 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -1,24 +1,46 @@ -use psxcdgen_ex::types::{CDDesc, Data}; +use psxcdgen_ex::types::{iterator::{DirectoryIterator, DirectoryIteratorElement}, CDDesc, Data, Directory}; use tool_helper::Error; fn populate() -> Result { - let mut desc = CDDesc::new(); - let file = Data::new("Planschi")?; + let mut desc = CDDesc::new(); + let file = Data::new("Planschi.jpg")?; + let file2 = Data::new("Wuff.png")?; + let folder = { + let mut folder = Directory::new("Sub")?; + let file = Data::new("Schwimm.jpg")?; + let file2 = Data::new("Miau.png")?; + folder.add_data(file); + folder.add_data(file2); + folder + }; + + desc.root.add_dir(folder); desc.root.add_data(file); + desc.root.add_data(file2); Ok(desc) } fn run_main() -> Result<(), Error> { + fn dump_dir(dir: DirectoryIterator) { + for element in dir { + match element { + DirectoryIteratorElement::Root(root) => println!("{}", root), + DirectoryIteratorElement::Data(data) => println!("{}", data), + DirectoryIteratorElement::Directory(dir) => dump_dir(dir.iter()), + } + } + + } let desc = populate()?; - desc.root.iter(); + dump_dir(desc.root.iter()); Ok(()) } fn main() { match run_main() { - Ok(_) => println!("Planschbecken"), + Ok(_) => println!("\n<== Planschbecken ==>"), Err(error) => println!("{}", error) } } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/types/iterator.rs b/src/Tools/psxcdgen_ex/src/types/iterator.rs index 987507af..f5ad3b90 100644 --- a/src/Tools/psxcdgen_ex/src/types/iterator.rs +++ b/src/Tools/psxcdgen_ex/src/types/iterator.rs @@ -18,7 +18,7 @@ impl<'a> std::iter::Iterator for DirectoryIterator<'a> { match std::mem::replace(&mut self.state, DirectoryIteratorState::Done) { DirectoryIteratorState::Root => { self.state = DirectoryIteratorState::Data(self.parent.data.iter()); - return Some(DirectoryIteratorElement::Directory(&self.parent)); + return Some(DirectoryIteratorElement::Root(&self.parent)); } DirectoryIteratorState::Data(mut iter) => { if let Some(data) = iter.next() { @@ -56,6 +56,7 @@ enum DirectoryIteratorState<'a> { } pub enum DirectoryIteratorElement<'a> { - Directory(&'a Directory), + Root(&'a Directory), Data(&'a Data), + Directory(&'a Directory), } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index 3ca87ce9..5ce26ad3 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -1,3 +1,5 @@ +use std::fmt::write; + use iterator::DirectoryIterator; use cdtypes::types::cdstring::DString; use tool_helper::Error; @@ -41,6 +43,12 @@ impl Directory { } } +impl std::fmt::Display for Directory { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Directory: {}", self.name) + } +} + pub struct Data { name: FileName, properties: Properties @@ -52,6 +60,12 @@ impl Data { } } +impl std::fmt::Display for Data { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "File: {}", self.name) + } +} + pub struct DirectoryName { name: DString<8>, } @@ -63,6 +77,12 @@ impl DirectoryName { } } +impl std::fmt::Display for DirectoryName { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", dstring_as_str(&self.name)) + } +} + pub struct FileName { name: DString<8>, ext: Option> @@ -85,9 +105,21 @@ impl FileName { } } +impl std::fmt::Display for FileName { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if let Some(ext) = &self.ext { + write!(f, "{}.{};1", dstring_as_str(&self.name), dstring_as_str(&ext)) + } + + else { + write!(f, "{}", dstring_as_str(&self.name)) + } + } +} + pub struct Properties { - lba: Option, - length: Option + pub lba: Option, + pub length: Option } impl Default for Properties { @@ -95,3 +127,10 @@ impl Default for Properties { Properties{lba: None, length: None} } } + +fn dstring_as_str(string: &DString) -> &str { + match std::str::from_utf8(string.as_raw()) { + Ok(str) => str, + Err(_) => "???", + } +} \ No newline at end of file