Improved error handling
This commit is contained in:
parent
e89d1fb9c7
commit
b84622bb61
|
@ -8,4 +8,5 @@ edition = "2021"
|
|||
[dependencies]
|
||||
cdtypes = {path = "../cdtypes"}
|
||||
paste = "*"
|
||||
roxmltree = "*"
|
||||
tool_helper = {path = "../tool_helper"}
|
|
@ -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)?)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
pub use tool_helper::{Error, ErrorString};
|
||||
|
||||
pub mod config_reader;
|
||||
pub mod encoder;
|
||||
pub mod file_writer;
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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)?))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue