Sorry if the title is a bit vague, it's hard to explain this in one sentence. I'm trying to implement a data structure that's dynamic by nature. In essence, there are only "fields". Each field has a name and type:
struct Metadata {
name: String,
}
enum Field {
Text(String, Metadata),
Image(String, Metadata),
Boolean(bool, Metadata),
}
I also added support for lists:
enum List {
TextList(Vec<String>, Metadata),
ImageList(Vec<String>, Metadata),
BooleanList(Vec<bool>, Metadata),
}
As you can see, lists don't support mixed types. That's exactly what I want. However, I do want to be able to have a list of "groups", where each group is some kind of wrapper able of holding multiple types. For example, let's say I'm creating a data structure that represents a grocery list. I'd need a list of groceries, and each grocery list item should have a name and a boolean to indicate whether it's checked off or not.
But as I said, it's dynamic by nature, so I'm running into problems:
struct Group {
fields: Vec<Field>,
}
enum Field {
// ...
+ Group(Group, Metadata),
enum List {
// ...
+ GroupList(Vec<Group>, Metadata),
}
Now when I use List::GroupList
, I can have groups of mixed types. However, all groups in a list need to adhere to a schema in the context of that list. So in my grocery list example, I can't be sure I'll have the same exact fields for each list item.
Am I approaching this the wrong way?
I hope what I'm trying to achieve came across. I found it pretty hard to explain.