From 90a7e71385d3d34d71e995dd795f9e76c76b1057 Mon Sep 17 00:00:00 2001 From: Charles Lanahan Date: Thu, 21 Dec 2023 16:47:29 +0000 Subject: [PATCH] Removed *_with_* from serderwrapper. Added a test equivalent to base crate serder. Updated README and demo with some changes that hadn't make it through. Signed-off-by: Charles Lanahan --- src/data.rs | 7 +++++++ wasm/README.md | 2 +- wasm/demo/webpack/index.js | 22 +++++++++++----------- wasm/src/primitives/serder.rs | 35 ++++++++++++++--------------------- wasm/tests/test_wasm.rs | 25 +++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/data.rs b/src/data.rs index b96be4d..53e4b6e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -54,6 +54,13 @@ impl Value { pub fn to_string(&self) -> Result { match self { Self::String(string) => Ok(string.clone()), + Self::Object(o) => { + let mut v = Vec::new(); + for (key, value) in o { + v.push(format!("{}:{}", json!(key), value.to_json()?)); + } + Ok(format!("{{{}}}", v.join(","))) + }, _ => err!(CESRError::Conversion("cannot convert to string".to_string())), } } diff --git a/wasm/README.md b/wasm/README.md index dc87520..6df8298 100644 --- a/wasm/README.md +++ b/wasm/README.md @@ -51,6 +51,6 @@ Its also nice to note that wasm-pack produces a types file in pkg/ that describe ## Testing You can test the Rust interface wrappers using rstest as long as they're vanilla Rust but as soon as you start having dependencies on anything that requires wasm-pack it'll be better to use wasm-bindgen-test crate to test. Those tests are in the `tests` directory and must remain in the base wasm crate for wasm-bindgen-test to work. -That crate is actually just the runner, `wasm-pack test --target=<{node,chrome,firefox,safari}>` is how the tests are run and by default they run in --headless browsers. Some of the targets may not work if you don't have the correct browser drivers installed on your testing instance or docker container. Installing those is out of scope for this README. +That crate is actually just the runner, `wasm-pack test -- where is one of {node,chrome,firefox,safari}` is how the tests are run and by default they run in --headless browsers. Some of the targets may not work if you don't have the correct browser drivers installed on your testing instance or docker container. Installing those is out of scope for this README. To add a test, import from the cesride\_wasm crate and test it by using the the macro #[wasm\_bindgen\_test] on all the things you'd like to test. Async tests are also available. More info can be found (here)[https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html] diff --git a/wasm/demo/webpack/index.js b/wasm/demo/webpack/index.js index a655358..3223355 100644 --- a/wasm/demo/webpack/index.js +++ b/wasm/demo/webpack/index.js @@ -37,22 +37,22 @@ const icp = { } const raw = new TextEncoder().encode(JSON.stringify(icp)) -const serder = cesride.Serder.new_with_raw(raw) -console.log(serder.saider().qb64()) +const serder = new cesride.Serder(undefined, raw, undefined, undefined, undefined) +console.log(serder.saider().qb64) document.write("

Date:

"); document.write("") -document.write("dts: " + dater.dts() + "
"); -document.write("dtsb: " + dater.dtsb() + "
"); -document.write("code: " + dater.code() + "
"); -document.write("size: " + dater.size() + "
"); -document.write("raw: " + dater.raw() + "
"); -document.write("qb64: " + dater.qb64() + "
"); -document.write("qb64b: " + dater.qb64b() + "
"); -document.write("qb2: " + dater.qb2() + "
"); +document.write("dts: " + dater.dts + "
"); +document.write("dtsb: " + dater.dtsb + "
"); +document.write("code: " + dater.code + "
"); +document.write("size: " + dater.size + "
"); +document.write("raw: " + dater.raw + "
"); +document.write("qb64: " + dater.qb64 + "
"); +document.write("qb64b: " + dater.qb64b + "
"); +document.write("qb2: " + dater.qb2 + "
"); try { dater = new cesride.Dater("asdf", undefined, undefined, undefined, undefined, undefined); - document.write("Wrong dater: " + dater.dts() + "
"); + document.write("Wrong dater: " + dater.dts + "
"); } catch (error) { document.write("Error: " + error + "
"); document.write("Error name: " + error.name + "
"); diff --git a/wasm/src/primitives/serder.rs b/wasm/src/primitives/serder.rs index 8c58ebf..f7c8725 100644 --- a/wasm/src/primitives/serder.rs +++ b/wasm/src/primitives/serder.rs @@ -1,12 +1,22 @@ use std::ops::Deref; +use js_sys::Array; +use wasm_bindgen::prelude::*; + use crate::{ - error::*, DigerWrapper, NumberWrapper, SaiderWrapper, TholderWrapper, U128Wrapper, - ValueWrapper, VerferWrapper, VersionWrapper, Wrap, + error::*, + DigerWrapper, + NumberWrapper, + SaiderWrapper, + TholderWrapper, + U128Wrapper, + ValueWrapper, + VerferWrapper, + VersionWrapper, + Wrap, }; use cesride_core::{data::Value, Sadder, Serder}; -use js_sys::Array; -use wasm_bindgen::prelude::*; + #[wasm_bindgen(js_name = Serder)] pub struct SerderWrapper(pub(crate) Serder); @@ -32,23 +42,6 @@ impl SerderWrapper { Ok(SerderWrapper(serder)) } - pub fn new_with_ked( - ked: ValueWrapper, - code: Option, - kind: Option, - ) -> Result { - let serder = - Serder::new_with_ked(&Value::from(ked), code.as_deref(), kind.as_deref()).as_js()?; - Ok(SerderWrapper(serder)) - } - - pub fn new_with_raw( - raw: &[u8] - ) -> Result { - let serder = Serder::new_with_raw(raw).as_js()?; - Ok(SerderWrapper(serder)) - } - pub fn verfers(&self) -> Result { let verfers = self.0.verfers().as_js()?; let arr = Array::new_with_length(verfers.len() as u32); diff --git a/wasm/tests/test_wasm.rs b/wasm/tests/test_wasm.rs index 02a5469..3015831 100644 --- a/wasm/tests/test_wasm.rs +++ b/wasm/tests/test_wasm.rs @@ -1,7 +1,12 @@ use wasm_bindgen_test::*; +use cesride_core::data::dat; + use cesride_wasm::DaterWrapper; use cesride_wasm::CesrideMatterCodex; +use cesride_wasm::SaiderWrapper; +use cesride_wasm::SerderWrapper; +use cesride_wasm::ValueWrapper; /* These dater tests are transcriptions from the first two test_dater tests in @@ -38,3 +43,23 @@ fn test_dater_dts1_construction() { assert_eq!(dater.qb64b().unwrap(), dt1qb64b); assert_eq!(dater.qb2().unwrap(), dt1qb2); } + +// Serder tests +#[wasm_bindgen_test] +fn convenience() { + let _vs = "KERI10JSON000000_"; + let e1 = dat!({ + "v": _vs, + "d": "", + "i": "ABCDEFG", + "s": "0001", + "t": "rot" + }); + + let e1 = SaiderWrapper::saidify(ValueWrapper::new(&e1.to_string().unwrap()), None, None, None, None).unwrap().value(); + + let serder = SerderWrapper::new(None, None, None, Some(ValueWrapper::new(&e1)), None).unwrap(); + let serder2 = SerderWrapper::new(None, Some(serder.raw()), None, None, None).unwrap(); + + assert_eq!(serder.pre().unwrap(), serder2.pre().unwrap()); +} \ No newline at end of file