1#![allow(unused)]
2
3use std::fmt;
4
5#[cfg(feature = "lsp")]
6use crate::lsp::{Notification, Request};
7
8#[derive(Debug, Clone, PartialEq)]
10pub struct ProtocolError(String, bool);
11
12impl ProtocolError {
13 pub(crate) fn new(msg: impl Into<String>) -> Self {
15 ProtocolError(msg.into(), false)
16 }
17
18 pub(crate) fn disconnected() -> ProtocolError {
20 ProtocolError("disconnected channel".into(), true)
21 }
22
23 pub fn channel_is_disconnected(&self) -> bool {
25 self.1
26 }
27}
28
29impl std::error::Error for ProtocolError {}
30
31impl fmt::Display for ProtocolError {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 fmt::Display::fmt(&self.0, f)
34 }
35}
36
37#[derive(Debug)]
40pub enum ExtractError<T> {
41 MethodMismatch(T),
43 JsonError {
45 method: String,
47 error: serde_json::Error,
49 },
50}
51
52#[cfg(feature = "lsp")]
53impl std::error::Error for ExtractError<Request> {}
54#[cfg(feature = "lsp")]
55impl fmt::Display for ExtractError<Request> {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match self {
58 ExtractError::MethodMismatch(req) => {
59 write!(f, "Method mismatch for request '{}'", req.method)
60 }
61 ExtractError::JsonError { method, error } => {
62 write!(f, "Invalid request\nMethod: {method}\n error: {error}",)
63 }
64 }
65 }
66}
67
68#[cfg(feature = "lsp")]
69impl std::error::Error for ExtractError<Notification> {}
70#[cfg(feature = "lsp")]
71impl fmt::Display for ExtractError<Notification> {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 ExtractError::MethodMismatch(req) => {
75 write!(f, "Method mismatch for notification '{}'", req.method)
76 }
77 ExtractError::JsonError { method, error } => {
78 write!(f, "Invalid notification\nMethod: {method}\n error: {error}")
79 }
80 }
81 }
82}