This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
rust-opensmtpd/opensmtpd/src/errors.rs

57 lines
1.4 KiB
Rust
Raw Normal View History

use crate::entry::Entry;
use crate::event_handlers::EventHandler;
2018-12-29 21:03:33 +01:00
use std::fmt;
pub struct Error {
message: String,
}
impl Error {
pub fn new(msg: &str) -> Self {
Error {
message: msg.to_string(),
}
}
2018-12-29 21:03:33 +01:00
}
2018-12-29 21:03:33 +01:00
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<log::SetLoggerError> for Error {
fn from(error: log::SetLoggerError) -> Self {
Error::new(&format!("Logger 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<T> From<std::sync::mpsc::SendError<EventHandler<T>>> for Error {
fn from(error: std::sync::mpsc::SendError<EventHandler<T>>) -> Self {
Error::new(&format!("IO error: {}", error))
}
}