Skip to content

Manually implement deserialize #43

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

Merged
merged 1 commit into from
Feb 14, 2025
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
45 changes: 43 additions & 2 deletions src/fixed_vector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::tree_hash::vec_tree_hash_root;
use crate::Error;
use serde_derive::{Deserialize, Serialize};
use serde::Deserialize;
use serde_derive::Serialize;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::slice::SliceIndex;
Expand Down Expand Up @@ -44,7 +45,7 @@
/// let long: FixedVector<_, typenum::U5> = FixedVector::from(base);
/// assert_eq!(&long[..], &[1, 2, 3, 4, 0]);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
#[serde(transparent)]
pub struct FixedVector<T, N> {
vec: Vec<T>,
Expand Down Expand Up @@ -340,6 +341,31 @@
}
}

impl<'de, T, N> Deserialize<'de> for FixedVector<T, N>
where
T: Deserialize<'de>,
N: Unsigned,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let vec = Vec::<T>::deserialize(deserializer)?;
if vec.len() == N::to_usize() {
Ok(FixedVector {
vec,
_phantom: PhantomData,

Check warning on line 357 in src/fixed_vector.rs

View check run for this annotation

Codecov / codecov/patch

src/fixed_vector.rs#L357

Added line #L357 was not covered by tests
})
} else {
Err(serde::de::Error::custom(format!(
"Wrong number of FixedVector elements. Expected {}, actual {}",

Check warning on line 361 in src/fixed_vector.rs

View check run for this annotation

Codecov / codecov/patch

src/fixed_vector.rs#L361

Added line #L361 was not covered by tests
N::to_usize(),
vec.len(),
)))
}
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrary<'a>
for FixedVector<T, N>
Expand Down Expand Up @@ -528,4 +554,19 @@
}
assert_eq!(hashset.len(), 2);
}
#[test]
fn serde_invalid_length() {
use typenum::U4;
let json = serde_json::json!([1, 2, 3, 4, 5]);
let result: Result<FixedVector<u64, U4>, _> = serde_json::from_value(json);
assert!(result.is_err());

let json = serde_json::json!([1, 2, 3]);
let result: Result<FixedVector<u64, U4>, _> = serde_json::from_value(json);
assert!(result.is_err());

let json = serde_json::json!([1, 2, 3, 4]);
let result: Result<FixedVector<u64, U4>, _> = serde_json::from_value(json);
assert!(result.is_ok());
}
}
46 changes: 44 additions & 2 deletions src/variable_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::tree_hash::vec_tree_hash_root;
use crate::Error;
use serde_derive::{Deserialize, Serialize};
use serde::Deserialize;
use serde_derive::Serialize;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::slice::SliceIndex;
Expand Down Expand Up @@ -46,7 +47,7 @@
/// // Push a value to if it _does_ exceed the maximum.
/// assert!(long.push(6).is_err());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize)]
#[serde(transparent)]
pub struct VariableList<T, N> {
vec: Vec<T>,
Expand Down Expand Up @@ -312,6 +313,31 @@
}
}

impl<'de, T, N> Deserialize<'de> for VariableList<T, N>
where
T: Deserialize<'de>,
N: Unsigned,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let vec = Vec::<T>::deserialize(deserializer)?;
if vec.len() <= N::to_usize() {
Ok(VariableList {
vec,
_phantom: PhantomData,

Check warning on line 329 in src/variable_list.rs

View check run for this annotation

Codecov / codecov/patch

src/variable_list.rs#L329

Added line #L329 was not covered by tests
})
} else {
Err(serde::de::Error::custom(format!(
"VariableList length {} exceeds maximum length {}",

Check warning on line 333 in src/variable_list.rs

View check run for this annotation

Codecov / codecov/patch

src/variable_list.rs#L333

Added line #L333 was not covered by tests
vec.len(),
N::to_usize()
)))
}
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T: arbitrary::Arbitrary<'a>, N: 'static + Unsigned> arbitrary::Arbitrary<'a>
for VariableList<T, N>
Expand Down Expand Up @@ -574,4 +600,20 @@
}
assert_eq!(hashset.len(), 2);
}

#[test]
fn serde_invalid_length() {
use typenum::U4;
let json = serde_json::json!([1, 2, 3, 4, 5]);
let result: Result<VariableList<u64, U4>, _> = serde_json::from_value(json);
assert!(result.is_err());

let json = serde_json::json!([1, 2, 3]);
let result: Result<VariableList<u64, U4>, _> = serde_json::from_value(json);
assert!(result.is_ok());

let json = serde_json::json!([1, 2, 3, 4]);
let result: Result<VariableList<u64, U4>, _> = serde_json::from_value(json);
assert!(result.is_ok());
}
}
Loading