Skip to main content

log_io/
error.rs

1//! Crate-level error type.
2
3use std::fmt;
4use std::io;
5
6/// Crate result alias.
7pub type Result<T> = core::result::Result<T, Error>;
8
9/// Errors produced by the logging pipeline.
10#[derive(Debug)]
11pub enum Error {
12    /// An IO error from a sink or writer.
13    Io(io::Error),
14    /// A formatter produced invalid output. This is rare and indicates
15    /// a bug in a custom formatter.
16    Format(fmt::Error),
17    /// Configuration error during builder construction.
18    Configuration(&'static str),
19}
20
21impl fmt::Display for Error {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::Io(e) => write!(f, "io error: {e}"),
25            Self::Format(e) => write!(f, "format error: {e}"),
26            Self::Configuration(msg) => write!(f, "configuration error: {msg}"),
27        }
28    }
29}
30
31impl std::error::Error for Error {
32    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
33        match self {
34            Self::Io(e) => Some(e),
35            Self::Format(e) => Some(e),
36            Self::Configuration(_) => None,
37        }
38    }
39}
40
41impl From<io::Error> for Error {
42    fn from(value: io::Error) -> Self {
43        Self::Io(value)
44    }
45}
46
47impl From<fmt::Error> for Error {
48    fn from(value: fmt::Error) -> Self {
49        Self::Format(value)
50    }
51}