use crate::{
controller,
schema::{self, DeviceClass, E173Document},
Error,
};
use wasm_bindgen::prelude::*;
#[wasm_bindgen(skip_jsdoc, js_name = importUdr)]
pub fn import_udr(doc: JsValue) -> Result<schema::E173Document, JsValue> {
let document: schema::E173Document = if doc.is_string() {
serde_json::from_str(&doc.as_string().unwrap()).map_err(Error::json_parse)?
} else if doc.is_object() {
serde_wasm_bindgen::from_value(doc).map_err(|err| Error::parse(&err.to_string()))?
} else {
return Err((Error::InvalidArgument).into());
};
schema::validate_doc(&document)?;
Ok(document)
}
#[wasm_bindgen(skip_jsdoc, js_name = validateUdr)]
pub fn validate_udr(document: E173Document) -> Result<(), JsValue> {
Ok(schema::validate_doc(&document)?)
}
#[wasm_bindgen(skip_jsdoc, js_name = validateUdrDeviceClass)]
pub fn validate_udr_device_class(device_class: DeviceClass) -> Result<(), JsValue> {
Ok(schema::validate_device_class(&device_class)?)
}
#[wasm_bindgen(skip_jsdoc, js_name = createParameterDatabase)]
pub fn create_parameter_database(
device_class: schema::DeviceClass,
) -> Result<controller::ParameterDatabase, Error> {
controller::create_parameter_database(&device_class)
}
#[wasm_bindgen(skip_jsdoc, js_name = importUdrArchive)]
pub fn import_udr_archive(doc: JsValue) -> Result<schema::E173Archive, JsValue> {
let archive: schema::E173Archive = if doc.is_string() {
serde_json::from_str(&doc.as_string().unwrap()).map_err(Error::json_parse)?
} else if doc.is_object() {
serde_wasm_bindgen::from_value(doc).map_err(|err| Error::parse(&err.to_string()))?
} else {
return Err((Error::InvalidArgument).into());
};
Ok(archive)
}