Improve Error
This commit is contained in:
parent
80c6271d0d
commit
e79df423b0
|
@ -92,11 +92,12 @@ pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<(
|
|||
}
|
||||
};
|
||||
|
||||
let action = String::from("Writing source file");
|
||||
if is_source_file {
|
||||
Error::try_or_new(write_source_file(input, &mut output, declarations, cfg.data_name, line_feed), None)
|
||||
Error::try_or_new_with_action(action, write_source_file(input, &mut output, declarations, cfg.data_name, line_feed))
|
||||
}
|
||||
|
||||
else {
|
||||
Error::try_or_new(write_header_file(input, &mut output, cfg.file_name, declarations, cfg.data_name, line_feed), None)
|
||||
Error::try_or_new_with_action(action, write_header_file(input, &mut output, cfg.file_name, declarations, cfg.data_name, line_feed))
|
||||
}
|
||||
}
|
|
@ -15,9 +15,9 @@ struct CommandLine {
|
|||
output_file: PathBuf,
|
||||
}
|
||||
|
||||
fn configurate(cmd: &mut CommandLine) -> tool_helper::Result<Configuration> {
|
||||
fn configurate(cmd: &mut CommandLine) -> Result<Configuration, Error> {
|
||||
let file_name = tool_helper::get_file_name_from_path_buf(&cmd.output_file, "output")?;
|
||||
let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), ||"File extension required for output".to_owned(), None)?, "extension")?.to_ascii_lowercase();
|
||||
let extension = tool_helper::os_str_to_string(Error::ok_or_new(cmd.output_file.extension(), ||"File extension required for output".to_owned())?, "extension")?.to_ascii_lowercase();
|
||||
let file_type = Error::ok_or_new({
|
||||
match extension.as_ref() {
|
||||
"h" => Some(FileType::CHeader),
|
||||
|
@ -26,12 +26,12 @@ fn configurate(cmd: &mut CommandLine) -> tool_helper::Result<Configuration> {
|
|||
"cpp" => Some(FileType::CPPSource),
|
||||
_ => None
|
||||
}
|
||||
}, ||format!("{} unkown file extension", extension), None)?;
|
||||
}, ||format!("{} unkown file extension", extension))?;
|
||||
|
||||
Ok(Configuration{file_name, data_name: std::mem::take(&mut cmd.data_name), line_feed: cpp_out::LineFeed::Windows, file_type})
|
||||
}
|
||||
|
||||
fn run_main() -> tool_helper::Result<()> {
|
||||
fn run_main() -> Result<(), Error> {
|
||||
match CommandLine::try_parse() {
|
||||
Ok(mut cmd) => {
|
||||
let cfg = configurate(&mut cmd)?;
|
||||
|
@ -40,7 +40,7 @@ fn run_main() -> tool_helper::Result<()> {
|
|||
|
||||
return cpp_out::convert(cfg, input, output);
|
||||
},
|
||||
Err(error) => Err(tool_helper::Error::new(error.to_string(), None))
|
||||
Err(error) => Err(tool_helper::Error::from_error(error))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,7 @@ fn main() {
|
|||
match run_main() {
|
||||
Ok(_) => (),
|
||||
Err(error) => {
|
||||
eprintln!("{}", error.text);
|
||||
eprintln!("{}", error);
|
||||
std::process::exit(error.exit_code);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,5 +19,5 @@ pub struct Arguments {
|
|||
pub fn convert(_args: Arguments, input: Input, _output: Output) -> Result<(), Error> {
|
||||
ImageReader::new(Cursor::new(tool_helper::input_to_vec(input)?));
|
||||
|
||||
Err(Error::new("Convert not implemented yet".to_owned(), Some(-2)))
|
||||
Err(Error::not_implemented("convert"))
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
use jaby_engine_fconv::images::{*};
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
use tool_helper::Error;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[clap(about = "Converts files to various JabyEngine related file formats", long_about = None)]
|
||||
|
@ -20,7 +21,7 @@ enum SubCommands {
|
|||
SimpleTIM(reduced_tim::Arguments)
|
||||
}
|
||||
|
||||
fn run_main() -> tool_helper::Result<()> {
|
||||
fn run_main() -> Result<(), Error> {
|
||||
match CommandLine::try_parse() {
|
||||
Ok(cmd) => {
|
||||
let input = tool_helper::open_input(cmd.input_file)?;
|
||||
|
@ -30,7 +31,7 @@ fn run_main() -> tool_helper::Result<()> {
|
|||
SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, output),
|
||||
}
|
||||
},
|
||||
Err(error) => Err(tool_helper::Error::new(error.to_string(), None))
|
||||
Err(error) => Err(Error::from_error(error))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,64 +2,89 @@ 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 {
|
||||
pub exit_code: i32,
|
||||
pub text: String,
|
||||
pub exit_code: i32,
|
||||
pub action: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn new(text: String, exit_code: Option<i32>) -> Error {
|
||||
Error{exit_code: Self::get_exit_code(exit_code), text}
|
||||
const DEFAULT_EXITCODE:i32 = -1;
|
||||
|
||||
pub fn from_text(text: String) -> Error {
|
||||
Error{exit_code: Self::DEFAULT_EXITCODE, action: String::new(), text}
|
||||
}
|
||||
|
||||
pub fn try_or_new<T, S>(result: std::result::Result<T, S>, exit_code: Option<i32>) -> Result<T> where S: std::fmt::Display {
|
||||
pub fn from_error<T>(error: T) -> Error where T: std::fmt::Display {
|
||||
Error::from_text(error.to_string())
|
||||
}
|
||||
|
||||
pub fn not_implemented(function: &str) -> Error {
|
||||
Error::from_text(format!("{} not implemented yet", function))
|
||||
}
|
||||
|
||||
pub fn try_or_new<T, S>(result: std::result::Result<T, S>) -> Result<T, Error> where S: std::fmt::Display {
|
||||
Self::try_or_new_with_action(String::new(), result)
|
||||
}
|
||||
|
||||
pub fn try_or_new_with_action<T, S>(action: String, result: std::result::Result<T, S>) -> Result<T, Error> where S: std::fmt::Display {
|
||||
match result {
|
||||
Ok(value) => Ok(value),
|
||||
Err(error) => Err(Error::new(error.to_string(), exit_code)),
|
||||
Err(error) => Err(Error{exit_code: Self::DEFAULT_EXITCODE, action, text: error.to_string()}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ok_or_new<T, F>(option: Option<T>, error_text: F, code: Option<i32>) -> Result<T> where F: Fn () -> String{
|
||||
Ok(option.ok_or(Error::new(error_text(), code))?)
|
||||
}
|
||||
|
||||
fn get_exit_code(exit_code: Option<i32>) -> i32 {
|
||||
match exit_code {
|
||||
Some(exit_code) => exit_code,
|
||||
None => -1,
|
||||
}
|
||||
pub fn ok_or_new<T, F>(option: Option<T>, error_text: F) -> Result<T, Error> where F: Fn () -> String{
|
||||
Ok(option.ok_or(Error{exit_code: Self::DEFAULT_EXITCODE, action: String::new(), text: error_text()})?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output> {
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if self.action.is_empty() {
|
||||
write!(f, "{}", self.text)
|
||||
}
|
||||
|
||||
else {
|
||||
write!(f, "{}:{}", self.action, self.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::From<std::io::Error> for Error {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
Error{exit_code: -1, action: String::new(), text: error.to_string()}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output, Error> {
|
||||
match output_file {
|
||||
Some(output_path) => Ok(Box::new(Error::try_or_new(std::fs::File::create(output_path), None)?)),
|
||||
Some(output_path) => Ok(Box::new(std::fs::File::create(output_path)?)),
|
||||
None => Ok(Box::new(std::io::stdout())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input> {
|
||||
pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> {
|
||||
match input_file {
|
||||
Some(input_path) => Ok(Box::new(Error::try_or_new(std::fs::File::open(input_path), None)?)),
|
||||
Some(input_path) => Ok(Box::new(std::fs::File::open(input_path)?)),
|
||||
None => Ok(Box::new(std::io::stdin())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String> {
|
||||
Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name), None)?.to_owned())
|
||||
pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result<String, Error> {
|
||||
Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name))?.to_owned())
|
||||
}
|
||||
|
||||
pub fn get_file_name_from_path_buf(input: &PathBuf, name: &str) -> Result<String> {
|
||||
os_str_to_string(Error::ok_or_new(input.file_name(), ||format!("No {} file name found", name), None)?, name)
|
||||
pub fn get_file_name_from_path_buf(input: &PathBuf, name: &str) -> Result<String, Error> {
|
||||
os_str_to_string(Error::ok_or_new(input.file_name(), ||format!("No {} file name found", name))?, name)
|
||||
}
|
||||
|
||||
pub fn input_to_vec(input: Input) -> Result<Vec<u8>> {
|
||||
pub fn input_to_vec(input: Input) -> Result<Vec<u8>, Error> {
|
||||
let mut data = Vec::new();
|
||||
|
||||
for byte in input.bytes() {
|
||||
data.push(Error::try_or_new(byte, None)?);
|
||||
data.push(byte?);
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
|
|
Loading…
Reference in New Issue