Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internally tagged enums are not working #3

Closed
awestlake87 opened this issue Oct 17, 2019 · 1 comment
Closed

Internally tagged enums are not working #3

awestlake87 opened this issue Oct 17, 2019 · 1 comment

Comments

@awestlake87
Copy link
Contributor

I was trying this library out and I noticed that internal tagging is not deserializing properly. I get runtime errors after simply serializing to a JsValue and deserializing from it.

I looked through your code and found the internally tagged enum test, but it looks like you guys aren't expanding the #[serde(tag = "tag")] attribute in your test_enum! { ... } macro.

I started getting compile errors in the tests/serde.rs file after I added the expansion to the macro_rules block:

macro_rules! test_enum {
    ($(# $attr:tt)* $name:ident) => {{
        #[derive(Debug, PartialEq, Serialize, Deserialize)]
        $(# $attr)*    // RIGHT HERE
        enum $name<A, B> {
            Unit,
            Newtype(A),
            Tuple(A, B),
            Struct { a: A, b: B },
        }

        test_via_json($name::Unit::<(), ()>);
        test_via_json($name::Newtype::<_, ()>("newtype content".to_string()));
        test_via_json($name::Tuple("tuple content".to_string(), 42));
        test_via_json($name::Struct {
            a: "struct content".to_string(),
            b: 42,
        });
    }};
}

Console output:

serde-wasm-bindgen$ wasm-pack test --node
[INFO]: Checking for the Wasm target...
   Compiling serde-wasm-bindgen v0.1.3 (serde-wasm-bindgen)
error: #[serde(tag = "...")] cannot be used with tuple variants

error[E0277]: the trait bound `enums::InternallyTagged<(), ()>: enums::_IMPL_DESERIALIZE_FOR_ExternallyTagged::_serde::Serialize` is not satisfied
   --> tests/serde.rs:89:23
    |
39  |   fn test_via_json<T>(value: T)
    |      -------------
40  |   where
41  |       T: Serialize + DeserializeOwned + PartialEq + Debug,
    |          --------- required by this bound in `test_via_json`
...
89  |           test_via_json($name::Unit::<(), ()>);
    |                         ^^^^^^^^^^^^^^^^^^^^^ the trait `enums::_IMPL_DESERIALIZE_FOR_ExternallyTagged::_serde::Serialize` is not implemented for `enums::InternallyTagged<(), ()>`
...
218 | /     test_enum! {
219 | |         #[serde(tag = "tag")]
220 | |         InternallyTagged
221 | |     }
    | |_____- in this macro invocation

These errors do make sense because internally tagged enums don't work with tuple variants. It's just that they weren't being tested before.

I'm looking at this bug to see if I can fix it, but unfortunately I don't know much about serde. I'm not sure if it's a simple fix or not, but if I figure anything out I'll open a PR.

In the meantime, if you guys have any thoughts or guidance on this, I'd appreciate it!

@awestlake87
Copy link
Contributor Author

I created pull request #4 with changes that appear to fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants