Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'src/lsp/rq.rs')
| -rw-r--r-- | src/lsp/rq.rs | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/src/lsp/rq.rs b/src/lsp/rq.rs index 1d7ef09..50a6fd3 100644 --- a/src/lsp/rq.rs +++ b/src/lsp/rq.rs @@ -4,15 +4,18 @@ use std::marker::PhantomData; use crossbeam::channel::SendError; use lsp_server::{Message, Response as Re}; -use lsp_types::DiagnosticServerCancellationData; -use lsp_types::request::Request; +use lsp_types::{ + DiagnosticServerCancellationData, LspRequestMethod, MessageDirection, + Request, +}; use serde::{Deserialize, Serialize}; use tokio::sync::oneshot; use tokio::task; use tokio_util::task::AbortOnDropHandle; use crate::lsp::Void; - +#[derive(Serialize, Deserialize)] +pub struct RqSendError<X>(Message, PhantomData<X>); #[derive(Serialize, Deserialize)] pub enum RequestError<X> { Rx(PhantomData<X>), @@ -25,7 +28,10 @@ pub type AQErr = RequestError<LSPError>; impl Request for LSPError { type Params = (); type Result = (); - const METHOD: &'static str = "<unknown method>"; + const MESSAGE_DIRECTION: lsp_types::MessageDirection = + MessageDirection::ServerToClient; + const METHOD: LspRequestMethod<'static> = + LspRequestMethod::Custom("<unknown method>"); } #[derive(Debug)] pub struct LSPError {} @@ -50,16 +56,42 @@ impl<X> From<oneshot::error::RecvError> for RequestError<X> { Self::Rx(PhantomData) } } +impl<X: Request> std::error::Error for RqSendError<X> { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + None + } +} impl<X: Request> std::error::Error for RequestError<X> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } } +impl<X> From<SendError<Message>> for RqSendError<X> { + fn from(x: SendError<Message>) -> Self { + Self(x.into_inner(), PhantomData) + } +} impl<X> From<SendError<Message>> for RequestError<X> { fn from(x: SendError<Message>) -> Self { Self::Send(x.into_inner()) } } +impl<X> From<RqSendError<X>> for RequestError<X> { + fn from(x: RqSendError<X>) -> Self { + Self::Send(x.0) + } +} + +impl<X: Request> std::fmt::Display for RqSendError<X> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} failed; couldnt send {:?}", X::METHOD, self.0) + } +} +impl<X: Request> std::fmt::Debug for RqSendError<X> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self, f) + } +} impl<X: Request> std::fmt::Display for RequestError<X> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -99,14 +131,15 @@ impl<const R: &'static str> Display for Unsupported<R> { write!(f, "request {} isnt supported by this LSP", R) } } -pub trait Peel<E> { - fn peel(self) -> Result<(), E>; +pub trait Peel<E, T> { + fn peel(self) -> Result<Option<T>, E>; } -impl<E, N> Peel<E> for Result<Result<(), E>, N> { - fn peel(self) -> Result<(), E> { +impl<E, N, T> Peel<E, T> for Result<Result<T, E>, N> { + fn peel(self) -> Result<Option<T>, E> { match self { Ok(Err(e)) => Err(e), - Ok(Ok(_)) | Err(_) => Ok(()), + Ok(Ok(x)) => Ok(Some(x)), + Err(_) => Ok(None), } } } @@ -130,11 +163,11 @@ impl<T: Clone, R, D, E> Clone for Rq<T, R, D, E> { } } #[derive(serde_derive::Serialize, serde_derive::Deserialize)] -pub struct Rq<T, R, D = (), E = RequestError<R>> { +pub struct Rq<Output, Res, Data = (), Error = LSPError> { #[serde(skip_serializing_if = "Option::is_none", default = "none")] - pub result: Option<T>, + pub result: Option<Output>, #[serde(skip, default = "none")] - pub request: Option<(AbortOnDropHandle<Result<R, E>>, D)>, + pub request: Option<(AbortOnDropHandle<Result<Res, Error>>, Data)>, } impl<T: Debug, R, D: Debug, E> Debug for Rq<T, R, D, E> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |