I am trying to deserialize a YAML config file in Rust using the Serde library. On many places, the format uses a convention of drivers, i.e. a field named "driver" determines what kind of object the configuration is for. For example, this would be two valid configurations:
reader:
driver: file
path: /var/log/foo
reader:
driver: http
port: 8080
endpoint: /api/bar
I have the following structs that I want to deserialize into:
struct Settings {
reader: Reader,
}
trait Reader {}
struct FileReader {
path: String,
}
impl Reader for FileReader {}
struct HttpReader {
port: u32,
endpoint: String,
}
impl Reader for HttpReader {}
Is there any way I can instruct Serde to deserialize into these structs, preferably without having to do the implementation myself? Can I somehow modify my structs to make them easier to deserialize into, perhaps by introducing an enum or something?