Prepare convert function

This commit is contained in:
Jaby 2022-09-13 21:19:42 +02:00 committed by Jaby
parent 6e1f1df486
commit c693a950a3
4 changed files with 23 additions and 15 deletions

View File

View File

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

View File

@ -14,8 +14,10 @@ struct CommandLine {
fn run_main() -> tool_helper::Result<()> { fn run_main() -> tool_helper::Result<()> {
match CommandLine::try_parse() { match CommandLine::try_parse() {
Ok(cmd) => { Ok(cmd) => {
let _output = tool_helper::open_output(cmd.input_file)?; let input = tool_helper::open_input(cmd.input_file)?;
return Ok(()); 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())) Err(error) => Err(tool_helper::Error::new(-1, error.to_string()))
} }
@ -23,7 +25,7 @@ fn run_main() -> tool_helper::Result<()> {
fn main() { fn main() {
match run_main() { match run_main() {
Ok(_) => println!("All good"), Ok(_) => (),
Err(error) => { Err(error) => {
eprintln!("{}", error.text); eprintln!("{}", error.text);
std::process::exit(error.exit_code); 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 Output = Box<dyn Write>;
pub type Input = Box<dyn Read>;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
pub struct 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 { match input_file {
Some(input_path) => Ok(Box::new(Error::from_io_result(std::fs::File::create(input_path))?)), Some(input_path) => Ok(Box::new(Error::from_io_result(std::fs::File::open(input_path))?)),
None => Ok(Box::new(std::io::stdout())), None => Ok(Box::new(std::io::stdin())),
} }
} }