Skip to main content

spoolframe_types/
required.rs

1//! A required `Option` field: the key must be on the wire, with
2//! `null` for none.
3//!
4//! serde fills a missing `Option` field with `None` even without
5//! `#[serde(default)]`, so a peer that leaves the key out is read as if it
6//! sent none. A field with its own `deserialize_with` does not get that,
7//! so `#[serde(deserialize_with = "spoolframe_types::required::option")]`,
8//! which is only `Option::deserialize`, makes a missing key an error.
9
10use serde::{Deserialize, Deserializer};
11
12pub fn option<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
13where
14    D: Deserializer<'de>,
15    T: Deserialize<'de>,
16{
17    Option::deserialize(deserializer)
18}