Skip to content

Commit

Permalink
fix: result and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Nov 15, 2024
1 parent 5743eba commit 98b933d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ pub fn decode_stream(type_variant: String, xdr_base64: String) -> Result<Vec<Str
let limits = Limits::len(decoded_max_len);
let mut cursor = Limited::new(Cursor::new(xdr_base64.as_bytes()), limits);

let value = Type::read_xdr_base64_iter(type_variant, &mut cursor)
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("{e}"))?;
let json = Type::read_xdr_base64_iter(type_variant, &mut cursor)
.map(|value| {
serde_json::to_string(&value.map_err(|e| format!("{e}"))?).map_err(|e| format!("{e}"))
})
.collect::<Result<Vec<_>, _>>()?;
// TODO: Return a native JS value.
// let js = serde_wasm_bindgen::to_value(&value).map_err(|e| format!("{e}"))?;
let json = serde_json::to_string(&value).map_err(|e| format!("{e}"))?;
Ok(json)
}

Expand Down
9 changes: 7 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,13 @@ fn decode_iter_success() {
"ScSpecEntry".to_string(),
"AAAAAAAAAAAAAAAKaW5pdGlhbGl6ZQAAAAAAAwAAAAAAAAAPdG9rZW5fd2FzbV9oYXNoAAAAA+4AAAAgAAAAAAAAAAd0b2tlbl9hAAAAABMAAAAAAAAAB3Rva2VuX2IAAAAAEwAAAAAAAAAAAAAAAAAAAAhzaGFyZV9pZAAAAAAAAAABAAAAEwAAAAAAAAAAAAAAB2RlcG9zaXQAAAAABQAAAAAAAAACdG8AAAAAABMAAAAAAAAACWRlc2lyZWRfYQAAAAAAAAsAAAAAAAAABW1pbl9hAAAAAAAACwAAAAAAAAAJZGVzaXJlZF9iAAAAAAAACwAAAAAAAAAFbWluX2IAAAAAAAALAAAAAAAAAAAAAAAAAAAABHN3YXAAAAAEAAAAAAAAAAJ0bwAAAAAAEwAAAAAAAAAFYnV5X2EAAAAAAAABAAAAAAAAAANvdXQAAAAACwAAAAAAAAAGaW5fbWF4AAAAAAALAAAAAAAAAAAAAAAAAAAACHdpdGhkcmF3AAAABAAAAAAAAAACdG8AAAAAABMAAAAAAAAADHNoYXJlX2Ftb3VudAAAAAsAAAAAAAAABW1pbl9hAAAAAAAACwAAAAAAAAAFbWluX2IAAAAAAAALAAAAAQAAA+0AAAACAAAACwAAAAsAAAAAAAAAAAAAAAlnZXRfcnNydnMAAAAAAAAAAAAAAQAAA+0AAAACAAAACwAAAAs=".to_string(),
),
Ok(r#"[{"function_v0":{"doc":"","name":"initialize","inputs":[{"doc":"","name":"token_wasm_hash","type_":{"bytes_n":{"n":32}}},{"doc":"","name":"token_a","type_":"address"},{"doc":"","name":"token_b","type_":"address"}],"outputs":[]}},{"function_v0":{"doc":"","name":"share_id","inputs":[],"outputs":["address"]}},{"function_v0":{"doc":"","name":"deposit","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"desired_a","type_":"i128"},{"doc":"","name":"min_a","type_":"i128"},{"doc":"","name":"desired_b","type_":"i128"},{"doc":"","name":"min_b","type_":"i128"}],"outputs":[]}},{"function_v0":{"doc":"","name":"swap","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"buy_a","type_":"bool"},{"doc":"","name":"out","type_":"i128"},{"doc":"","name":"in_max","type_":"i128"}],"outputs":[]}},{"function_v0":{"doc":"","name":"withdraw","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"share_amount","type_":"i128"},{"doc":"","name":"min_a","type_":"i128"},{"doc":"","name":"min_b","type_":"i128"}],"outputs":[{"tuple":{"value_types":["i128","i128"]}}]}},{"function_v0":{"doc":"","name":"get_rsrvs","inputs":[],"outputs":[{"tuple":{"value_types":["i128","i128"]}}]}}]"#.to_string()
),
Ok([r#"{"function_v0":{"doc":"","name":"initialize","inputs":[{"doc":"","name":"token_wasm_hash","type_":{"bytes_n":{"n":32}}},{"doc":"","name":"token_a","type_":"address"},{"doc":"","name":"token_b","type_":"address"}],"outputs":[]}}"#,
r#"{"function_v0":{"doc":"","name":"share_id","inputs":[],"outputs":["address"]}}"#,
r#"{"function_v0":{"doc":"","name":"deposit","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"desired_a","type_":"i128"},{"doc":"","name":"min_a","type_":"i128"},{"doc":"","name":"desired_b","type_":"i128"},{"doc":"","name":"min_b","type_":"i128"}],"outputs":[]}}"#,
r#"{"function_v0":{"doc":"","name":"swap","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"buy_a","type_":"bool"},{"doc":"","name":"out","type_":"i128"},{"doc":"","name":"in_max","type_":"i128"}],"outputs":[]}}"#,
r#"{"function_v0":{"doc":"","name":"withdraw","inputs":[{"doc":"","name":"to","type_":"address"},{"doc":"","name":"share_amount","type_":"i128"},{"doc":"","name":"min_a","type_":"i128"},{"doc":"","name":"min_b","type_":"i128"}],"outputs":[{"tuple":{"value_types":["i128","i128"]}}]}}"#,
r#"{"function_v0":{"doc":"","name":"get_rsrvs","inputs":[],"outputs":[{"tuple":{"value_types":["i128","i128"]}}]}}"#
].iter().map(ToString::to_string).collect::<Vec<String>>()),
);
}

Expand Down

0 comments on commit 98b933d

Please sign in to comment.