From a2858e0daf45d760ce1960cc30b8a07793a14c8c Mon Sep 17 00:00:00 2001 From: Jaby Date: Fri, 4 Nov 2022 11:32:55 +0100 Subject: [PATCH] Improved error handling --- src/Tools/psxcdgen_ex/Cargo.toml | 1 + .../psxcdgen_ex/src/config_reader/mod.rs | 5 +++-- .../psxcdgen_ex/src/config_reader/xml.rs | 21 +++++++++++++++++++ src/Tools/psxcdgen_ex/src/lib.rs | 2 ++ src/Tools/psxcdgen_ex/src/main.rs | 3 +-- src/Tools/tool_helper/src/lib.rs | 14 +++++++++++++ 6 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Tools/psxcdgen_ex/Cargo.toml b/src/Tools/psxcdgen_ex/Cargo.toml index 9a398257..ea41679e 100644 --- a/src/Tools/psxcdgen_ex/Cargo.toml +++ b/src/Tools/psxcdgen_ex/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] cdtypes = {path = "../cdtypes"} paste = "*" +roxmltree = "*" tool_helper = {path = "../tool_helper"} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs index 136e07dc..b84ee1e3 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs @@ -1,5 +1,6 @@ +use super::{Error, ErrorString}; mod xml; -pub fn parse_xml(xml: String) { - println!("Wuff: {}", xml); +pub fn parse_xml(xml: String) -> Result<(), Error> { + Ok(xml::parse(xml)?) } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index e69de29b..7d53cea5 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -0,0 +1,21 @@ +use super::ErrorString; + +pub fn parse(xml: String) -> Result<(), Error> { + roxmltree::Document::parse(xml.as_str())?; + + Ok(()) +} + +pub struct Error(roxmltree::Error); + +impl ErrorString for Error { + fn to_string(self) -> String { + self.0.to_string() + } +} + +impl std::convert::From for Error { + fn from(error: roxmltree::Error) -> Error { + Error(error) + } +} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index fe505430..87eadffc 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -1,3 +1,5 @@ +pub use tool_helper::{Error, ErrorString}; + pub mod config_reader; pub mod encoder; pub mod file_writer; diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 72ae1188..489de4db 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -59,8 +59,7 @@ fn run_main() -> Result<(), Error> { } fn run_main_xml() -> Result<(), Error> { - config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?); - Ok(()) + config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?) } fn main() { diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 17261909..548208d7 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -6,6 +6,10 @@ pub mod raw; pub type Output = Box; pub type Input = Box; +pub trait ErrorString { + fn to_string(self) -> String; +} + pub struct Error { pub exit_code: i32, pub action: String, @@ -27,6 +31,10 @@ impl Error { Error::from_text(error.to_string()) } + pub fn from_core_error(error: T) -> Error where T: core::fmt::Display { + Error::from_text(error.to_string()) + } + pub fn from_callback(callback: F) -> Error where F: Fn() -> String { Error::from_text(callback()) } @@ -86,6 +94,12 @@ impl std::convert::From for Error { } } +impl std::convert::From for Error { + fn from(error: T) -> Self { + Error::from_text(error.to_string()) + } +} + pub fn open_output_file(output_path: PathBuf) -> Result, Error> { Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?)) }