Skip to content

Commit

Permalink
wip: Add value deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Sep 2, 2023
1 parent 97b10af commit ca6644a
Show file tree
Hide file tree
Showing 5 changed files with 726 additions and 343 deletions.
36 changes: 18 additions & 18 deletions src/abi/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,44 +97,44 @@ impl<T: WithAbiType> WithAbiType for Rc<T> {

impl<T: WithAbiType> WithAbiType for Option<T> {
fn abi_type() -> AbiType {
AbiType::Optional(Box::new(T::abi_type()))
AbiType::Optional(Arc::new(T::abi_type()))
}
}

impl<T: WithAbiType> WithAbiType for Vec<T> {
fn abi_type() -> AbiType {
AbiType::Array(Box::new(T::abi_type()))
AbiType::Array(Arc::new(T::abi_type()))
}
}

impl<T: WithAbiType> WithAbiType for [T] {
fn abi_type() -> AbiType {
AbiType::Array(Box::new(T::abi_type()))
AbiType::Array(Arc::new(T::abi_type()))
}
}

impl<T: WithAbiType, const N: usize> WithAbiType for [T; N] {
fn abi_type() -> AbiType {
AbiType::FixedArray(Box::new(T::abi_type()), N)
AbiType::FixedArray(Arc::new(T::abi_type()), N)
}
}

impl<K: WithPlainAbiType, V: WithAbiType> WithAbiType for BTreeMap<K, V> {
fn abi_type() -> AbiType {
AbiType::Map(K::plain_abi_type(), Box::new(V::abi_type()))
AbiType::Map(K::plain_abi_type(), Arc::new(V::abi_type()))
}
}

impl<K: WithPlainAbiType, V: WithAbiType, S> WithAbiType for HashMap<K, V, S> {
fn abi_type() -> AbiType {
AbiType::Map(K::plain_abi_type(), Box::new(V::abi_type()))
AbiType::Map(K::plain_abi_type(), Arc::new(V::abi_type()))
}
}

#[cfg(feature = "models")]
impl<T: WithAbiType> WithAbiType for crate::models::Lazy<T> {
fn abi_type() -> AbiType {
AbiType::Ref(Box::new(T::abi_type()))
AbiType::Ref(Arc::new(T::abi_type()))
}
}

Expand Down Expand Up @@ -209,7 +209,7 @@ impl_with_plain_abi_type! {
pub trait IntoPlainAbi: IntoAbi {
/// Returns a corresponding plain ABI value.
///
/// NOTE: use [`IntoPlainAbi::into_abi`] when building ABI from a temp value.
/// NOTE: use [`IntoPlainAbi::into_plain_abi`] when building ABI from a temp value.
fn as_plain_abi(&self) -> PlainAbiValue;

/// Converts into a corresponding plain ABI value.
Expand Down Expand Up @@ -488,7 +488,7 @@ impl IntoAbi for str {
impl<T: WithAbiType + IntoAbi> IntoAbi for [T] {
fn as_abi(&self) -> AbiValue {
AbiValue::Array(
Box::new(T::abi_type()),
Arc::new(T::abi_type()),
self.iter().map(T::as_abi).collect(),
)
}
Expand All @@ -505,7 +505,7 @@ impl<T: WithAbiType + IntoAbi> IntoAbi for [T] {
impl<T: WithAbiType + IntoAbi> IntoAbi for Vec<T> {
fn as_abi(&self) -> AbiValue {
AbiValue::Array(
Box::new(T::abi_type()),
Arc::new(T::abi_type()),
self.iter().map(T::as_abi).collect(),
)
}
Expand All @@ -515,7 +515,7 @@ impl<T: WithAbiType + IntoAbi> IntoAbi for Vec<T> {
Self: Sized,
{
AbiValue::Array(
Box::new(T::abi_type()),
Arc::new(T::abi_type()),
self.into_iter().map(T::into_abi).collect(),
)
}
Expand All @@ -525,7 +525,7 @@ impl<K: WithPlainAbiType + IntoPlainAbi, V: WithAbiType + IntoAbi> IntoAbi for B
fn as_abi(&self) -> AbiValue {
AbiValue::Map(
K::plain_abi_type(),
Box::new(V::abi_type()),
Arc::new(V::abi_type()),
self.iter()
.map(|(key, value)| (K::as_plain_abi(key), V::as_abi(value)))
.collect(),
Expand All @@ -538,7 +538,7 @@ impl<K: WithPlainAbiType + IntoPlainAbi, V: WithAbiType + IntoAbi> IntoAbi for B
{
AbiValue::Map(
K::plain_abi_type(),
Box::new(V::abi_type()),
Arc::new(V::abi_type()),
self.into_iter()
.map(|(key, value)| (K::into_plain_abi(key), V::into_abi(value)))
.collect(),
Expand All @@ -550,7 +550,7 @@ impl<K: WithPlainAbiType + IntoPlainAbi, V: WithAbiType + IntoAbi, S> IntoAbi fo
fn as_abi(&self) -> AbiValue {
AbiValue::Map(
K::plain_abi_type(),
Box::new(V::abi_type()),
Arc::new(V::abi_type()),
self.iter()
.map(|(key, value)| (K::as_plain_abi(key), V::as_abi(value)))
.collect(),
Expand All @@ -563,7 +563,7 @@ impl<K: WithPlainAbiType + IntoPlainAbi, V: WithAbiType + IntoAbi, S> IntoAbi fo
{
AbiValue::Map(
K::plain_abi_type(),
Box::new(V::abi_type()),
Arc::new(V::abi_type()),
self.into_iter()
.map(|(key, value)| (K::into_plain_abi(key), V::into_abi(value)))
.collect(),
Expand All @@ -575,7 +575,7 @@ impl<T: WithAbiType + IntoAbi> IntoAbi for Option<T> {
#[inline]
fn as_abi(&self) -> AbiValue {
AbiValue::Optional(
Box::new(T::abi_type()),
Arc::new(T::abi_type()),
self.as_ref().map(T::as_abi).map(Box::new),
)
}
Expand All @@ -585,7 +585,7 @@ impl<T: WithAbiType + IntoAbi> IntoAbi for Option<T> {
where
Self: Sized,
{
AbiValue::Optional(Box::new(T::abi_type()), self.map(T::into_abi).map(Box::new))
AbiValue::Optional(Arc::new(T::abi_type()), self.map(T::into_abi).map(Box::new))
}
}

Expand Down Expand Up @@ -647,7 +647,7 @@ mod tests {
AbiValue::Bool(true),
AbiValue::unnamed_tuple([
AbiValue::Cell(Cell::empty_cell()),
AbiValue::Optional(Box::new(AbiType::Bool), None),
AbiValue::Optional(Arc::new(AbiType::Bool), None),
]),
]);

Expand Down
29 changes: 15 additions & 14 deletions src/abi/ty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::num::NonZeroU8;
use std::str::FromStr;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -186,15 +187,15 @@ pub enum AbiType {
/// Product type.
Tuple(Vec<NamedAbiType>),
/// Array of elements of the specified ABI type.
Array(Box<Self>),
Array(Arc<Self>),
/// Fixed-length array of elements of the specified ABI type.
FixedArray(Box<Self>, usize),
FixedArray(Arc<Self>, usize),
/// Dictionary with the specified key and value ABI types.
Map(PlainAbiType, Box<Self>),
Map(PlainAbiType, Arc<Self>),
/// Optional type.
Optional(Box<Self>),
Optional(Arc<Self>),
/// Type stored in a new cell.
Ref(Box<Self>),
Ref(Arc<Self>),
}

impl AbiType {
Expand Down Expand Up @@ -264,11 +265,11 @@ impl AbiType {
fn components_mut(&mut self) -> Option<&mut Vec<NamedAbiType>> {
match self {
Self::Tuple(types) => Some(types),
Self::Array(ty) => ty.components_mut(),
Self::FixedArray(ty, _) => ty.components_mut(),
Self::Map(_, value_ty) => value_ty.components_mut(),
Self::Optional(ty) => ty.components_mut(),
Self::Ref(ty) => ty.components_mut(),
Self::Array(ty) => Arc::make_mut(ty).components_mut(),
Self::FixedArray(ty, _) => Arc::make_mut(ty).components_mut(),
Self::Map(_, value_ty) => Arc::make_mut(value_ty).components_mut(),
Self::Optional(ty) => Arc::make_mut(ty).components_mut(),
Self::Ref(ty) => Arc::make_mut(ty).components_mut(),
_ => None,
}
}
Expand All @@ -287,7 +288,7 @@ impl AbiType {
.map_err(ParseAbiTypeError::InvalidArrayLength)))
};

let ty = ok!(Self::from_simple_str(ty).map(Box::new));
let ty = ok!(Self::from_simple_str(ty).map(Arc::new));
return Ok(match len {
None => Self::Array(ty),
Some(len) => Self::FixedArray(ty, len),
Expand Down Expand Up @@ -333,20 +334,20 @@ impl AbiType {

Self::Map(
ok!(PlainAbiType::from_str(key_ty)),
ok!(Self::from_simple_str(value_ty).map(Box::new)),
ok!(Self::from_simple_str(value_ty).map(Arc::new)),
)
} else if let Some(s) = s.strip_prefix("optional(") {
let s = ok!(s
.strip_suffix(')')
.ok_or(ParseAbiTypeError::UnterminatedInnerType));

Self::Optional(ok!(Self::from_simple_str(s).map(Box::new)))
Self::Optional(ok!(Self::from_simple_str(s).map(Arc::new)))
} else if let Some(s) = s.strip_prefix("ref(") {
let s = ok!(s
.strip_suffix(')')
.ok_or(ParseAbiTypeError::UnterminatedInnerType));

Self::Ref(ok!(Self::from_simple_str(s).map(Box::new)))
Self::Ref(ok!(Self::from_simple_str(s).map(Arc::new)))
} else {
return Err(ParseAbiTypeError::UnknownType);
}
Expand Down
Loading

0 comments on commit ca6644a

Please sign in to comment.