Support command line parameters

This commit is contained in:
jaby 2022-11-11 17:00:10 +01:00
parent ff5bbca3a8
commit 39747613bc
6 changed files with 35 additions and 23 deletions

View File

@ -78,7 +78,7 @@
{
"id": "cargo run args",
"type": "pickString",
"options": ["", "--help", "psx bin-cue -o wuff.bin ../Tests/ISO_Planschbecken.xml"],
"options": ["", "--help", "psx bin-cue -o ../Tests/Test_Planschbecken ../Tests/ISO_Planschbecken.xml"],
"default": "",
"description": "Argument options to pass to cargo run"
}

View File

@ -3,4 +3,10 @@ use super::{file_writer::{SectorWriter}, types::{CDDesc, Error}};
pub mod psx;
pub mod builder;
pub type EncoderFunction = fn(CDDesc, &mut dyn SectorWriter) -> Result<(), Error>;
pub type LbaCalculatorFunction = fn(&mut CDDesc);
pub type ImageEncoderFunction = fn(CDDesc, &mut dyn SectorWriter) -> Result<(), Error>;
pub struct EncodingFunctions {
pub lba_calculator: LbaCalculatorFunction,
pub encoder: ImageEncoderFunction
}

View File

@ -10,8 +10,8 @@ pub struct BinCueWriter {
}
impl BinCueWriter {
pub fn new<T: Write + 'static>(writer: T, cue_path: &PathBuf) -> Result<BinCueWriter, Error> {
Ok(BinCueWriter{bin_out: std::boxed::Box::new(writer), cd_time: Time::cd_start(), cue: Self::default_cue(cue_path)?})
pub fn new<T: Write + 'static>(writer: T, bin_path: &PathBuf) -> Result<BinCueWriter, Error> {
Ok(BinCueWriter{bin_out: std::boxed::Box::new(writer), cd_time: Time::cd_start(), cue: Self::default_cue(bin_path)?})
}
pub fn write_cue<T: Write>(self, dst: T) -> Result<(), Error> {
@ -29,10 +29,10 @@ impl BinCueWriter {
Ok(())
}
fn default_cue(cue_path: &PathBuf) -> Result<Vec<CueSpecifier>, Error> {
fn default_cue(bin_path: &PathBuf) -> Result<Vec<CueSpecifier>, Error> {
let mut cue = Vec::new();
cue.push(CueSpecifier::File{path: get_file_name_from_path_buf(cue_path, "cue output path")?, format: CueFormat::Binary});
cue.push(CueSpecifier::File{path: get_file_name_from_path_buf(bin_path, "bin output path")?, format: CueFormat::Binary});
cue.push(CueSpecifier::Track{number: 1, data_type: CueDataType::Mode2_2352});
cue.push(CueSpecifier::Index{number: 1, time: Time::cue_start()});
Ok(cue)

View File

@ -2,11 +2,13 @@ pub mod bin_cue;
pub use cdtypes::{cd::sector::Sector, types::*};
use super::{encoder::EncoderFunction, types::CDDesc};
use super::{encoder::ImageEncoderFunction, types::CDDesc};
use bin_cue::BinCueWriter;
use clap::ValueEnum;
use std::path::PathBuf;
use tool_helper::{Error, open_output_file};
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum ImageType {
BinCue
}
@ -35,7 +37,7 @@ pub trait SectorWriter {
fn write(&mut self, sector: Sector) -> Result<usize, Error>;
}
pub fn write_image(cd_desc: CDDesc, encoder: EncoderFunction, image_type: ImageType, mut output_path: PathBuf) -> Result<(), Error> {
pub fn write_image(cd_desc: CDDesc, encoder: ImageEncoderFunction, image_type: ImageType, mut output_path: PathBuf) -> Result<(), Error> {
match image_type {
ImageType::BinCue => {
output_path.set_extension("bin");
@ -47,10 +49,10 @@ pub fn write_image(cd_desc: CDDesc, encoder: EncoderFunction, image_type: ImageT
cue_output_path
};
let mut writer = BinCueWriter::new(open_output_file(output_path)?, &cue_output_path)?;
let mut writer = BinCueWriter::new(open_output_file(&output_path)?, &output_path)?;
encoder(cd_desc, &mut writer)?;
writer.write_cue(open_output_file(cue_output_path)?)
writer.write_cue(open_output_file(&cue_output_path)?)
}
}
}

View File

@ -1,6 +1,6 @@
use clap::{Parser, ValueEnum};
use psxcdgen_ex::{encoder::psx::{encode_psx_image, calculate_psx_lbas}, file_writer::{ImageType, write_image}, types::{layout::Layout}, config_reader};
use std::{path::PathBuf, str::FromStr};
use psxcdgen_ex::{encoder::{EncodingFunctions, psx::{encode_psx_image, calculate_psx_lbas}}, file_writer::{ImageType, write_image}, types::{layout::Layout}, config_reader};
use std::{path::PathBuf, };
use tool_helper::Error;
#[derive(Parser)]
@ -10,7 +10,7 @@ struct CommandLine {
system_type: SystemType,
#[clap(value_enum, value_parser)]
output_type: OutputType,
output_type: ImageType,
#[clap(short='o')]
output_file: PathBuf,
@ -24,13 +24,17 @@ enum SystemType {
Psx
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum OutputType {
BinCue
impl SystemType {
pub fn get_encoding_functions(self) -> EncodingFunctions {
match self {
SystemType::Psx => EncodingFunctions{lba_calculator: calculate_psx_lbas, encoder: encode_psx_image}
}
}
}
fn run_main() -> Result<(), Error> {
let desc = psxcdgen_ex::process(config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?)?, calculate_psx_lbas)?;
fn run_main(cmd_line: CommandLine) -> Result<(), Error> {
let encoding_functions = cmd_line.system_type.get_encoding_functions();
let desc = psxcdgen_ex::process(config_reader::parse_xml(std::fs::read_to_string(cmd_line.input_file)?)?, encoding_functions.lba_calculator)?;
println!("\n<== Planschbecken ==>");
for element in desc.get_memory_layout().iter() {
@ -48,13 +52,13 @@ fn run_main() -> Result<(), Error> {
}
println!("\n<== Planschbecken ==>\nStart encoding");
write_image(desc, encode_psx_image, ImageType::BinCue, PathBuf::from_str("planschi")?)
write_image(desc, encoding_functions.encoder, cmd_line.output_type, cmd_line.output_file)
}
fn main() {
match CommandLine::try_parse() {
Ok(_) => {
match run_main() {
Ok(cmd_line) => {
match run_main(cmd_line) {
Ok(_) => println!("\n<== Planschbecken End ==>"),
Err(error) => println!("{}", error)
}

View File

@ -100,13 +100,13 @@ impl<T: ErrorString> std::convert::From<T> for Error {
}
}
pub fn open_output_file(output_path: PathBuf) -> Result<BufWriter<std::fs::File>, Error> {
pub fn open_output_file(output_path: &PathBuf) -> Result<BufWriter<std::fs::File>, Error> {
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
}
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output, Error> {
match output_file {
Some(output_path) => Ok(Box::new(open_output_file(output_path)?)),
Some(output_path) => Ok(Box::new(open_output_file(&output_path)?)),
None => Ok(Box::new(BufWriter::new(std::io::stdout()))),
}
}