Created constructor functions and paniced

This commit is contained in:
Jaby 2022-10-06 21:09:45 +02:00
parent ac3031e9aa
commit 013ab8c82f
4 changed files with 74 additions and 9 deletions

View File

@ -1,4 +1,7 @@
use psxcdgen_ex::types::CDDesc;
fn main() {
let _desc = CDDesc::new();
println!("Planschbecken");
}

View File

@ -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<usize>,
length: Option<usize>,
data: Vec<Data>,
dirs: Vec<Directory>
name: DirectoryName,
properties: Properties,
data: Vec<Data>,
dirs: Vec<Directory>
}
impl Directory {
pub fn new(dir_name: &str) -> Result<Directory, Error> {
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<Data, Error> {
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<DirectoryName, Error> {
Ok(DirectoryName{name: DString::from_str(str)?})
}
}
pub struct FileName {
name: DString<8>,
ext: DString<3>
ext: Option<DString<3>>
}
impl FileName {
pub fn from_str(file_name: &str) -> Result<FileName, Error> {
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<usize>,
length: Option<usize>
}
impl Default for Properties {
fn default() -> Self {
Properties{lba: None, length: None}
}
}

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
paste = "*"
cdtypes = {path = "../cdtypes"}
paste = "*"

View File

@ -74,6 +74,12 @@ impl std::convert::From<std::io::Error> for Error {
}
}
impl std::convert::From<cdtypes::Error> for Error {
fn from(error: cdtypes::Error) -> Self {
Error::from_error(error)
}
}
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output, Error> {
match output_file {
Some(output_path) => Ok(Box::new(std::io::BufWriter::new(std::fs::File::create(output_path)?))),