to_value

Function to_value 

Source
pub fn to_value<T>(context: Ctx<'_>, value: T) -> Result<Value<'_>>
where T: Serialize,
Expand description

Convert a T into rquickjs::Value

ยงExample

use std::error::Error;

use serde::Serialize;
use rquickjs::{Runtime, Context};

#[derive(Serialize)]
struct User {
    fingerprint: String,
    location: String,
}

fn serialize_to_value() -> Result<(), Box<dyn Error>> {
    let u = User {
        fingerprint: "0xF9BA143B95FF6D82".to_owned(),
        location: "Menlo Park, CA".to_owned(),
    };

    let rt = Runtime::new().unwrap();
    let ctx = Context::full(&rt).unwrap();

    ctx.with(|ctx| {
        let v = rquickjs_serde::to_value(ctx, u).unwrap();
        let obj = v.into_object().unwrap();

        let fingerprint: String = obj.get("fingerprint").unwrap();
        assert_eq!(fingerprint, "0xF9BA143B95FF6D82");

        let location: String = obj.get("location").unwrap();
        assert_eq!(location, "Menlo Park, CA");
    });

    Ok(())
}