1use std::{
39 error::Error, fmt, fmt::Display, io, io::ErrorKind, ops::RangeInclusive,
40 sync::Arc,
41};
42
43use futures::channel::oneshot;
44use rmpv::{
45 decode::Error as RmpvDecodeError, encode::Error as RmpvEncodeError, Value,
46};
47
48#[derive(Debug, PartialEq, Clone)]
53pub enum InvalidMessage {
54 NotAnArray(Value),
56 WrongArrayLength(RangeInclusive<u64>, u64),
59 InvalidType(Value),
61 UnknownMessageType(u64),
64 InvalidParams(Value, String),
66 InvalidNotificationName(Value),
68 InvalidRequestName(u64, Value),
70 InvalidMsgid(Value),
72}
73
74impl Error for InvalidMessage {}
75
76impl Display for InvalidMessage {
77 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
78 use InvalidMessage::*;
79
80 match self {
81 NotAnArray(val) => write!(fmt, "Value not an Array: '{val}'"),
82 WrongArrayLength(should, is) => write!(
83 fmt,
84 "Array should have length {:?}, has length {}",
85 should, is
86 ),
87 InvalidType(val) => {
88 write!(fmt, "Message type not decodable into u64: {val}")
89 }
90 UnknownMessageType(m) => {
91 write!(fmt, "Message type {m} is not 0, 1 or 2")
92 }
93 InvalidParams(val, s) => {
94 write!(fmt, "Params of method '{s}' not an Array: '{val}'")
95 }
96 InvalidNotificationName(val) => write!(
97 fmt,
98 "Notification name not a
99 string: '{}'",
100 val
101 ),
102 InvalidRequestName(id, val) => {
103 write!(fmt, "Request id {id}: name not valid String: '{val}'")
104 }
105 InvalidMsgid(val) => {
106 write!(fmt, "Msgid of message not decodable into u64: '{val}'")
107 }
108 }
109 }
110}
111
112#[derive(Debug)]
114pub enum DecodeError {
115 BufferError(RmpvDecodeError),
117 ReaderError(io::Error),
121 InvalidMessage(InvalidMessage),
123}
124
125impl Error for DecodeError {
126 fn source(&self) -> Option<&(dyn Error + 'static)> {
127 match *self {
128 DecodeError::BufferError(ref e) => Some(e),
129 DecodeError::InvalidMessage(ref e) => Some(e),
130 DecodeError::ReaderError(ref e) => Some(e),
131 }
132 }
133}
134
135impl Display for DecodeError {
136 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
137 let s = match *self {
138 DecodeError::BufferError(_) => "Error while reading from buffer",
139 DecodeError::InvalidMessage(_) => "Error while decoding",
140 DecodeError::ReaderError(_) => "Error while reading from Reader",
141 };
142
143 fmt.write_str(s)
144 }
145}
146
147impl From<RmpvDecodeError> for Box<DecodeError> {
148 fn from(err: RmpvDecodeError) -> Box<DecodeError> {
149 Box::new(DecodeError::BufferError(err))
150 }
151}
152
153impl From<InvalidMessage> for Box<DecodeError> {
154 fn from(err: InvalidMessage) -> Box<DecodeError> {
155 Box::new(DecodeError::InvalidMessage(err))
156 }
157}
158
159impl From<io::Error> for Box<DecodeError> {
160 fn from(err: io::Error) -> Box<DecodeError> {
161 Box::new(DecodeError::ReaderError(err))
162 }
163}
164
165#[derive(Debug)]
167pub enum EncodeError {
168 BufferError(RmpvEncodeError),
170 WriterError(io::Error),
172}
173
174impl Error for EncodeError {
175 fn source(&self) -> Option<&(dyn Error + 'static)> {
176 match *self {
177 EncodeError::BufferError(ref e) => Some(e),
178 EncodeError::WriterError(ref e) => Some(e),
179 }
180 }
181}
182
183impl Display for EncodeError {
184 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
185 let s = match *self {
186 Self::BufferError(_) => "Error writing to buffer",
187 Self::WriterError(_) => "Error writing to the Writer",
188 };
189
190 fmt.write_str(s)
191 }
192}
193
194impl From<RmpvEncodeError> for Box<EncodeError> {
195 fn from(err: RmpvEncodeError) -> Box<EncodeError> {
196 Box::new(EncodeError::BufferError(err))
197 }
198}
199
200impl From<io::Error> for Box<EncodeError> {
201 fn from(err: io::Error) -> Box<EncodeError> {
202 Box::new(EncodeError::WriterError(err))
203 }
204}
205
206#[derive(Debug)]
211pub enum CallError {
212 SendError(EncodeError, String),
219 InternalReceiveError(oneshot::Canceled, String),
228 DecodeError(Arc<DecodeError>, String),
237 NeovimError(Option<i64>, String),
244 WrongValueType(Value),
247}
248
249impl Error for CallError {
250 fn source(&self) -> Option<&(dyn Error + 'static)> {
251 match *self {
252 CallError::SendError(ref e, _) => Some(e),
253 CallError::InternalReceiveError(ref e, _) => Some(e),
254 CallError::DecodeError(ref e, _) => Some(e.as_ref()),
255 CallError::NeovimError(_, _) | CallError::WrongValueType(_) => None,
256 }
257 }
258}
259
260impl CallError {
261 #[must_use]
265 pub fn is_channel_closed(&self) -> bool {
266 match *self {
267 CallError::SendError(EncodeError::WriterError(ref e), _)
268 if e.kind() == ErrorKind::UnexpectedEof =>
269 {
270 return true
271 }
272 CallError::DecodeError(ref err, _) => {
273 if let DecodeError::ReaderError(ref e) = err.as_ref() {
274 if e.kind() == ErrorKind::UnexpectedEof {
275 return true;
276 }
277 }
278 }
279 _ => {}
280 }
281
282 false
283 }
284}
285
286impl Display for CallError {
287 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
288 match *self {
289 Self::SendError(_, ref s) => write!(fmt, "Error sending request '{s}'"),
290 Self::InternalReceiveError(_, ref s) => {
291 write!(fmt, "Error receiving response for '{s}'")
292 }
293 Self::DecodeError(_, ref s) => {
294 write!(fmt, "Error decoding response to request '{s}'")
295 }
296 Self::NeovimError(ref i, ref s) => match i {
297 Some(i) => write!(fmt, "Error processing request: {i} - '{s}')"),
298 None => write!(
299 fmt,
300 "Error processing request, unknown error format: '{s}'"
301 ),
302 },
303 CallError::WrongValueType(ref val) => {
304 write!(fmt, "Wrong value type: '{val}'")
305 }
306 }
307 }
308}
309
310impl From<Value> for Box<CallError> {
311 fn from(val: Value) -> Box<CallError> {
312 match val {
313 Value::Array(mut arr)
314 if arr.len() == 2 && arr[0].is_i64() && arr[1].is_str() =>
315 {
316 let s = arr
317 .pop()
318 .expect("This was checked")
319 .as_str()
320 .expect("This was checked")
321 .into();
322 let i = arr.pop().expect("This was checked").as_i64();
323 Box::new(CallError::NeovimError(i, s))
324 }
325 val => Box::new(CallError::NeovimError(None, format!("{val:?}"))),
326 }
327 }
328}
329
330#[derive(Debug)]
332pub enum LoopError {
333 MsgidNotFound(u64),
335 DecodeError(Arc<DecodeError>, Option<Vec<u64>>),
344 InternalSendResponseError(u64, Result<Value, Value>),
352}
353
354impl Error for LoopError {
355 fn source(&self) -> Option<&(dyn Error + 'static)> {
356 match *self {
357 LoopError::MsgidNotFound(_)
358 | LoopError::InternalSendResponseError(_, _) => None,
359 LoopError::DecodeError(ref e, _) => Some(e.as_ref()),
360 }
361 }
362}
363
364impl LoopError {
365 #[must_use]
366 pub fn is_channel_closed(&self) -> bool {
367 if let LoopError::DecodeError(ref err, _) = *self {
368 if let DecodeError::ReaderError(ref e) = err.as_ref() {
369 if e.kind() == ErrorKind::UnexpectedEof {
370 return true;
371 }
372 }
373 }
374 false
375 }
376
377 #[must_use]
378 pub fn is_reader_error(&self) -> bool {
379 if let LoopError::DecodeError(ref err, _) = *self {
380 if let DecodeError::ReaderError(_) = err.as_ref() {
381 return true;
382 }
383 }
384 false
385 }
386}
387
388impl Display for LoopError {
389 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
390 match *self {
391 Self::MsgidNotFound(i) => {
392 write!(fmt, "Could not find Msgid '{i}' in the Queue")
393 }
394 Self::DecodeError(_, ref o) => match o {
395 None => write!(fmt, "Error reading message"),
396 Some(v) => write!(
397 fmt,
398 "Error reading message, could not forward \
399 error to the following requests: '{:?}'",
400 v
401 ),
402 },
403 Self::InternalSendResponseError(i, ref res) => write!(
404 fmt,
405 "Request {i}: Could not send response, which was {:?}",
406 res
407 ),
408 }
409 }
410}
411
412impl From<(u64, Result<Value, Value>)> for Box<LoopError> {
413 fn from(res: (u64, Result<Value, Value>)) -> Box<LoopError> {
414 Box::new(LoopError::InternalSendResponseError(res.0, res.1))
415 }
416}
417
418impl From<(Arc<DecodeError>, Vec<u64>)> for Box<LoopError> {
419 fn from(v: (Arc<DecodeError>, Vec<u64>)) -> Box<LoopError> {
420 Box::new(LoopError::DecodeError(v.0, Some(v.1)))
421 }
422}
423
424impl From<u64> for Box<LoopError> {
425 fn from(i: u64) -> Box<LoopError> {
426 Box::new(LoopError::MsgidNotFound(i))
427 }
428}
429
430#[derive(Debug)]
431pub enum HandshakeError {
432 SendError(EncodeError),
438 RecvError(io::Error, String),
445 UnexpectedResponse(String),
451 LaunchError(io::Error),
457}
458
459impl From<Box<EncodeError>> for Box<HandshakeError> {
460 fn from(v: Box<EncodeError>) -> Box<HandshakeError> {
461 Box::new(HandshakeError::SendError(*v))
462 }
463}
464
465impl From<(io::Error, String)> for Box<HandshakeError> {
466 fn from(v: (io::Error, String)) -> Box<HandshakeError> {
467 Box::new(HandshakeError::RecvError(v.0, v.1))
468 }
469}
470
471impl From<io::Error> for Box<HandshakeError> {
472 fn from(v: io::Error) -> Box<HandshakeError> {
473 Box::new(HandshakeError::LaunchError(v))
474 }
475}
476
477impl Error for HandshakeError {
478 fn source(&self) -> Option<&(dyn Error + 'static)> {
479 match *self {
480 Self::SendError(ref s) => Some(s),
481 Self::RecvError(ref s, _) => Some(s),
482 Self::LaunchError(ref s) => Some(s),
483 Self::UnexpectedResponse(_) => None,
484 }
485 }
486}
487
488impl Display for HandshakeError {
489 fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
490 match *self {
491 Self::SendError(ref s) => write!(fmt, "Error sending handshake '{s}'"),
492 Self::RecvError(ref s, ref output) => {
493 write!(
494 fmt,
495 "Error receiving handshake response '{s}'\n\
496 Unexpected output:\n{output}"
497 )
498 }
499 Self::LaunchError(ref s) => write!(fmt, "Error launching nvim '{s}'"),
500 Self::UnexpectedResponse(ref output) => write!(
501 fmt,
502 "Error receiving handshake response, unexpected output:\n{output}"
503 ),
504 }
505 }
506}