This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
pt/members/libpt-log/src/error.rs

93 lines
3.7 KiB
Rust
Raw Normal View History

2023-09-20 20:14:10 +02:00
//! # Error module for [`pt-log`](crate)
2023-09-15 17:15:09 +02:00
//!
2023-09-20 20:14:10 +02:00
//! This module handles errors in logging contexts.
2023-09-15 17:15:09 +02:00
//// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
// we want docs
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
// we want Debug everywhere.
#![warn(missing_debug_implementations)]
// enable clippy's extra lints, the pedantic version
#![warn(clippy::pedantic)]
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
2023-09-20 14:32:25 +02:00
use pyo3::{exceptions::PyException, PyErr};
2023-09-15 17:15:09 +02:00
use tracing::subscriber::SetGlobalDefaultError;
//// TYPES /////////////////////////////////////////////////////////////////////////////////////////
2023-09-20 20:14:10 +02:00
/// a quick alias for a result with a [`Error`]
2023-09-15 17:15:09 +02:00
pub type Result<T> = std::result::Result<T, Error>;
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
//// MACROS ////////////////////////////////////////////////////////////////////////////////////////
//// ENUMS /////////////////////////////////////////////////////////////////////////////////////////
2023-09-20 20:14:10 +02:00
/// ## Errors for the [Logger](super::Logger)
2023-09-15 17:15:09 +02:00
pub enum Error {
/// Bad IO operation
IO(std::io::Error),
/// Various errors raised when the messenger is used in a wrong way
Usage(String),
/// Could not assign logger as the global default
SetGlobalDefaultFail(SetGlobalDefaultError),
}
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Error::IO(value)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
impl From<SetGlobalDefaultError> for Error {
fn from(value: SetGlobalDefaultError) -> Self {
Error::SetGlobalDefaultFail(value)
}
}
2023-09-20 14:32:25 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////
impl Into<PyErr> for Error {
fn into(self) -> PyErr {
match self {
Error::IO(err) => PyException::new_err(format!("LoggerError: IO {err:?}")),
Error::Usage(err) => PyException::new_err(format!("LoggerError: Usage {err}")),
Error::SetGlobalDefaultFail(err) => {
PyException::new_err(format!("LoggerError: SetGlobalDefaultFail {err}"))
}
}
}
}
2023-09-15 17:15:09 +02:00
////////////////////////////////////////////////////////////////////////////////////////////////////
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IO(e) => write!(f, "<IO Error {e:?}>"),
Error::Usage(e) => write!(f, "<Usage Error {e:?}>"),
Error::SetGlobalDefaultFail(e) => write!(f, "<SetGlobalDefaultFail {e:?}>"),
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IO(e) => write!(f, "IO Error {e}"),
Error::Usage(e) => write!(f, "Usage Error {e}"),
Error::SetGlobalDefaultFail(e) => write!(f, "SetGlobalDefaultFail {e}"),
}
}
}
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////