serde-rs/test

Simplify enum representation in serde_test

dtolnay opened this issue · 1 comments

Continuing the discussion from #8:

I think it'd be best to serialise enums with multiple tokens so that Enum is used always, rather than making variants include names and not have the start header. I think it'd be cleaner and make it easier to understand the purpose of the token rather than duplicating the information over multiple token types.

@clarcharr

Here are the reasons I hesitated to eliminate the Token::*Variant form:

  1. The more flexible form is only rarely needed. In our test suite right now we have 7 uses of Token::Enum to 54 uses of Token::*Variant. And of the 7, really 4 of them could be written using Token::NewtypeVariant so the balance is 3 to 58.

    $ git rev-parse HEAD
      5415abf80bb3214498f484ed08d7bace561aa5fc
    $ rg -c 'Token::Enum\(' test_suite/tests/
      test_suite/tests/test_de.rs:7
    $ rg -c 'Token::(Unit|Newtype|Tuple|Struct)Variant\(' test_suite/tests/
      test_suite/tests/test_de.rs:8
      test_suite/tests/test_ser.rs:6
      test_suite/tests/test_macros.rs:22
      test_suite/tests/test_annotations.rs:18
    
  2. I think Token::*Variant is a better match for people used to the Serializer API which is broken down into serialize_unit_variant / serialize_newtype_variant / serialize_tuple_variant / serialize_struct_variant methods.

  3. Token::Enum as it exists right now is ambiguous, which makes it really only useful for testing deserialization and not for serialization.

    [
      Token::Enum("E"),
      Token::Str("A"),
      Token::Unit,
    ]

    Is this a unit variant E::A or a newtype variant E::A(())?