Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when deserializing notification #185

Open
spencerkohan opened this issue Nov 1, 2020 · 2 comments
Open

Error when deserializing notification #185

spencerkohan opened this issue Nov 1, 2020 · 2 comments

Comments

@spencerkohan
Copy link

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?

@Marwes
Copy link
Member

Marwes commented Nov 2, 2020

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

@TheVeryDarkness
Copy link

TheVeryDarkness commented Feb 21, 2025

Notification message does not contain a RequestId, so extracting it will cause an error. Here is my code to extract notification parameters (adapting from lsp_servers' cast example):

fn extract_notification<R>(req: Notification) -> R::Params
where
    R: lsp_types::notification::Notification,
{
    match req.extract(R::METHOD) {
        Ok(req) => req,
        Err(err @ ExtractError::JsonError { .. }) => panic!("{err:?}"),
        Err(ExtractError::MethodMismatch(req)) => unreachable!("{req:?}"),
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants