I currently have a problem where I strictly don't want to implement a custom serde::Deserialize for a type, rather I want a good way of composing rust structs and some proc macros to map a variable sized array like:
["some-value",
["some-other-value"],
["some-other-other-value"]
value_00,value_01,
value_10,value_11,
...
]
I'm currently clueless on how serde is able to map arrays into structs.
To help show the desired goal, what is the shape of the struct you expect this to deserialize into?
A common pattern is to deserialize into a shape that matches the incoming data (Vec<MyListOrStringEnum>) and then you can convert from that rust type to the desired type (struct, with positional optional fields, as a guess?)
pub enum Fruit<T> {
Apple(AppleExpression<T>),
}
pub struct AppleExpression<T> {
apple_type: AppleType,
apple_property: AppleProperty,
apple_args: AppleArgs<T>,
}
I'm expecting it to map into Fruit, while the first "some-value",
is just "apple" in case of it being apple.
whatever you end up doing, it's more or less equivalent to a custom Deserialize, so you must have a good reason.
anyways, if your data is only in json format, you can deserialize it into serde_json::Value and then impl From<serde_json::Value> for MyType {...}.
serde_json::Value is capable of representing the json data model , if you want to also support other formats, you can use serde_value::Value, which supports the full serde data model.
I mean, there's always more fine-grained control, and I also do had problems dealing with mapping arrays into struct fields. It seems that the arrays being deserialized into struct seems like a secondary operation rather than being a deliberate operation.