title: How to unit-test a deserialization function used in serde(deserialize_with)?
body: <p>I have a struct which implements <code>Deserialize</code> and uses the <code>serde(deserialize_with)</code> on a field:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[derive(Debug, Deserialize)]
  struct Record {
      name: String,
      #[serde(deserialize_with = "deserialize_numeric_bool")]
      is_active: bool,
  }
  </code></pre>
  
  <p>The implementation of <code>deserialize_numeric_bool</code> deserializes a string "0" or "1" to the corresponding boolean value:</p>
  
  <pre class="lang-rust prettyprint-override"><code>pub fn deserialize_numeric_bool&lt;'de, D&gt;(deserializer: D) -&gt; Result&lt;bool, D::Error&gt;
      where D: Deserializer&lt;'de&gt;
  {
      struct NumericBoolVisitor;
  
      impl&lt;'de&gt; Visitor&lt;'de&gt; for NumericBoolVisitor {
          type Value = bool;
  
          fn expecting(&amp;self, formatter: &amp;mut fmt::Formatter) -&gt; fmt::Result {
              formatter.write_str("either 0 or 1")
          }
  
          fn visit_u64&lt;E&gt;(self, value: u64) -&gt; Result&lt;bool, E&gt;
              where E: DeserializeError
          {
              match value {
                  0 =&gt; Ok(false),
                  1 =&gt; Ok(true),
                  _ =&gt; Err(E::custom(format!("invalid bool: {}", value))),
              }
          }
      }
  
      deserializer.deserialize_u64(NumericBoolVisitor)
  }
  </code></pre>
  
  <p>(I appreciate comments about code improvements)</p>
  
  <p>I'd like to write unit tests for deserialization functions like <code>deserialize_numeric_bool</code>. Of course, my friendly search box revealed the <code>serde_test</code> crate and a <a href="https://serde.rs/unit-testing.html" rel="noreferrer">documentation page about unit-testing</a>.
  But these resources couldn't help me in my case, as the crate tests a structure directly implementing <code>Deserialize</code>.</p>
  
  <p>One idea I had was to create a newtype which only contains the output of my deserialize functions and test it with it. But this looks like a unnecessary indirection to me.</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[derive(Deserialize)]
  NumericBool {
      #[serde(deserialize_with = "deserialize_numeric_bool")]
      value: bool
  };
  </code></pre>
  
  <p>How do I write idiomatic tests for it?</p>