diff --git a/libs/deer/src/impls/core.rs b/libs/deer/src/impls/core.rs index 4e08ee1bf35..96d1447adb9 100644 --- a/libs/deer/src/impls/core.rs +++ b/libs/deer/src/impls/core.rs @@ -1,6 +1,7 @@ mod array; mod atomic; mod bool; +mod bytes; mod floating; mod integral; mod non_zero; diff --git a/libs/deer/src/impls/core/bytes.rs b/libs/deer/src/impls/core/bytes.rs new file mode 100644 index 00000000000..eaeaf5d5996 --- /dev/null +++ b/libs/deer/src/impls/core/bytes.rs @@ -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 &'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 { + 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>(de: D) -> error_stack::Result { + de.deserialize_bytes(BytesVisitor(PhantomData)) + .change_context(DeserializeError) + } +}