title: How do I implement Queryable and Insertable for custom field types in Diesel?
body: <p>I have an SQL table that I want to work with through Diesel:</p>
  
  <pre><code>CREATE TABLE records (
      id BIGSERIAL PRIMARY KEY,
      record_type SMALLINT NOT NULL,
      value DECIMAL(10, 10) NOT NULL
  )
  </code></pre>
  
  <p>This table generates the following schema:</p>
  
  <pre><code>table! {
      records (id) {
          id -&gt; Int8,
          record_type -&gt; Int2,
          value -&gt; Numeric,
      }
  }
  </code></pre>
  
  <p>Diesel exports decimals as <code>bigdecimal::BigDecimal</code>, but I'd like to work with <code>decimal::d128</code> instead. I also want to map <code>record_type</code> to an enum, so I declare my model like this:</p>
  
  <pre><code>use decimal::d128;
  
  pub enum RecordType {
      A,
      B,
  }
  
  pub struct Record {
      pub id: i64,
      pub record_type: RecordType,
      pub value: d128,
  }
  </code></pre>
  
  <p>I can't use <code>#derive(Queryable, Insertable)</code> because of non-standard type mapping, so I try to implement these traits myself:</p>
  
  <pre><code>impl Queryable&lt;records::SqlType, Pg&gt; for Record {
      type Row = (i64, i16, BigDecimal);
  
      fn build(row: Self::Row) -&gt; Self {
          Record {
              id: row.0,
              record_type: match row.1 {
                  1 =&gt; RecordType::A,
                  2 =&gt; RecordType::B,
                  _ =&gt; panic!("Wrong record type"),
              },
              value: d128!(format!("{}", row.2)),
          }
      }
  }
  </code></pre>
  
  <p>I can't figure out how to implement <code>Insertable</code>. What is the <code>Values</code> associated type? Diesel's documentation is not very clear on this.</p>
  
  <p>Maybe there's a better way to achieve what I'm trying to do?</p>
  
  <p><code>Cargo.toml</code>:</p>
  
  <pre><code>[dependencies]
  bigdecimal = "0.0.10"
  decimal = "2.0.4"
  diesel = { version = "1.1.1", features = ["postgres", "bigdecimal", "num-bigint", "num-integer", "num-traits"] }
  dotenv = "0.9.0"
  </code></pre>