ron_wasm/parse.rs
1// SPDX-FileCopyrightText: 2024 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! Converts the RON string to the JavaScript value.
6
7use ron::Value;
8use serde::Serialize;
9use serde_wasm_bindgen::Serializer;
10use wasm_bindgen::{JsError, JsValue, prelude::wasm_bindgen};
11
12/// Parses a RON string, constructing the JavaScript value or object described
13/// by the string.
14///
15/// # Errors
16///
17/// Returns an error if any of the following are true:
18///
19/// - The string to parse is not valid RON.
20/// - Cannot construct the JavaScript value or object from a RON value.
21#[wasm_bindgen]
22pub fn parse(text: &str) -> Result<JsValue, JsError> {
23 let value: Value = ron::from_str(text)?;
24 value
25 .serialize(&Serializer::json_compatible())
26 .map_err(JsError::from)
27}