// Copyright (c) 2019 Rodolphe Bréard // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. 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 for Error { fn from(error: std::io::Error) -> Self { Error::new(&format!("IO error: {}", error)) } } impl From for Error { fn from(error: log::SetLoggerError) -> Self { Error::new(&format!("Logger error: {}", error)) } } impl From> 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> for Error { fn from(error: std::sync::mpsc::SendError) -> Self { Error::new(&format!("IO error: {}", error)) } } impl From>> for Error { fn from(error: std::sync::mpsc::SendError>) -> Self { Error::new(&format!("IO error: {}", error)) } }