Skip to main content

taba/rpc/
error.rs

1use core::fmt;
2
3/// Failed to answer a send or poll.
4#[derive(Debug)]
5pub struct Error(Problem);
6
7#[derive(Debug)]
8enum Problem {
9    Ended,
10    Malformed(serde_json::Error),
11}
12
13impl Error {
14    /// The request's session has ended, or isn't at its address.
15    pub(crate) fn ended() -> Self {
16        Self(Problem::Ended)
17    }
18
19    /// A body that isn't taba's.
20    pub(crate) fn malformed(error: serde_json::Error) -> Self {
21        Self(Problem::Malformed(error))
22    }
23}
24
25impl fmt::Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match &self.0 {
28            Problem::Ended => f.write_str("this session has ended"),
29            Problem::Malformed(error) => write!(f, "can't read what was sent: {error}"),
30        }
31    }
32}
33
34impl core::error::Error for Error {}