Skip to content

Commit

Permalink
Add try_as_array_mut and try_as_object_mut
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <heinz@licenser.net>
  • Loading branch information
Licenser committed Aug 12, 2024
1 parent 26e505b commit 4586fd8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{self, Write};

use crate::{array::Array, object::Object, ValueType};
use crate::{array::Array, object::Object, TryTypeError, ValueType};

/// Type information on a value
pub trait TypedValue {
Expand Down Expand Up @@ -172,13 +172,33 @@ pub trait ValueAsMutArray {
fn as_array_mut(&mut self) -> Option<&mut Self::Array>;
}

/// `try_as_array_mut` access to array value types
pub trait ValueTryAsArrayMut {
/// The array structure
type Array: Array;

/// Tries to represent the value as an array and returns a mutable reference to it
/// # Errors
/// if the requested type doesn't match the actual type
fn try_as_array_mut(&mut self) -> Result<&mut Self::Array, TryTypeError>;
}

/// Mutatability for Object values
pub trait ValueAsMutObject {
/// The type for Objects
type Object;
/// Tries to represent the value as an object and returns a mutable reference to it
fn as_object_mut(&mut self) -> Option<&mut Self::Object>;
}
/// Mutatability for Object values
pub trait ValueTryAsMutObject {
/// The type for Objects
type Object;
/// Tries to represent the value as an object and returns a mutable reference to it
/// # Errors
/// if the requested type doesn't match the actual type
fn try_as_object_mut(&mut self) -> Result<&mut Self::Object, TryTypeError>;
}

/// A trait that specifies how to turn the Value `into` it's sub types
pub trait ValueIntoString {
Expand Down
35 changes: 33 additions & 2 deletions src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{borrow::Borrow, hash::Hash};

use crate::{
array::{ArrayMut, Indexed, IndexedMut},
base::{TypedValue, ValueAsScalar, ValueIntoString},
array::{self, ArrayMut, Indexed, IndexedMut},
base::{TypedValue, ValueAsScalar, ValueIntoString, ValueTryAsArrayMut, ValueTryAsMutObject},
derived::{
MutableArray, MutableObject, MutableValueArrayAccess, TypedArrayValue, TypedObjectValue,
TypedScalarValue, ValueArrayAccess, ValueArrayTryAccess, ValueObjectAccess,
Expand Down Expand Up @@ -229,6 +229,22 @@ where
})
}
}
impl<T> ValueTryAsArrayMut for T
where
T: ValueAsMutArray + TypedValue,
<T as ValueAsMutArray>::Array: array::Array,
{
type Array = T::Array;
#[inline]
fn try_as_array_mut(&mut self) -> Result<&mut Self::Array, TryTypeError> {
let got = self.value_type();
self.as_array_mut().ok_or(TryTypeError {
expected: ValueType::Array,
got,
})
}
}

impl<T> ValueTryAsObject for T
where
T: ValueAsObject + TypedValue,
Expand All @@ -243,6 +259,21 @@ where
})
}
}
impl<T> ValueTryAsMutObject for T
where
T: ValueAsMutObject + TypedValue,
{
type Object = T::Object;

#[inline]
fn try_as_object_mut(&mut self) -> Result<&mut Self::Object, TryTypeError> {
let got = self.value_type();
self.as_object_mut().ok_or(TryTypeError {
expected: ValueType::Object,
got,
})
}
}

impl<T> ValueObjectAccess for T
where
Expand Down

0 comments on commit 4586fd8

Please sign in to comment.