Skip to main content

acktor_ipc/codec/
errors.rs

1use std::borrow::Cow;
2
3use thiserror::Error;
4
5use crate::remote_message::ToRemoteMessageRecipientError;
6
7/// Error type used by [`Encode`][crate::codec::Encode].
8#[derive(Debug, Error)]
9pub enum EncodeError {
10    #[error(transparent)]
11    ProstEncodeError(#[from] prost::EncodeError),
12
13    #[error("remote address cannot be encoded in a remote message")]
14    EncodeRemoteAddress,
15
16    #[error(transparent)]
17    ToRemoteMessageRecipientError(#[from] ToRemoteMessageRecipientError),
18
19    #[error("missing encode context")]
20    MissingEncodeContext,
21
22    #[error("could not encode the message: {description}")]
23    Other { description: Cow<'static, str> },
24}
25
26impl From<&'static str> for EncodeError {
27    fn from(description: &'static str) -> Self {
28        EncodeError::Other {
29            description: description.into(),
30        }
31    }
32}
33
34impl From<String> for EncodeError {
35    fn from(description: String) -> Self {
36        EncodeError::Other {
37            description: description.into(),
38        }
39    }
40}
41
42/// Error type used by [`Decode`][crate::codec::Decode].
43#[derive(Debug, Error)]
44pub enum DecodeError {
45    #[error(transparent)]
46    ProstDecodeError(#[from] prost::DecodeError),
47
48    #[error("could not decode the message: {description}")]
49    ZerocopyError { description: Cow<'static, str> },
50
51    #[error("remote message should not contain a remote address")]
52    DecodeRemoteAddress,
53
54    #[error("missing decode context")]
55    MissingDecodeContext,
56
57    #[error("unknown message id: {0}")]
58    UnknownMessageId(u64),
59
60    #[error("could not decode the message: {description}")]
61    Other { description: Cow<'static, str> },
62}
63
64impl From<&'static str> for DecodeError {
65    fn from(description: &'static str) -> Self {
66        DecodeError::Other {
67            description: description.into(),
68        }
69    }
70}
71
72impl From<String> for DecodeError {
73    fn from(description: String) -> Self {
74        DecodeError::Other {
75            description: description.into(),
76        }
77    }
78}
79
80impl<A, S, V> From<zerocopy::ConvertError<A, S, V>> for DecodeError
81where
82    A: std::fmt::Display + std::fmt::Debug,
83    S: std::fmt::Display + std::fmt::Debug,
84    V: std::fmt::Display + std::fmt::Debug,
85{
86    fn from(err: zerocopy::ConvertError<A, S, V>) -> Self {
87        DecodeError::ZerocopyError {
88            description: err.to_string().into(),
89        }
90    }
91}
92
93impl<Src, Dst> From<zerocopy::AlignmentError<Src, Dst>> for DecodeError
94where
95    Src: std::ops::Deref,
96    Dst: zerocopy::KnownLayout + ?Sized,
97{
98    fn from(err: zerocopy::AlignmentError<Src, Dst>) -> Self {
99        DecodeError::ZerocopyError {
100            description: err.to_string().into(),
101        }
102    }
103}
104
105impl<Src, Dst> From<zerocopy::SizeError<Src, Dst>> for DecodeError
106where
107    Src: std::ops::Deref,
108    Dst: zerocopy::KnownLayout + ?Sized,
109{
110    fn from(err: zerocopy::SizeError<Src, Dst>) -> Self {
111        DecodeError::ZerocopyError {
112            description: err.to_string().into(),
113        }
114    }
115}
116
117impl<Src, Dst> From<zerocopy::ValidityError<Src, Dst>> for DecodeError
118where
119    Dst: zerocopy::KnownLayout + zerocopy::TryFromBytes + ?Sized,
120{
121    fn from(err: zerocopy::ValidityError<Src, Dst>) -> Self {
122        DecodeError::ZerocopyError {
123            description: err.to_string().into(),
124        }
125    }
126}