-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod array; | ||
mod atomic; | ||
mod bool; | ||
mod bytes; | ||
mod floating; | ||
mod integral; | ||
mod non_zero; | ||
|
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,39 @@ | ||
use core::marker::PhantomData; | ||
|
||
use error_stack::ResultExt; | ||
|
||
use crate::{ | ||
error::{DeserializeError, VisitorError}, | ||
Deserialize, Deserializer, Document, Reflection, Schema, Visitor, | ||
}; | ||
|
||
struct BytesVisitor<'de>(PhantomData<fn() -> &'de ()>); | ||
|
||
impl<'de> Visitor<'de> for BytesVisitor<'de> { | ||
type Value = &'de [u8]; | ||
|
||
fn expecting(&self) -> Document { | ||
Self::Value::reflection() | ||
} | ||
|
||
fn visit_borrowed_bytes(self, v: &'de [u8]) -> error_stack::Result<Self::Value, VisitorError> { | ||
Ok(v) | ||
} | ||
} | ||
|
||
impl Reflection for [u8] { | ||
fn schema(_: &mut Document) -> Schema { | ||
// this type does not really exist in json-schema :/ | ||
// TODO: correct valid schema? | ||
Schema::new("bytes") | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for &'de [u8] { | ||
type Reflection = [u8]; | ||
|
||
fn deserialize<D: Deserializer<'de>>(de: D) -> error_stack::Result<Self, DeserializeError> { | ||
de.deserialize_bytes(BytesVisitor(PhantomData)) | ||
.change_context(DeserializeError) | ||
} | ||
} |