Skip to content

Commit

Permalink
Removed *_with_* from serderwrapper.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
daidoji committed Dec 21, 2023
1 parent a5fbbf8 commit 90a7e71
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
7 changes: 7 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ impl Value {
pub fn to_string(&self) -> Result<String> {
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())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 --<target> where <target> 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]
22 changes: 11 additions & 11 deletions wasm/demo/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("<p>Date:</p>");
document.write("<code>")
document.write("dts: " + dater.dts() + "<br/>");
document.write("dtsb: " + dater.dtsb() + "<br/>");
document.write("code: " + dater.code() + "<br/>");
document.write("size: " + dater.size() + "<br/>");
document.write("raw: " + dater.raw() + "<br/>");
document.write("qb64: " + dater.qb64() + "<br/>");
document.write("qb64b: " + dater.qb64b() + "<br/>");
document.write("qb2: " + dater.qb2() + "<br/>");
document.write("dts: " + dater.dts + "<br/>");
document.write("dtsb: " + dater.dtsb + "<br/>");
document.write("code: " + dater.code + "<br/>");
document.write("size: " + dater.size + "<br/>");
document.write("raw: " + dater.raw + "<br/>");
document.write("qb64: " + dater.qb64 + "<br/>");
document.write("qb64b: " + dater.qb64b + "<br/>");
document.write("qb2: " + dater.qb2 + "<br/>");
try {
dater = new cesride.Dater("asdf", undefined, undefined, undefined, undefined, undefined);
document.write("Wrong dater: " + dater.dts() + "<br/>");
document.write("Wrong dater: " + dater.dts + "<br/>");
} catch (error) {
document.write("Error: " + error + "<br/>");
document.write("Error name: " + error.name + "<br/>");
Expand Down
35 changes: 14 additions & 21 deletions wasm/src/primitives/serder.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -32,23 +42,6 @@ impl SerderWrapper {
Ok(SerderWrapper(serder))
}

pub fn new_with_ked(
ked: ValueWrapper,
code: Option<String>,
kind: Option<String>,
) -> Result<SerderWrapper> {
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<SerderWrapper> {
let serder = Serder::new_with_raw(raw).as_js()?;
Ok(SerderWrapper(serder))
}

pub fn verfers(&self) -> Result<Array> {
let verfers = self.0.verfers().as_js()?;
let arr = Array::new_with_length(verfers.len() as u32);
Expand Down
25 changes: 25 additions & 0 deletions wasm/tests/test_wasm.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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());
}

0 comments on commit 90a7e71

Please sign in to comment.