24b332c615
The previous design did not handled errors correctly and was kind of spaghetti code. With the new one, the reader and the dispatcher are clearly separated. The filter will only exit on an error from the reader or if EOF has been reached, any other error is displayed but does not exit the filter, which is required by the API. If the filter must exit, all threads are gracefully stopped.
44 lines
1 KiB
Rust
44 lines
1 KiB
Rust
use crate::entry::Entry;
|
|
|
|
pub struct Error {
|
|
message: String,
|
|
}
|
|
|
|
impl Error {
|
|
pub fn new(msg: &str) -> Self {
|
|
Error {
|
|
message: msg.to_string(),
|
|
}
|
|
}
|
|
|
|
pub fn from_string(msg: &String) -> Self {
|
|
Error::new(&msg)
|
|
}
|
|
|
|
pub fn display(&self) {
|
|
eprintln!("Error: {}", self.message);
|
|
}
|
|
}
|
|
|
|
impl From<std::io::Error> for Error {
|
|
fn from(error: std::io::Error) -> Self {
|
|
Error::from_string(&format!("IO error: {}", error))
|
|
}
|
|
}
|
|
|
|
impl From<nom::Err<&str>> for Error {
|
|
fn from(error: nom::Err<&str>) -> Self {
|
|
let msg = match error {
|
|
nom::Err::Incomplete(_) => "not enough data".to_string(),
|
|
nom::Err::Error(c) => format!("{:?}", c),
|
|
nom::Err::Failure(c) => format!("{:?}", c),
|
|
};
|
|
Error::from_string(&format!("Parsing error: {}", msg))
|
|
}
|
|
}
|
|
|
|
impl From<std::sync::mpsc::SendError<Entry>> for Error {
|
|
fn from(error: std::sync::mpsc::SendError<Entry>) -> Self {
|
|
Error::from_string(&format!("IO error: {}", error))
|
|
}
|
|
}
|