Dump CDDesc
This commit is contained in:
parent
c0e67feca7
commit
dbeb3746bf
|
@ -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<CDDesc, Error> {
|
||||
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)
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
}
|
|
@ -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<DString<3>>
|
||||
|
@ -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<usize>,
|
||||
length: Option<usize>
|
||||
pub lba: Option<usize>,
|
||||
pub length: Option<usize>
|
||||
}
|
||||
|
||||
impl Default for Properties {
|
||||
|
@ -95,3 +127,10 @@ impl Default for Properties {
|
|||
Properties{lba: None, length: None}
|
||||
}
|
||||
}
|
||||
|
||||
fn dstring_as_str<const SIZE: usize>(string: &DString<SIZE>) -> &str {
|
||||
match std::str::from_utf8(string.as_raw()) {
|
||||
Ok(str) => str,
|
||||
Err(_) => "???",
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue