lsp-types icon indicating copy to clipboard operation
lsp-types copied to clipboard

Error when deserializing notification

Open spencerkohan opened this issue 3 years ago • 1 comments

I have a didSave notification from the SublimeText LSP plugin which looks like this:

{
  "jsonrpc": "2.0",
  "method": "textDocument/didSave",
  "params": {
    "textDocument": {
      "uri": "file:///Users/spencerkohan/Projects/patina/examples/foo.pt"
    },
    "text": "func foo(x: Int) -> Bool {\n\treturn x == 0\n}"
  }
}

I am trying to get a DidSaveTextDocument notification out of it using this method:

fn cast_notification<N>(not: lsp_server::Notification) -> Result<(RequestId, N::Params), lsp_server::Notification>
where
    N: lsp_types::notification::Notification,
    N::Params: serde::de::DeserializeOwned,
{
    not.extract(N::METHOD)
}

impl Notification {
    pub fn extract<P: DeserializeOwned>(self, method: &str) -> Result<P, Notification> {
        eprintln!("Notificatin params: {:?}", self.params);
        if self.method == method {
            let params = serde_json::from_value(self.params).unwrap_or_else(|err| {
                panic!("Invalid notification\nMethod: {}\n error: {}", method, err)
            });
            Ok(params)
        } else {
            Err(self)
        }
    }
}

And I get this error:

invalid type: map, expected a tuple of size 2

So it looks like it's expecting textDocument or params to be a tuple rather than a dictionary. Is this a mal-formed notification JSON, or is there something else I'm doing incorrectly?

spencerkohan avatar Nov 01 '20 17:11 spencerkohan

Your function extracts a tuple, which doesn't seem right fn cast_notification<N>(not: lsp_server::Notification) -> Result<(RequestId, N::Params), lsp_server::Notification> ?

Can't see any other tuples so that must be it

Marwes avatar Nov 02 '20 09:11 Marwes