Improved error handling

This commit is contained in:
jaby 2022-11-04 11:32:55 +01:00
parent e89d1fb9c7
commit b84622bb61
6 changed files with 42 additions and 4 deletions

View File

@ -8,4 +8,5 @@ edition = "2021"
[dependencies]
cdtypes = {path = "../cdtypes"}
paste = "*"
roxmltree = "*"
tool_helper = {path = "../tool_helper"}

View File

@ -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)?)
}

View File

@ -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<roxmltree::Error> for Error {
fn from(error: roxmltree::Error) -> Error {
Error(error)
}
}

View File

@ -1,3 +1,5 @@
pub use tool_helper::{Error, ErrorString};
pub mod config_reader;
pub mod encoder;
pub mod file_writer;

View File

@ -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() {

View File

@ -6,6 +6,10 @@ pub mod raw;
pub type Output = Box<dyn Write>;
pub type Input = Box<dyn Read>;
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<T>(error: T) -> Error where T: core::fmt::Display {
Error::from_text(error.to_string())
}
pub fn from_callback<F>(callback: F) -> Error where F: Fn() -> String {
Error::from_text(callback())
}
@ -86,6 +94,12 @@ impl std::convert::From<std::convert::Infallible> for Error {
}
}
impl<T: ErrorString> std::convert::From<T> for Error {
fn from(error: T) -> Self {
Error::from_text(error.to_string())
}
}
pub fn open_output_file(output_path: PathBuf) -> Result<BufWriter<std::fs::File>, Error> {
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
}