Skip to content

Commit

Permalink
feat: import from #1875
Browse files Browse the repository at this point in the history
  • Loading branch information
indietyp committed Apr 10, 2023
1 parent 9420a82 commit b38524f
Show file tree
Hide file tree
Showing 2 changed files with 40 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
@@ -1,6 +1,7 @@
mod array;
mod atomic;
mod bool;
mod bytes;
mod floating;
mod integral;
mod non_zero;
Expand Down
39 changes: 39 additions & 0 deletions libs/deer/src/impls/core/bytes.rs
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)
}
}

0 comments on commit b38524f

Please sign in to comment.