Skip to content

Commit

Permalink
chore: convert from #1875
Browse files Browse the repository at this point in the history
  • Loading branch information
indietyp committed Apr 9, 2023
1 parent 720c4dc commit 41c213a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/deer/src/impls/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod atomic;
mod bool;
mod floating;
mod integral;
mod marker;
mod non_zero;
mod string;
mod unit;
45 changes: 45 additions & 0 deletions libs/deer/src/impls/core/marker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use core::marker::PhantomData;

use error_stack::{Result, ResultExt};

use crate::{
error::{DeserializeError, VisitorError},
Deserialize, Deserializer, Document, Reflection, Schema, Visitor,
};

struct PhantomDataVisitor<T: ?Sized>(PhantomData<T>);

impl<'de, T: ?Sized> Visitor<'de> for PhantomDataVisitor<T> {
type Value = PhantomData<T>;

fn expecting(&self) -> Document {
Self::Value::reflection()
}

fn visit_none(self) -> Result<Self::Value, VisitorError> {
Ok(PhantomData)
}

fn visit_null(self) -> Result<Self::Value, VisitorError> {
Ok(PhantomData)
}
}

pub struct PhantomDataReflection;

impl Reflection for PhantomDataReflection {
fn schema(_: &mut Document) -> Schema {
// TODO: this is also optional (none)
// currently we're unable to express that constraint (something for 0.2)
Schema::new("null")
}
}

impl<'de, T: ?Sized> Deserialize<'de> for PhantomData<T> {
type Reflection = PhantomDataReflection;

fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, DeserializeError> {
de.deserialize_null(PhantomDataVisitor(PhantomData))
.change_context(DeserializeError)
}
}

0 comments on commit 41c213a

Please sign in to comment.