Skip to main content

tracing_reload/
error.rs

1#![expect(missing_docs, reason = "We'll do it later.")]
2
3use std::{error, fmt};
4
5/// Indicates that an error occurred when accessing a reloadable layer.
6#[derive(Debug)]
7pub struct Error {
8    kind: ErrorKind,
9}
10
11#[derive(Debug)]
12pub(crate) enum ErrorKind {
13    SubscriberGone,
14    SubscriberNotInitialized,
15    Poisoned,
16}
17
18impl Error {
19    pub(crate) fn subscriber_gone() -> Self {
20        Self {
21            kind: ErrorKind::SubscriberGone,
22        }
23    }
24
25    pub(crate) fn subscriber_not_initialized() -> Self {
26        Self {
27            kind: ErrorKind::SubscriberNotInitialized,
28        }
29    }
30
31    pub(crate) fn poisoned() -> Self {
32        Self {
33            kind: ErrorKind::Poisoned,
34        }
35    }
36
37    #[must_use]
38    pub fn is_poisoned(&self) -> bool {
39        matches!(self.kind, ErrorKind::Poisoned)
40    }
41
42    #[must_use]
43    pub fn is_gone(&self) -> bool {
44        matches!(self.kind, ErrorKind::SubscriberGone)
45    }
46
47    #[must_use]
48    pub fn is_uninitialized(&self) -> bool {
49        matches!(self.kind, ErrorKind::SubscriberNotInitialized)
50    }
51}
52
53impl fmt::Display for Error {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        let msg = match self.kind {
56            ErrorKind::SubscriberGone => "subscriber no longer exists",
57            ErrorKind::SubscriberNotInitialized => "subscriber was not initialized",
58            ErrorKind::Poisoned => "lock poisoned",
59        };
60        f.pad(msg)
61    }
62}
63
64impl error::Error for Error {}