sse_agent/
error.rs

1use std::{error::Error as StdError, fmt};
2
3#[derive(Debug, Clone)]
4pub struct Error<E> {
5    kind: Box<ErrorKind<E>>,
6}
7
8impl<E> Error<E> {
9    pub fn kind(self) -> ErrorKind<E> {
10        *self.kind
11    }
12}
13
14impl<E> fmt::Display for Error<E>
15where
16    E: fmt::Display,
17{
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match self.kind.as_ref() {
20            ErrorKind::Inner(ref err) => write!(f, "Transport error: {}", err),
21            ErrorKind::Sse(err) => write!(f, "Parse error: {}", err),
22        }
23    }
24}
25
26impl<E> StdError for Error<E>
27where
28    E: StdError,
29{
30    fn source(&self) -> Option<&(dyn StdError + 'static)> {
31        match self.kind.as_ref() {
32            ErrorKind::Inner(ref err) => err.source(),
33            ErrorKind::Sse(err) => err.source(),
34        }
35    }
36}
37
38impl<E> Error<E> {
39    pub(crate) fn inner(err: E) -> Self {
40        Self {
41            kind: Box::new(ErrorKind::Inner(err)),
42        }
43    }
44
45    pub(crate) fn parser(err: crate::parser::Error) -> Self {
46        Self {
47            kind: Box::new(ErrorKind::Sse(err)),
48        }
49    }
50}
51
52#[derive(Clone, Debug)]
53pub enum ErrorKind<E> {
54    Sse(crate::parser::Error),
55    Inner(E),
56}