Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deer: implement Deserialize for core::ops #2422

Merged
merged 22 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions libs/deer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use core::{
fmt::{self, Debug, Display, Formatter},
};

pub use duplicate::{DuplicateField, DuplicateFieldError, DuplicateKey, DuplicateKeyError};
use error_stack::{Context, Frame, IntoReport, Report, Result};
pub use extra::{
ArrayLengthError, ExpectedLength, ObjectItemsExtraError, ObjectLengthError, ReceivedKey,
Expand All @@ -80,6 +81,7 @@ pub use value::{MissingError, ReceivedValue, ValueError};

use crate::error::serialize::{impl_serialize, Export};

mod duplicate;
mod extra;
mod internal;
mod location;
Expand Down Expand Up @@ -469,3 +471,18 @@ impl<C: Context> ReportExt<C> for Report<C> {
Export::new(self)
}
}

pub(crate) trait ResultExtPrivate<C: Context> {
fn extend_one(&mut self, error: Report<C>);
}

impl<T, C: Context> ResultExtPrivate<C> for Result<T, C> {
fn extend_one(&mut self, error: Report<C>) {
match self {
Err(errors) => {
errors.extend_one(error);
}
errors => *errors = Err(error),
}
}
}
113 changes: 113 additions & 0 deletions libs/deer/src/error/duplicate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use alloc::string::String;
use core::{
fmt,
fmt::{Display, Formatter},
};

use crate::{
error::{ErrorProperties, ErrorProperty, Id, Location, Namespace, Variant, NAMESPACE},
id,
};

pub struct DuplicateField(&'static str);

impl DuplicateField {
#[must_use]
pub const fn new(name: &'static str) -> Self {
Self(name)
}
}

impl ErrorProperty for DuplicateField {
type Value<'a> = Option<&'static str> where Self: 'a;

fn key() -> &'static str {
"field"
}

fn value<'a>(mut stack: impl Iterator<Item = &'a Self>) -> Self::Value<'a> {
stack.next().map(|field| field.0)
}
}

#[derive(Debug)]
pub struct DuplicateFieldError;

impl Variant for DuplicateFieldError {
type Properties = (Location, DuplicateField);

const ID: Id = id!["duplicate", "field"];
const NAMESPACE: Namespace = NAMESPACE;

fn message(
&self,
fmt: &mut Formatter,
properties: &<Self::Properties as ErrorProperties>::Value<'_>,
) -> fmt::Result {
let (_, field) = properties;

if let Some(field) = field {
write!(fmt, "duplicate field `{field}`")
} else {
Display::fmt(self, fmt)
}
}
}

impl Display for DuplicateFieldError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("duplicate field")
}
}

pub struct DuplicateKey(String);

impl DuplicateKey {
pub fn new(key: impl Into<String>) -> Self {
Self(key.into())
}
}

impl ErrorProperty for DuplicateKey {
type Value<'a> = Option<&'a str> where Self: 'a;

fn key() -> &'static str {
"key"
}

fn value<'a>(mut stack: impl Iterator<Item = &'a Self>) -> Self::Value<'a> {
stack.next().map(|key| key.0.as_str())
}
}

#[derive(Debug)]
pub struct DuplicateKeyError;

impl Variant for DuplicateKeyError {
type Properties = (Location, DuplicateKey);

const ID: Id = id!["duplicate", "key"];
const NAMESPACE: Namespace = NAMESPACE;

fn message(
&self,
fmt: &mut Formatter,
properties: &<Self::Properties as ErrorProperties>::Value<'_>,
) -> fmt::Result {
let (_, key) = properties;

if let Some(key) = key {
write!(fmt, "duplicate key `{key}`")
} else {
Display::fmt(self, fmt)
}
}
}

impl Display for DuplicateKeyError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("duplicate key")
}
}

// TODO: unit test
thehabbos007 marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions libs/deer/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use error_stack::{Result, ResultExt};
use serde::{ser::SerializeMap, Serialize, Serializer};

use crate::{
error::{DeserializeError, VisitorError},
ext::TupleExt,
schema::Reference,
Deserialize, Deserializer, Document, EnumVisitor, FieldVisitor, ObjectAccess, Reflection,
Schema, Visitor,
};
Expand Down Expand Up @@ -123,3 +125,20 @@ impl<'de> Deserialize<'de> for ExpectNone {
}

// TODO: consider adding an error attachment marker type for "short-circuit"

pub struct Properties<const N: usize>(pub [(&'static str, Reference); N]);

impl<const N: usize> Serialize for Properties<N> {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.0.len()))?;

for (key, value) in self.0 {
map.serialize_entry(key, &value)?;
}

map.end()
}
}
25 changes: 25 additions & 0 deletions libs/deer/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
use error_stack::Result;

use crate::{error::VisitorError, Deserialize, Document, OptionalVisitor};

mod core;

pub(crate) struct UnitVariantVisitor;

impl<'de> OptionalVisitor<'de> for UnitVariantVisitor {
type Value = ();

fn expecting(&self) -> Document {
// TODO: in theory also none, cannot be expressed with current schema
<() as Deserialize>::reflection()
}

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

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

// we do not implement `visit_some` because we do not allow for some values
}
1 change: 1 addition & 0 deletions libs/deer/src/impls/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod cmp;
mod marker;
mod mem;
mod num;
mod ops;
mod option;
mod result;
mod string;
Expand Down
Loading