-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: import from #1875 * chore: docs * test: `Option`
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ mod floating; | |
mod integral; | ||
mod mem; | ||
mod non_zero; | ||
mod option; | ||
mod string; | ||
mod unit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use core::marker::PhantomData; | ||
|
||
use error_stack::{Result, ResultExt}; | ||
|
||
use crate::{ | ||
error::{DeserializeError, VisitorError}, | ||
Deserialize, Deserializer, Document, OptionalVisitor, Reflection, Schema, | ||
}; | ||
|
||
struct OptionVisitor<T>(PhantomData<fn() -> *const T>); | ||
|
||
impl<'de, T: Deserialize<'de>> OptionalVisitor<'de> for OptionVisitor<T> { | ||
type Value = Option<T>; | ||
|
||
fn expecting(&self) -> Document { | ||
Self::Value::reflection() | ||
} | ||
|
||
fn visit_none(self) -> Result<Self::Value, VisitorError> { | ||
Ok(None) | ||
} | ||
|
||
fn visit_null(self) -> Result<Self::Value, VisitorError> { | ||
Ok(None) | ||
} | ||
|
||
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, VisitorError> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
T::deserialize(deserializer) | ||
.change_context(VisitorError) | ||
.map(Some) | ||
} | ||
} | ||
|
||
pub struct OptionReflection<T: ?Sized>(PhantomData<fn() -> *const T>); | ||
|
||
impl<T: Reflection + ?Sized> Reflection for OptionReflection<T> { | ||
fn schema(doc: &mut Document) -> Schema { | ||
// TODO: an accurate reflection is not really possible right now | ||
// we need to do oneOf null/none/T and `Schema` does not support it right now | ||
// this needs to be fixed until `0.1` | ||
// For now we just fallback to `T` | ||
T::schema(doc) | ||
} | ||
} | ||
|
||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Option<T> { | ||
type Reflection = OptionReflection<T::Reflection>; | ||
|
||
fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, DeserializeError> { | ||
de.deserialize_optional(OptionVisitor(PhantomData)) | ||
.change_context(DeserializeError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use deer_desert::{assert_tokens, Token}; | ||
use proptest::prelude::*; | ||
|
||
#[cfg(not(miri))] | ||
proptest! { | ||
#[test] | ||
fn option_some_ok(value in any::<u8>()) { | ||
assert_tokens(&Some(value), &[Token::Number(value.into())]); | ||
} | ||
} | ||
|
||
#[test] | ||
fn option_none_ok() { | ||
assert_tokens(&None::<u8>, &[Token::Null]); | ||
} |