#wasm_bindgen and protobuf

6 messages · Page 1 of 1 (latest)

warped forge
#

Hello,

I'm trying to make something that will allow me to convert my protobuf easily (without using JsValue directly) from my wasm_bindgen.
So I've this wasm lib cf second message otherwise this one is too long.
And in generated files I've added this cf third message other wise this one is too long.
the solo problem I'm encountering is on the response the trait bound `Result<SayHelloResponse, wasm_bindgen::JsError>: IntoJsResult` is not satisfied the following other types implement trait `IntoJsResult`: Result<(), E> Result<T, E>
Any idea how to avoid that ?
Note: if I replace the return with JsValue ofc all is working well

#
use core::fmt;

use protos::helloworld::v1::{
    greeter_service_client::GreeterServiceClient, SayHelloRequest, SayHelloResponse,
};
use serde_wasm_bindgen::{from_value, to_value};
use tonic_web_wasm_client::Client;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::js_sys::Object;

#[wasm_bindgen]
pub struct GreeterClient {
    client: GreeterServiceClient<Client>,
}

#[wasm_bindgen]
impl GreeterClient {
    pub fn new(url: String) -> GreeterClient {
        GreeterClient {
            client: GreeterServiceClient::new(Client::new(url)),
        }
    }

    pub async fn say_hello(
        &mut self,
        request: SayHelloRequest,
    ) -> Result<SayHelloResponse, JsError> {
        // Call the gRPC method
        let response = self
            .client
            .say_hello(request)
            .await
            .map_err(|err| JsError::from(err))?;

        // Extract the inner response from tonic::Response
        let inner_response = response.into_inner();

        // Serialize the response back into a JsValue
        let response_js_value = to_value(&inner_response).map_err(|err| JsError::from(err))?;

        Ok(response_js_value)
    }
}
#
use serde_wasm_bindgen::{from_value, to_value};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::js_sys::Object;

impl wasm_bindgen::describe::WasmDescribe for SayHelloRequest {
    fn describe() {
        Object::describe();
    }
}

impl wasm_bindgen::convert::FromWasmAbi for SayHelloRequest {
    type Abi = <JsValue as wasm_bindgen::convert::FromWasmAbi>::Abi;

    unsafe fn from_abi(js: Self::Abi) -> Self {
        let js_value = <JsValue as wasm_bindgen::convert::FromWasmAbi>::from_abi(js);
        from_value(js_value).unwrap()
    }
}

impl wasm_bindgen::convert::IntoWasmAbi for SayHelloRequest {
    type Abi = <JsValue as wasm_bindgen::convert::IntoWasmAbi>::Abi;

    fn into_abi(self) -> Self::Abi {
        let js_value = to_value(&self).unwrap();
        <JsValue as wasm_bindgen::convert::IntoWasmAbi>::into_abi(js_value)
    }
}

impl wasm_bindgen::describe::WasmDescribe for SayHelloResponse {
    fn describe() {
        Object::describe();
    }
}

impl wasm_bindgen::convert::FromWasmAbi for SayHelloResponse {
    type Abi = <JsValue as wasm_bindgen::convert::FromWasmAbi>::Abi;

    unsafe fn from_abi(js: Self::Abi) -> Self {
        let js_value = <JsValue as wasm_bindgen::convert::FromWasmAbi>::from_abi(js);
        from_value(js_value).unwrap()
    }
}

impl wasm_bindgen::convert::IntoWasmAbi for SayHelloResponse {
    type Abi = <JsValue as wasm_bindgen::convert::IntoWasmAbi>::Abi;

    fn into_abi(self) -> Self::Abi {
        let js_value = to_value(&self).unwrap();
        <JsValue as wasm_bindgen::convert::IntoWasmAbi>::into_abi(js_value)
    }
}
#

(my final goal is to make something that will generate my wasm cli alone as well as having the right type and not a JsValue returned)

#

for the protobuf i'm generating a serde impl of those that allo me to use the from and to

#

ok nvm I was just missing

impl From<JsValue> for SayHelloResponse {
    fn from(js_value: JsValue) -> Self {
        from_value(js_value).unwrap_or_else(|_| SayHelloResponse {
            message: String::new(),
        })
    }
}

impl Into<JsValue> for SayHelloResponse {
    fn into(self) -> JsValue {
        to_value(&self).unwrap_or(JsValue::NULL)
    }
}