Prepare convert function

This commit is contained in:
jaby 2022-09-13 21:19:42 +02:00
parent e42b3ab6cf
commit 4b69d678e8
4 changed files with 23 additions and 15 deletions

View File

View File

@ -1,8 +1,6 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
use tool_helper::{Input, Output, Result};
pub use tool_helper::Error;
pub fn convert(_input: Input, _output: Output) -> Result<()> {
Err(Error::new(-1, "Not implemented yet".to_owned()))
}

View File

@ -14,8 +14,10 @@ struct CommandLine {
fn run_main() -> tool_helper::Result<()> {
match CommandLine::try_parse() {
Ok(cmd) => {
let _output = tool_helper::open_output(cmd.input_file)?;
return Ok(());
let input = tool_helper::open_input(cmd.input_file)?;
let output = tool_helper::open_output(Some(cmd.output_file))?;
return cpp_out::convert(input, output);
},
Err(error) => Err(tool_helper::Error::new(-1, error.to_string()))
}
@ -23,7 +25,7 @@ fn run_main() -> tool_helper::Result<()> {
fn main() {
match run_main() {
Ok(_) => println!("All good"),
Ok(_) => (),
Err(error) => {
eprintln!("{}", error.text);
std::process::exit(error.exit_code);

View File

@ -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())),
}
}