infinity_bridge_wire/
error.rs1use alloc::string::String;
2use core::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ErrorKind {
6 Transport,
7 Timeout,
8 Protocol,
9 Application,
10 NoClients,
11}
12
13impl fmt::Display for ErrorKind {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 match self {
16 Self::Transport => f.write_str("TRANSPORT"),
17 Self::Timeout => f.write_str("TIMEOUT"),
18 Self::Protocol => f.write_str("PROTOCOL"),
19 Self::Application => f.write_str("APPLICATION"),
20 Self::NoClients => f.write_str("NO_CLIENTS"),
21 }
22 }
23}
24
25#[derive(Debug, Clone)]
26pub struct BridgeError {
27 kind: ErrorKind,
28 message: String,
29}
30
31impl BridgeError {
32 pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
33 Self {
34 kind,
35 message: message.into(),
36 }
37 }
38
39 pub fn transport(message: impl Into<String>) -> Self {
40 Self::new(ErrorKind::Transport, message)
41 }
42
43 pub fn timeout(message: impl Into<String>) -> Self {
44 Self::new(ErrorKind::Timeout, message)
45 }
46
47 pub fn protocol(message: impl Into<String>) -> Self {
48 Self::new(ErrorKind::Protocol, message)
49 }
50
51 pub fn application(message: impl Into<String>) -> Self {
52 Self::new(ErrorKind::Application, message)
53 }
54
55 pub fn no_clients(message: impl Into<String>) -> Self {
56 Self::new(ErrorKind::NoClients, message)
57 }
58
59 pub fn kind(&self) -> ErrorKind {
60 self.kind
61 }
62
63 pub fn message(&self) -> &str {
64 &self.message
65 }
66}
67
68impl fmt::Display for BridgeError {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "[{}] {}", self.kind, self.message)
71 }
72}
73
74#[cfg(feature = "std")]
75impl std::error::Error for BridgeError {}
76
77impl From<serde_json::Error> for BridgeError {
78 fn from(e: serde_json::Error) -> Self {
79 Self::protocol(alloc::format!("JSON error: {e}"))
80 }
81}