I am new to Rust, and trying to process some JSON data from a Nodejs server, but cannot figure out this error:
the trait
std::convert::From<Rand>
is not implemented forjson::value::JsonValue
help: the following implementations were found:
<json::value::JsonValue as std::convert::From<&'a [T]>>
<json::value::JsonValue as std::convert::From<&'a str>>
<json::value::JsonValue as std::convert::From>
<json::value::JsonValue as std::convert::From>
and 18 others
note: required because of the requirements on the impl ofstd::convert::Into<json::value::JsonValue>
forRand
rustc(E0277)
lib.rs(43, 12): the traitstd::convert::From<Rand>
is not implemented forjson::value::JsonValue
Here is my code, including some of my commented out attempts at resolving the error:
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
// use wasm_bindgen::convert::traits::IntoWasmAbi;
// use JsonValue::object;
use json;
#[derive(Serialize, Deserialize)]
struct Rand {
value: f32,
date: String,
}
// enum JsonValue {
// Rand(std::convert::From<Rand>),
// }
// impl std::convert::from<Rand> for json::JsonValue {
// fn Rand(&self, f: &mut Rand) -> Rand::Result {
// match self {
// &json::JsonValue::Rand{
// ref Rand
// } => {
// write!(f, "{{solution_type: {},}}",Rand)
// }
// _ => println!("Failed")
// }
// }
// }
#[wasm_bindgen]
pub fn get_max(rand_times: &str) -> String {
// Loop through all json data, and find max
let data: HashMap<String, String> = serde_json::from_str(rand_times).unwrap();
let mut record = Rand {value: 0.0, date: "".to_string()};
// let mut record: Rand = json::object!{value: 0.0, date: "".to_string()};
for (k, v) in &data {
let v_int: f32 = v.parse().unwrap();
if &v_int > &record.value {
record = Rand { value: v_int, date: k.to_string() };
}
}
return json::stringify(record);
}
Any help is appreciated, including criticism of my methods.