1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Error types.

use crate::core::message::Message;
use crate::core::{DeserializeError, SerializeError, SerializedValue};
use thiserror::Error;

/// Error when connecting to a broker.
#[derive(Error, Debug, Clone)]
pub enum ConnectError<T> {
    /// The protocol version of the broker is incompatible.
    #[error("incompatible protocol version")]
    IncompatibleVersion,

    /// An unexpected message was received.
    ///
    /// This is usually an indication of a bug in the broker.
    #[error("unexpected message received")]
    UnexpectedMessageReceived(Message),

    /// The transport returned an error.
    ///
    /// This error indicates some issue with the lower-level transport mechanism, e.g. an I/O error.
    #[error(transparent)]
    Transport(T),

    /// The broker rejected the connection.
    #[error("connection rejected")]
    Rejected(Option<SerializedValue>),

    /// A value failed to serialize.
    #[error(transparent)]
    Serialize(#[from] SerializeError),

    /// A value failed to deserialize.
    #[error(transparent)]
    Deserialize(#[from] DeserializeError),
}

/// Error while running a client.
#[derive(Error, Debug, Clone)]
pub enum RunError<T> {
    /// An unexpected message was received.
    #[error("unexpected message received")]
    UnexpectedMessageReceived(Message),

    /// The transport returned an error.
    ///
    /// This error indicates some issue with the lower-level transport mechanism, e.g. an I/O error.
    #[error(transparent)]
    Transport(#[from] T),

    /// A value failed to serialize.
    #[error(transparent)]
    Serialize(SerializeError),

    /// A value failed to deserialize.
    #[error(transparent)]
    Deserialize(DeserializeError),
}

/// Standard error type used for most functions.
#[derive(Error, Debug, Copy, Clone, PartialEq, Eq)]
pub enum Error {
    /// The client has shut down.
    #[error("client shut down")]
    Shutdown,

    /// An object could not be created because its UUID exists already.
    #[error("duplicate object")]
    DuplicateObject,

    /// An invalid object id was used.
    ///
    /// This typically indicates that the object was destroyed.
    #[error("invalid object")]
    InvalidObject,

    /// An service could not be created because its UUID exists already on the object.
    #[error("duplicate service")]
    DuplicateService,

    /// An invalid service id was used.
    ///
    /// This typically indicates that the service or owning object was destroyed.
    #[error("invalid service")]
    InvalidService,

    /// An invalid function was called.
    ///
    /// This can indicate a schema mismatch.
    #[error(transparent)]
    InvalidFunction(#[from] InvalidFunction),

    /// Invalid arguments were supplied to a function or event.
    ///
    /// This can indicate a schema mismatch.
    #[error(transparent)]
    InvalidArguments(#[from] InvalidArguments),

    /// A call was aborted.
    #[error("call aborted")]
    CallAborted,

    /// A field that is required for some type is missing.
    #[error(transparent)]
    RequiredFieldMissing(#[from] RequiredFieldMissing),

    /// An invalid reply was received for a call.
    ///
    /// This can indicate a schema mismatch.
    #[error(transparent)]
    InvalidReply(#[from] InvalidReply),

    /// An invalid channel was used.
    #[error("invalid channel")]
    InvalidChannel,

    /// An invalid item was received on a channel.
    ///
    /// This can indicate a schema mismatch.
    #[error(transparent)]
    InvalidItem(#[from] InvalidItem),

    /// An invalid bus was used.
    #[error("invalid bus listener")]
    InvalidBusListener,

    /// A bus listener was started, that is already started.
    #[error("bus listener already started")]
    BusListenerAlreadyStarted,

    /// A bus listener was stopped, that is already stopped.
    #[error("bus listener not started")]
    BusListenerNotStarted,

    /// An invalid lifetime was used.
    #[error("invalid lifetime")]
    InvalidLifetime,

    /// A value failed to serialized.
    #[error(transparent)]
    Serialize(#[from] SerializeError),

    /// The negotiated protocol version is too low.
    #[error("not supported")]
    NotSupported,
}

impl Error {
    /// Creates a new `InvalidFunction` error.
    pub fn invalid_function(function: u32) -> Self {
        Self::InvalidFunction(InvalidFunction::new(function))
    }

    /// Creates a new `InvalidArguments` error.
    pub fn invalid_arguments(id: u32, source: Option<DeserializeError>) -> Self {
        Self::InvalidArguments(InvalidArguments::new(id, source))
    }

    /// Creates a new `RequiredFieldMissing` error.
    pub fn required_field_missing(field: u32) -> Self {
        Self::RequiredFieldMissing(RequiredFieldMissing::new(field))
    }

    /// Creates a new `InvalidReply` error.
    pub fn invalid_reply(source: DeserializeError) -> Self {
        Self::InvalidReply(InvalidReply::new(source))
    }

    /// Creates a new `InvalidItem` error.
    pub fn invalid_item(source: DeserializeError) -> Self {
        Self::InvalidItem(InvalidItem::new(source))
    }
}

/// An invalid function was called.
///
/// This can indicate a schema mismatch.
#[derive(Error, Debug, Copy, Clone, PartialEq, Eq)]
#[error("invalid function {} called", .function)]
pub struct InvalidFunction {
    function: u32,
}

impl InvalidFunction {
    /// Creates a new `InvalidFunction` error.
    pub fn new(function: u32) -> Self {
        Self { function }
    }

    /// Returns the id of the invalid function.
    pub fn function(self) -> u32 {
        self.function
    }
}

/// Invalid arguments were supplied to a function or event.
///
/// This can indicate a schema mismatch.
#[derive(Error, Debug, Copy, Clone, PartialEq, Eq)]
#[error("invalid arguments for function or event {}", .id)]
pub struct InvalidArguments {
    id: u32,
    source: Option<DeserializeError>,
}

impl InvalidArguments {
    /// Creates a new `InvalidArguments` error.
    pub fn new(id: u32, source: Option<DeserializeError>) -> Self {
        Self { id, source }
    }

    /// Returns the id of the function or event.
    pub fn id(self) -> u32 {
        self.id
    }
}

/// A field that is required for some type is missing.
#[derive(Error, Debug, Copy, Clone, PartialEq, Eq)]
#[error("required field {} missing", .field)]
pub struct RequiredFieldMissing {
    field: u32,
}

impl RequiredFieldMissing {
    /// Creates a new `RequiredFieldMissing` error.
    pub fn new(field: u32) -> Self {
        Self { field }
    }

    /// Returns the id of the field that is missing.
    pub fn field(self) -> u32 {
        self.field
    }
}

/// An invalid reply was received for a call.
///
/// This can indicate a schema mismatch.
#[derive(Error, Debug, Copy, Clone, PartialEq, Eq)]
#[error("invalid reply")]
pub struct InvalidReply {
    #[from]
    source: DeserializeError,
}

impl InvalidReply {
    /// Creates a new `InvalidReply` error.
    pub fn new(source: DeserializeError) -> Self {
        Self { source }
    }
}

/// An invalid item was received on a channel.
///
/// This can indicate a schema mismatch.
#[derive(Error, Debug, Copy, Clone, PartialEq, Eq)]
#[error("invalid item")]
pub struct InvalidItem {
    #[from]
    source: DeserializeError,
}

impl InvalidItem {
    /// Creates a new `InvalidItem` error.
    pub fn new(source: DeserializeError) -> Self {
        Self { source }
    }
}