Skip to content

Commit

Permalink
deer: add *Access::context() (#2443)
Browse files Browse the repository at this point in the history
* feat: add `context()` method

* feat: add to existing `*Access`
  • Loading branch information
indietyp authored Apr 19, 2023
1 parent fa4ff75 commit 30c9d06
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion libs/deer/desert/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use deer::{
ArrayAccessError, ArrayLengthError, BoundedContractViolationError, ExpectedLength,
ReceivedLength, Variant,
},
Deserialize, Deserializer as _,
Context, Deserialize, Deserializer as _,
};
use error_stack::{Report, Result, ResultExt};

Expand Down Expand Up @@ -57,6 +57,10 @@ impl<'a, 'b, 'de> ArrayAccess<'a, 'b, 'de> {
}

impl<'de> deer::ArrayAccess<'de> for ArrayAccess<'_, '_, 'de> {
fn context(&self) -> &Context {
self.deserializer.context()
}

fn set_bounded(&mut self, length: usize) -> Result<(), ArrayAccessError> {
if self.consumed > 0 {
return Err(
Expand Down
6 changes: 5 additions & 1 deletion libs/deer/desert/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use deer::{
BoundedContractViolationError, ExpectedLength, ObjectAccessError, ObjectLengthError,
ReceivedLength, Variant,
},
Deserializer as _, FieldVisitor,
Context, Deserializer as _, FieldVisitor,
};
use error_stack::{Report, Result, ResultExt};

Expand Down Expand Up @@ -57,6 +57,10 @@ impl<'a, 'b, 'de: 'a> ObjectAccess<'a, 'b, 'de> {
}

impl<'de> deer::ObjectAccess<'de> for ObjectAccess<'_, '_, 'de> {
fn context(&self) -> &Context {
self.deserializer.context()
}

fn set_bounded(&mut self, length: usize) -> Result<(), ObjectAccessError> {
if self.consumed > 0 {
return Err(
Expand Down
8 changes: 8 additions & 0 deletions libs/deer/json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ impl<'a> ArrayAccess<'a> {
}

impl<'a, 'de> deer::ArrayAccess<'de> for ArrayAccess<'a> {
fn context(&self) -> &Context {
self.context
}

fn set_bounded(&mut self, length: usize) -> Result<(), ArrayAccessError> {
if self.dirty {
return Err(
Expand Down Expand Up @@ -550,6 +554,10 @@ impl<'a> ObjectAccess<'a> {
}

impl<'a, 'de> deer::ObjectAccess<'de> for ObjectAccess<'a> {
fn context(&self) -> &Context {
self.context
}

fn set_bounded(&mut self, length: usize) -> Result<(), ObjectAccessError> {
if self.dirty {
return Err(
Expand Down
4 changes: 4 additions & 0 deletions libs/deer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type FieldValue<'de, F> = <F as FieldVisitor<'de>>::Value;
type FieldResult<'de, F> = Option<Result<FieldValue<'de, F>, ObjectAccessError>>;

pub trait ObjectAccess<'de> {
fn context(&self) -> &Context;

/// This enables bound-checking for [`ObjectAccess`].
///
/// After calling this [`ObjectAccess`] will
Expand Down Expand Up @@ -115,6 +117,8 @@ pub trait FieldVisitor<'de> {
}

pub trait ArrayAccess<'de> {
fn context(&self) -> &Context;

/// Enables bound-checking for [`ArrayAccess`].
///
/// After calling this [`ArrayAccess`] will
Expand Down

0 comments on commit 30c9d06

Please sign in to comment.