Use procedural macros to define events

The construction of an EventHandler object should not be directly done
by the client. Instead, it is easier to use procedural macro to
automatize the process, hence exposing a nice and simple interface. Such
use of procedural macros requires to crate an additional crate.
This commit is contained in:
Rodolphe Breard 2019-01-06 15:41:30 +01:00
parent ccda4b1517
commit 789455668c
17 changed files with 1190 additions and 76 deletions

View file

@ -1,50 +0,0 @@
use crate::entry::Entry;
use crate::event_handlers::EventHandler;
use std::fmt;
pub struct Error {
message: String,
}
impl Error {
pub fn new(msg: &str) -> Self {
Error {
message: msg.to_string(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::new(&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::new(&format!("Parsing error: {}", msg))
}
}
impl From<std::sync::mpsc::SendError<Entry>> for Error {
fn from(error: std::sync::mpsc::SendError<Entry>) -> Self {
Error::new(&format!("IO error: {}", error))
}
}
impl From<std::sync::mpsc::SendError<EventHandler>> for Error {
fn from(error: std::sync::mpsc::SendError<EventHandler>) -> Self {
Error::new(&format!("IO error: {}", error))
}
}