1

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Anders
  • 8,307
  • 9
  • 56
  • 88
  • 1
    See also [How can deserialization of polymorphic trait objects be added in Rust if at all?](https://stackoverflow.com/q/44231020/155423) – Shepmaster Jan 01 '19 at 17:06

0 Answers0