Improved error handling
This commit is contained in:
parent
5a8c7fb56c
commit
75c32c98eb
|
@ -8,4 +8,5 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cdtypes = {path = "../cdtypes"}
|
cdtypes = {path = "../cdtypes"}
|
||||||
paste = "*"
|
paste = "*"
|
||||||
|
roxmltree = "*"
|
||||||
tool_helper = {path = "../tool_helper"}
|
tool_helper = {path = "../tool_helper"}
|
|
@ -1,5 +1,6 @@
|
||||||
|
use super::{Error, ErrorString};
|
||||||
mod xml;
|
mod xml;
|
||||||
|
|
||||||
pub fn parse_xml(xml: String) {
|
pub fn parse_xml(xml: String) -> Result<(), Error> {
|
||||||
println!("Wuff: {}", xml);
|
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 config_reader;
|
||||||
pub mod encoder;
|
pub mod encoder;
|
||||||
pub mod file_writer;
|
pub mod file_writer;
|
||||||
|
|
|
@ -59,8 +59,7 @@ fn run_main() -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_main_xml() -> Result<(), Error> {
|
fn run_main_xml() -> Result<(), Error> {
|
||||||
config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?);
|
config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -6,6 +6,10 @@ pub mod raw;
|
||||||
pub type Output = Box<dyn Write>;
|
pub type Output = Box<dyn Write>;
|
||||||
pub type Input = Box<dyn Read>;
|
pub type Input = Box<dyn Read>;
|
||||||
|
|
||||||
|
pub trait ErrorString {
|
||||||
|
fn to_string(self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
pub exit_code: i32,
|
pub exit_code: i32,
|
||||||
pub action: String,
|
pub action: String,
|
||||||
|
@ -27,6 +31,10 @@ impl Error {
|
||||||
Error::from_text(error.to_string())
|
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 {
|
pub fn from_callback<F>(callback: F) -> Error where F: Fn() -> String {
|
||||||
Error::from_text(callback())
|
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> {
|
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)?))
|
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue