Prepare convert function
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::{boxed::Box, io::Write, path::PathBuf};
|
||||
use std::{boxed::Box, io::{Read, Write}, path::PathBuf};
|
||||
|
||||
pub type Output = Box<dyn Write>;
|
||||
pub type Input = Box<dyn Read>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub struct Error {
|
||||
@@ -30,9 +31,16 @@ impl Error {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_output(input_file: Option<PathBuf>) -> Result<Output> {
|
||||
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output> {
|
||||
match output_file {
|
||||
Some(output_path) => Ok(Box::new(Error::from_io_result(std::fs::File::create(output_path))?)),
|
||||
None => Ok(Box::new(std::io::stdout())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input> {
|
||||
match input_file {
|
||||
Some(input_path) => Ok(Box::new(Error::from_io_result(std::fs::File::create(input_path))?)),
|
||||
None => Ok(Box::new(std::io::stdout())),
|
||||
Some(input_path) => Ok(Box::new(Error::from_io_result(std::fs::File::open(input_path))?)),
|
||||
None => Ok(Box::new(std::io::stdin())),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user