Prepare convert function
This commit is contained in:
parent
44a0142d0a
commit
f87fbe96e9
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -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);
|
||||||
|
|
|
@ -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 input_file {
|
match output_file {
|
||||||
Some(input_path) => Ok(Box::new(Error::from_io_result(std::fs::File::create(input_path))?)),
|
Some(output_path) => Ok(Box::new(Error::from_io_result(std::fs::File::create(output_path))?)),
|
||||||
None => Ok(Box::new(std::io::stdout())),
|
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))?)),
|
||||||
|
None => Ok(Box::new(std::io::stdin())),
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue