I've built an ECS (a simple one I think) and I've built a mechanism to load external data (entity templates) into my program the problem I'm having is how to transform the already loaded data into a type.
Since Serde does this I thought to look up how but I can't actually find the part that does this.
What I mean is, when you create a data structure like this:
person:
name: Bob
age: 34
and serde is able to transform it into a struct:
struct Person {
name: String,
age: i32
}
How does serde transform the string person
into the type Person
EDIT: To give an example in another language(ruby):
class Person
attr_accessor :name, :age
def initialize(name:, age:)
@name = name
@age = age
end
end
# pretend type was loaded in from the yaml example from the key
type = 'person'
# pretend person_data was loaded in from the yaml example form the value of the key
person_data = {
name: 'Bob',
age: 34
}
# and now we get the type and then initialize it
# Just like serde does
const_get(type.capitalize).new(person_data)
Now obviously Rust can't do this at runtime or does it like this but serde must do something to that ends up with the same result, with "person"
transforming into Person
.
serde-yaml
– Stargateur