Operating on a JsValue in Rust/Leptos

255 views Asked by At

I have a js file that returns an array of objects in the following format [{id:0, name:"A"}, {id:1, name:"B"}, {id:2, name:"C"}]

I am using the Rust web framework Leptos currently I am being able to log the js array from my rust code. I want to access the name value and show in my website through Leptos.

This is my code in main.rs file.

use leptos::*;
use wasm_bindgen::prelude::*;
use serde::Deserialize;
use web_sys::console;

struct Team {
    id: u32,
    name: String,
}

#[wasm_bindgen(module = "/src/scripts/rust-tourney.js")]
extern "C" {
   pub fn getMatchups()-> JsValue;
}

#[wasm_bindgen]
pub fn run()->JsValue{
    
    let js_value: JsValue = getMatchups();
    console::log_2(&"Teams value".into(), &js_value);
    return js_value;
}

fn main() {
    leptos::mount_to_body(|| view! { <App/> });
    let js_values = run();
    let json_string = js_values.as_string().unwrap();
    let teams: Vec<Team> = serde_json::from_str(&json_string).expect("Failed to deserialize JSON");
    println!("{:?}", teams)
}

The error I am getting is RuntimeError: unreachable.

0

There are 0 answers