Skip to content

Commit

Permalink
bound VariadicValue multiple variant
Browse files Browse the repository at this point in the history
  • Loading branch information
librelois committed Aug 9, 2024
1 parent 2e219e1 commit c4ea3b3
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions client/rpc-core/src/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use serde_json::{from_value, Value};

use crate::types::{BlockNumberOrHash, Log};

const VARIADIC_MULTIPLE_MAX_SIZE: usize = 16;

/// Variadic value
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum VariadicValue<T>
Expand All @@ -55,13 +57,26 @@ where
let v: Value = Deserialize::deserialize(deserializer)?;

if v.is_null() {
return Ok(VariadicValue::Null);
Ok(VariadicValue::Null)
} else if let Ok(value) = from_value::<T>(v.clone()) {
Ok(VariadicValue::Single(value))
} else {
match from_value::<Vec<T>>(v) {
Ok(vec) => {
if vec.len() <= VARIADIC_MULTIPLE_MAX_SIZE {
Ok(VariadicValue::Multiple(vec))
} else {
Err(D::Error::custom(format!(
"Invalid variadic value type: too big array"
)))
}
}
Err(err) => Err(D::Error::custom(format!(
"Invalid variadic value type: {}",
err
))),
}
}

from_value(v.clone())
.map(VariadicValue::Single)
.or_else(|_| from_value(v).map(VariadicValue::Multiple))
.map_err(|err| D::Error::custom(format!("Invalid variadic value type: {}", err)))
}
}

Expand Down

0 comments on commit c4ea3b3

Please sign in to comment.