Skip to content

Commit

Permalink
add decode test
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Mar 21, 2024
1 parent 3a3b67f commit 8513e00
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 10 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/kitsune-wasm-mrf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ futures-util = { version = "0.3.30", default-features = false, features = [
kitsune-config = { path = "../kitsune-config" }
kitsune-type = { path = "../kitsune-type" }
miette = "7.2.0"
mrf-manifest = { path = "../../lib/mrf-manifest", features = ["parse"] }
mrf-manifest = { path = "../../lib/mrf-manifest", features = ["decode"] }
multiplex-pool = { path = "../../lib/multiplex-pool" }
redis = { version = "0.25.2", default-features = false, features = [
"connection-manager",
Expand Down
5 changes: 3 additions & 2 deletions lib/mrf-manifest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ wasmparser = { version = "0.201.0", optional = true }

[dev-dependencies]
insta = { version = "1.36.1", default-features = false, features = ["json"] }
wat = "1.201.0"

[features]
encode = ["dep:wasm-encoder", "serialise"]
parse = [
decode = [
"dep:miette",
"dep:leb128",
"dep:serde_json",
"dep:thiserror",
"dep:wasmparser",
]
encode = ["dep:wasm-encoder", "serialise"]
serialise = ["dep:olpc-cjson", "dep:serde_json"]

[lints]
Expand Down
12 changes: 6 additions & 6 deletions lib/mrf-manifest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use std::{
ops::{Deref, DerefMut},
};

#[cfg(feature = "parse")]
#[cfg(feature = "decode")]
pub use self::decode::{decode, DecodeError, SectionRange};
#[cfg(feature = "encode")]
pub use self::encode::encode;
#[cfg(feature = "serialise")]
pub use self::serialise::serialise;

#[cfg(feature = "parse")]
#[cfg(feature = "decode")]
mod decode;
#[cfg(feature = "encode")]
mod encode;
Expand All @@ -39,7 +39,7 @@ where
}

/// Wrapper around a hash set intended for use with the `activityTypes` field
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(transparent)]
pub struct ActivitySet<'a>(#[serde(borrow)] pub ahash::HashSet<Cow<'a, str>>);

Expand Down Expand Up @@ -91,7 +91,7 @@ impl<'a> From<ahash::HashSet<Cow<'a, str>>> for ActivitySet<'a> {
}

/// Version of the API used
#[derive(Clone, Copy, Debug, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum ApiVersion {
Expand All @@ -100,7 +100,7 @@ pub enum ApiVersion {
}

/// Manifest of MRF modules
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase", tag = "manifestVersion")]
#[non_exhaustive]
pub enum Manifest<'a> {
Expand All @@ -122,7 +122,7 @@ impl Manifest<'_> {
}

/// Manifest v1
#[derive(Clone, Debug, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ManifestV1<'a> {
/// Version of the MRF API
Expand Down
20 changes: 20 additions & 0 deletions lib/mrf-manifest/tests/decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg(feature = "decode")]

use mrf_manifest::{Manifest, SECTION_NAME};

const MANIFEST: &str = include_str!("test-manifest.json");

#[test]
fn decode_works() {
let manifest: Manifest<'_> = serde_json::from_str(MANIFEST).unwrap();
// Calling `serde_json::to_string` on a string will encode it into its JSON representation
// which is a fully escaped representation of the string
let escaped_manifest = serde_json::to_string(&MANIFEST).unwrap();

let wat_src = format!(r#"( module ( @custom "{SECTION_NAME}" {escaped_manifest} ) )"#);
let wasm_blob = wat::parse_str(wat_src).unwrap();

let (parsed_manifest, section_range) = mrf_manifest::decode(&wasm_blob).unwrap().unwrap();
assert_eq!(manifest, parsed_manifest);
assert_eq!(section_range, 8..245);
}
12 changes: 12 additions & 0 deletions lib/mrf-manifest/tests/test-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"manifestVersion": "v1",
"apiVersion": "v1",
"name": "test-manifest",
"version": "0.0.1-cattywampus",
"activityTypes": [
"Announce",
"Create",
"Delete",
"Like"
]
}
2 changes: 1 addition & 1 deletion lib/mrf-tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ license = "MIT OR Apache-2.0"
clap = { version = "4.5.3", features = ["derive", "wrap_help"] }
miette = { version = "7.2.0", features = ["fancy"] }
mrf-manifest = { path = "../mrf-manifest", features = [
"decode",
"encode",
"parse",
"serialise",
] }
serde_json = "1.0.114"
Expand Down

0 comments on commit 8513e00

Please sign in to comment.