Rust inside node.js with serde_json


#1

I was following the chapter on Rust Inside Other Languages and I decided to try the node.js example which was simple enough to compile. What I’m struggling with is the idea of passing a json object from nodejs to a Rust function that accepts serde_json::Value.

Is this a guide somewhere that I could follow? As yet, I have not been successful in doing that.


#2

You may want to check out https://www.neon-bindings.com/


#3

Thank you! That is exactly what I was looking for. Great talk too on the homepage.


#4

Hi @fasihrana, I’m interested to know how you get along.

I’m currently figuring out how to use serde_json with Neon and I need help.

Compiler is asking me to impl std::convert::From for neon::vm::Throw, if I’ve understood it correctly.

error[E0277]: the trait bound `neon::vm::Throw: std::convert::From<serde_json::Error>` is not satisfied
  --> src\lib.rs:23:25
   |
23 |   let app_info: Value = serde_json::from_str(&app_info_string)?;
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<serde_json::Error>` is not implemented for `neon::vm::Throw`
   |
   = note: required by `std::convert::From::from`

To satisfy https://doc.rust-lang.org/error-index.html#E0117, I’m trying variations of the following:

use neon::vm::{ Throw };
use serde_json::{ Error };

struct CustomThrow(Throw);

impl From<Error> for CustomThrow {
  fn from(error: Error) -> Self {
    Self.0?
  }
}

However, Throw is a struct, https://api.neon-bindings.com/neon/vm/struct.throw, and I’m not sure how to pass it an error value.

Thank you for any pointers.


#5

Hi @hunterlester,

I’m not an expert and there are probably far more knowledgeable people to answer your question.

I would have simply repackaged that error to the custom type.


#6

@fasihrana Thank you. :smiley:

Thank you to @RReverser for educating me on what was the issue.

I wanted to make sure to updated this thread, for anyone else running into this issue.

I was using the ? operator without understanding or thinking about it: https://m4rw3r.github.io/rust-questionmark-operator

See his answer here (scroll to bottom) which is the exact solution: https://github.com/neon-bindings/neon/issues/166