From 48626728e80a31b35220eb6903c30b414aa4d65a Mon Sep 17 00:00:00 2001 From: jaby Date: Thu, 6 Oct 2022 21:09:45 +0200 Subject: [PATCH] Created constructor functions and paniced --- src/Tools/psxcdgen_ex/src/main.rs | 3 ++ src/Tools/psxcdgen_ex/src/types/mod.rs | 71 +++++++++++++++++++++++--- src/Tools/tool_helper/Cargo.toml | 3 +- src/Tools/tool_helper/src/lib.rs | 6 +++ 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 516b3c89..fcbb10c8 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -1,4 +1,7 @@ +use psxcdgen_ex::types::CDDesc; fn main() { + let _desc = CDDesc::new(); + println!("Planschbecken"); } \ 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 813eb8bb..edf425bf 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -1,7 +1,17 @@ use cdtypes::types::cdstring::DString; +use tool_helper::Error; pub struct CDDesc { - root: Directory + pub root: Directory +} + +impl CDDesc { + pub fn new() -> CDDesc { + match Directory::new("root") { + Ok(root) => CDDesc{root}, + Err(error) => panic!("Creating root directory failed with: {}", error) + } + } } pub struct PVD { @@ -9,22 +19,67 @@ pub struct PVD { } pub struct Directory { - name: DirectoryName, - lba: Option, - length: Option, - data: Vec, - dirs: Vec + name: DirectoryName, + properties: Properties, + data: Vec, + dirs: Vec +} + +impl Directory { + pub fn new(dir_name: &str) -> Result { + Ok(Directory{name: DirectoryName::from_str(dir_name)?, properties: Properties::default(), data: Vec::new(), dirs: Vec::new()}) + } } pub struct Data { - name: FileName, + name: FileName, + properties: Properties +} + +impl Data { + pub fn new(file_name: &str) -> Result { + Ok(Data{name: FileName::from_str(file_name)?, properties: Properties::default()}) + } } pub struct DirectoryName { name: DString<8>, } +impl DirectoryName { + pub fn from_str(str: &str) -> Result { + Ok(DirectoryName{name: DString::from_str(str)?}) + } +} + pub struct FileName { name: DString<8>, - ext: DString<3> + ext: Option> +} + +impl FileName { + pub fn from_str(file_name: &str) -> Result { + let (name, ext) = { + let mut sub_str = file_name.split('.'); + (Error::ok_or_new(sub_str.next(), || "File name can't be emplty".to_owned())?, sub_str.next()) + }; + + Ok(FileName{name: DString::from_str(name)?, ext: { + match ext { + Some(ext) => Some(DString::from_str(ext)?), + None => None + } + }}) + } +} + +pub struct Properties { + lba: Option, + length: Option +} + +impl Default for Properties { + fn default() -> Self { + Properties{lba: None, length: None} + } } \ No newline at end of file diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index 4c090cb4..10eac76f 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -paste = "*" \ No newline at end of file +cdtypes = {path = "../cdtypes"} +paste = "*" \ No newline at end of file diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index f77cd044..989adaf3 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -74,6 +74,12 @@ impl std::convert::From for Error { } } +impl std::convert::From for Error { + fn from(error: cdtypes::Error) -> Self { + Error::from_error(error) + } +} + pub fn open_output(output_file: Option) -> Result { match output_file { Some(output_path) => Ok(Box::new(std::io::BufWriter::new(std::fs::File::create(output_path)?))),