title: Deserialize into different implementors of trait with serde?
body: <p>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:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>reader:
    driver: file
    path: /var/log/foo
  </code></pre>
  
  <pre class="lang-yaml prettyprint-override"><code>reader:
    driver: http
    port: 8080
    endpoint: /api/bar
  </code></pre>
  
  <p>I have the following structs that I want to deserialize into:</p>
  
  <pre><code>struct Settings {
      reader: Reader,
  }
  
  trait Reader {}
  
  struct FileReader {
      path: String,
  }
  
  impl Reader for FileReader {}
  
  struct HttpReader {
      port: u32,
      endpoint: String,
  }
  
  impl Reader for HttpReader {}
  </code></pre>
  
  <p>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?</p>