Skip to content

Commit

Permalink
Changed internally tagged enums to only test deserialization from Obj…
Browse files Browse the repository at this point in the history
…ect, updated supported types in README
  • Loading branch information
awestlake87 authored and RReverser committed May 14, 2021
1 parent b276aa2 commit 476359a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Supported types and values for the deserialization:
- `char` from a JavaScript string containing a single codepoint.
- `String` from any JavaScript string.
- Rust map (`HashMap`, `BTreeMap`, ...) from any JavaScript iterable producing `[key, value]` pairs (including but not limited to ES2015 `Map`).
> One exception being [internally tagged](https://serde.rs/enum-representations.html#internally-tagged) and [untagged](https://serde.rs/enum-representations.html#untagged) enums. These representations currently do not support deserializing map-like iterables. They only support deserialization from `Object` due to their special treatment in `serde`.
>
> This restriction may be lifted at some point in the future if a `serde(with = ...)` attribute can define the expected Javascript representation of the variant, or if serde-rs/serde#1183 gets resolved.
- `HashMap<String, _>` from any plain JavaScript object (`{ key1: value1, ... }`).
- Rust sequence (tuple, `Vec`, `HashSet`, ...) from any JavaScript iterable (including but not limited to `Array`, ES2015 `Set`, etc.).
- Rust byte buffer (see [`serde_bytes`](https://github.com/serde-rs/bytes)) from JavaScript `ArrayBuffer` or `Uint8Array`.
Expand Down
29 changes: 19 additions & 10 deletions tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ fn enums() {
Unit,
Struct { a: A, b: B },
Sequence { seq: Vec<A> },
Map(BTreeMap<A, B>),
Map(BTreeMap<A, B>)
}

test_via_json(InternallyTagged::Unit::<(), ()>);
Expand All @@ -288,15 +288,24 @@ fn enums() {
test_via_json(InternallyTagged::<i32, ()>::Sequence {
seq: vec![12, 41, -11, -65, 961],
});
test_via_json(InternallyTagged::Map(
vec![
("a".to_string(), 12),
("abc".to_string(), -1161),
("b".to_string(), 64),
]
.into_iter()
.collect(),
));


// Internal tags with maps are not properly deserialized from Map values due to the exclusion
// of Iterables during deserialize_any(). They can be deserialized properly from plain objects
// so we can test that.
assert_eq!(
InternallyTagged::Map(
vec![
("a".to_string(), 12),
("abc".to_string(), -1161),
("b".to_string(), 64)
].into_iter().collect()
),
from_value::<InternallyTagged<String, i32>>(
js_sys::eval("({ 'tag': 'Map', 'a': 12, 'abc': -1161, 'b': 64 })").unwrap()
).unwrap()
);


test_enum! {
#[serde(tag = "tag", content = "content")]
Expand Down

0 comments on commit 476359a

Please sign in to comment.