-
Notifications
You must be signed in to change notification settings - Fork 191
externally tagged enums #225
Comments
Hiya, so I ran into these use cases in one of my projects, and was wondering if you think this type of usage pattern should be accepted: For the given types: #[derive(Debug, Deserialize)]
struct Config {
plain: MyEnum,
// tuple: MyEnum,
#[serde(rename = "struct")]
structv: MyEnum,
newtype: MyEnum,
my_enum: Vec<MyEnum>,
}
#[derive(Debug, Deserialize)]
enum MyEnum {
Plain,
Tuple(i64, bool),
NewType(String),
Struct { value: i64 },
} Accept the following: plain = "Plain"
tuple = { 0 = 123, 1 = true }
struct = { Struct = { value = 123 } }
newtype = { NewType = "value" }
my_enum = [
{ Plain = {} },
{ Tuple = { 0 = 123, 1 = true } },
{ NewType = "value" },
{ Struct = { value = 123 } }
] So far this is what I've got: # toml
my_enum = "UnitVariant" # works with current toml crate
my_enum = { UnitVariant = {} } # implemented on #264
my_enum = { StructVariant = { field = "value" } } # implemented on #264
my_enum = { NewTypeVariant = { "value" } } # implemented on #264
my_enum = { TupleVariant = { 0 = 123, 1 = false } } # implemented on #264
# Ignore
my_enum = { TupleVariant = [123, false] } # invalid TOML |
In terms of intuitiveness and "should we support it", I think it's something like this: # intuitive, should support
my_enum = "UnitVariant"
my_enum = { StructVariant = { field = "value" } }
my_enum = { NewTypeVariant = { "value" } }
# not intuitive, should support
my_enum = { UnitVariant = {} } # necessary for a vector of enums
# not intuitive, maybe support
my_enum = { TupleVariant = { 0 = 123, 1 = false } }
# not intuitive, should not support
my_enum = "value" # for NewTypeVariant |
Does this kind of enum currently only work with the inline table syntax? Given: // Rust code
struct Vec2 {
x: f32,
y: f32,
}
struct TransformData {
translation: Vec2,
scaling: Vec2,
rotation: f32,
}
enum ComponentData {
Transform {
transform_data: TransformData,
}
}
struct Component {
data: ComponentData,
} I can get something like this to deserialize: # TOML
data = { Transform = { translation = { x = 100.0, y = 100.0 }, scaling = { x = 100.0, y = 100.0}, rotation = 1.0 } } So the inline table format works, but unless I've misread something, inline tables have to be on a single line, so any non-trivial variant is going to be several screens long. I've tried about seventeen different ways of using the [brackety] table syntax for the above, but I can't get anything to parse. |
@internally-combusted Unfortunately it looks like the enum support only handles inline tables. |
oh yes, attempted to get the full |
Add support for deserializing enums from dotted tables à la toml-rs#225.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Ive opened a PR to get full tables working with enums. Feedback welcome to get this merged in! |
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Use the current referenced table instead of assuming we should only have 1 table, allowing for more complex parsing of enum structures. Similarly, depending on the context, values can be in either the table or in the current values context. Take them as appropriate.
Maintenance of this crate has moved to the https://github.com/toml-rs/toml repo. As a heads up, we plan to move Closing this out. If this is still a problem, feel free to recreate this issue in the new repo. |
When you define an enum and derive
Serialize
orDeserialize
, you might expect it to get the "externally tagged" semantics described by the serde documentation -- where each variant is represented as a table with a key named after the variant name. But AFAICT this representation doesn't work. Here's a simple example, a config format with a[main]
section and aplugin
field that can be one of{ "url" = ...}
or{ "bin" = ... }
:This fails to deserialize at runtime with the error "expected string for key
main.plugin
". I'm not sure why.BTW there are workarounds, e.g. to use the untagged representation:
The text was updated successfully, but these errors were encountered: