Produce first output

This commit is contained in:
2022-09-13 21:35:52 +02:00
parent 4b69d678e8
commit 888a6247a9
5 changed files with 26 additions and 15 deletions

View File

@@ -14,33 +14,35 @@ impl Error {
Error{exit_code, text}
}
pub fn from_io_result_with_code<T>(result: std::result::Result<T, std::io::Error>, exit_code: Option<i32>) -> Result<T> {
pub fn try_with_code<T, S>(result: std::result::Result<T, S>, exit_code: Option<i32>) -> Result<T> where S: std::fmt::Display {
match result {
Ok(value) => Ok(value),
Err(error) => Err(Error::new({
match exit_code {
Some(exit_code) => exit_code,
None => -1,
}
}, error.to_string())),
Err(error) => Err(Error::new(Self::get_exit_code(exit_code), error.to_string())),
}
}
pub fn from_io_result<T>(result: std::result::Result<T, std::io::Error>) -> Result<T> {
Self::from_io_result_with_code(result, None)
pub fn try_with<T, S>(result: std::result::Result<T, S>) -> Result<T> where S: std::fmt::Display {
Self::try_with_code(result, None)
}
fn get_exit_code(exit_code: Option<i32>) -> i32 {
match exit_code {
Some(exit_code) => exit_code,
None => -1,
}
}
}
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))?)),
Some(output_path) => Ok(Box::new(Error::try_with(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::open(input_path))?)),
Some(input_path) => Ok(Box::new(Error::try_with(std::fs::File::open(input_path))?)),
None => Ok(Box::new(std::io::stdin())),
}
}