Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'lib/lsp-server/src/msg.rs')
| -rw-r--r-- | lib/lsp-server/src/msg.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/lsp-server/src/msg.rs b/lib/lsp-server/src/msg.rs index eb8b6bbf04..5e91e33280 100644 --- a/lib/lsp-server/src/msg.rs +++ b/lib/lsp-server/src/msg.rs @@ -234,6 +234,12 @@ impl Response { let error = ResponseError { code, message, data: None }; Response { id, result: None, error: Some(error) } } + pub fn load<P: DeserializeOwned>(self) -> Result<P, ExtractError<Request>> { + self.result.ok_or(ExtractError::NoResult).and_then(|x| { + serde_json::from_value(x) + .map_err(|error| ExtractError::JsonError { method: "".into(), error }) + }) + } } impl Request { @@ -265,6 +271,14 @@ impl Notification { pub fn new(method: String, params: impl serde::Serialize) -> Notification { Notification { method, params: serde_json::to_value(params).unwrap() } } + pub fn load<T: lsp_types::notification::Notification>( + self, + ) -> Result<T::Params, ExtractError<Notification>> { + (self.method == T::METHOD).ok_or(ExtractError::NoResult).and_then(|()| { + serde_json::from_value(self.params) + .map_err(|e| ExtractError::JsonError { method: self.method, error: e }) + }) + } pub fn extract<P: DeserializeOwned>( self, method: &str, |