Prepare bin/cue writer

This commit is contained in:
Jaby
2022-10-18 21:00:20 +02:00
parent 7aa2f4851e
commit fe5c70507a
8 changed files with 70 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
use std::{boxed::Box, io::{Read, Write}, path::PathBuf};
use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf};
pub mod bits;
pub mod raw;
@@ -80,17 +80,27 @@ impl std::convert::From<cdtypes::Error> for Error {
}
}
impl std::convert::From<std::convert::Infallible> for Error {
fn from(error: std::convert::Infallible) -> Self {
Error::from_error(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(std::io::BufWriter::new(std::fs::File::create(output_path)?))),
None => Ok(Box::new(std::io::BufWriter::new(std::io::stdout()))),
Some(output_path) => Ok(Box::new(open_output_file(output_path)?)),
None => Ok(Box::new(BufWriter::new(std::io::stdout()))),
}
}
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> {
match input_file {
Some(input_path) => Ok(Box::new(std::io::BufReader::new(std::fs::File::open(input_path)?))),
None => Ok(Box::new(std::io::BufReader::new(std::io::stdin()))),
Some(input_path) => Ok(Box::new(BufReader::new(std::fs::File::open(input_path)?))),
None => Ok(Box::new(BufReader::new(std::io::stdin()))),
}
}