1use std::fmt::{self, Display, Formatter};
4
5pub type RpcResult<T> = Result<T, RpcError>;
6
7#[derive(Debug)]
9pub enum RpcError {
10 NoSuchService(String),
12 Timeout,
18 AlreadyConnected,
20 NotConnected,
22 ConnectionFault(String),
24 EncoderFault(String),
26 Other(String),
28}
29
30impl Display for RpcError {
31 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
32 write!(
33 f,
34 "{}",
35 match self {
36 Self::NoSuchService(s) => format!("The requested service {} does not exist!", s),
37 Self::Timeout => "The requested operation took too long (timeout)!".into(),
38 Self::AlreadyConnected =>
39 "Tried connecting to an already connected component!".into(),
40 Self::NotConnected =>
41 "Tried to perform an action that needs a component connection!".into(),
42 Self::ConnectionFault(s) => format!("I/O error: {}", s),
43 Self::EncoderFault(s) => format!("Encode error: {}", s),
44 Self::Other(s) => format!("Unknown error: {}", s),
45 }
46 )
47 }
48}
49
50impl From<std::io::Error> for RpcError {
51 fn from(e: std::io::Error) -> Self {
52 Self::ConnectionFault(e.to_string())
53 }
54}
55
56impl From<capnp::Error> for RpcError {
57 fn from(e: capnp::Error) -> Self {
58 Self::EncoderFault(e.to_string())
59 }
60}
61
62impl From<async_std::future::TimeoutError> for RpcError {
63 fn from(_: async_std::future::TimeoutError) -> Self {
64 Self::Timeout
65 }
66}