|
| 1 | +use core::marker::PhantomData; |
| 2 | + |
| 3 | +use error_stack::{Result, ResultExt}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + error::{DeserializeError, VisitorError}, |
| 7 | + Deserialize, Deserializer, Document, OptionalVisitor, Reflection, Schema, |
| 8 | +}; |
| 9 | + |
| 10 | +struct OptionVisitor<T>(PhantomData<fn() -> *const T>); |
| 11 | + |
| 12 | +impl<'de, T: Deserialize<'de>> OptionalVisitor<'de> for OptionVisitor<T> { |
| 13 | + type Value = Option<T>; |
| 14 | + |
| 15 | + fn expecting(&self) -> Document { |
| 16 | + Self::Value::reflection() |
| 17 | + } |
| 18 | + |
| 19 | + fn visit_none(self) -> Result<Self::Value, VisitorError> { |
| 20 | + Ok(None) |
| 21 | + } |
| 22 | + |
| 23 | + fn visit_null(self) -> Result<Self::Value, VisitorError> { |
| 24 | + Ok(None) |
| 25 | + } |
| 26 | + |
| 27 | + fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, VisitorError> |
| 28 | + where |
| 29 | + D: Deserializer<'de>, |
| 30 | + { |
| 31 | + T::deserialize(deserializer) |
| 32 | + .change_context(VisitorError) |
| 33 | + .map(Some) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +pub struct OptionReflection<T: ?Sized>(PhantomData<fn() -> *const T>); |
| 38 | + |
| 39 | +impl<T: Reflection + ?Sized> Reflection for OptionReflection<T> { |
| 40 | + fn schema(doc: &mut Document) -> Schema { |
| 41 | + // TODO: how?! |
| 42 | + todo!() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<'de, T: Deserialize<'de>> Deserialize<'de> for Option<T> { |
| 47 | + type Reflection = OptionReflection<T::Reflection>; |
| 48 | + |
| 49 | + fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, DeserializeError> { |
| 50 | + de.deserialize_optional(OptionVisitor(PhantomData)) |
| 51 | + .change_context(DeserializeError) |
| 52 | + } |
| 53 | +} |
0 commit comments