From b69f18c8f0255795e134cf3d0c295492fd96c4e5 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:33:38 +0200 Subject: [PATCH] feat: add utils to `ValueOrArray` (#924) * feat: add utils to ValueOrArray * fix clippy --- crates/rpc-types-eth/src/filter.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/rpc-types-eth/src/filter.rs b/crates/rpc-types-eth/src/filter.rs index abdfaab0b7f..09d6551e693 100644 --- a/crates/rpc-types-eth/src/filter.rs +++ b/crates/rpc-types-eth/src/filter.rs @@ -668,6 +668,36 @@ pub enum ValueOrArray { Array(Vec), } +impl ValueOrArray { + /// Get the value if present. + pub const fn as_value(&self) -> Option<&T> { + if let Self::Value(value) = self { + Some(value) + } else { + None + } + } + + /// Get the array if present. + pub fn as_array(&self) -> Option<&[T]> { + if let Self::Array(array) = self { + Some(array) + } else { + None + } + } + + /// Check if the enum is a single value. + pub const fn is_value(&self) -> bool { + matches!(self, Self::Value(_)) + } + + /// Check if the enum is an array. + pub const fn is_array(&self) -> bool { + matches!(self, Self::Array(_)) + } +} + impl From
for ValueOrArray
{ fn from(src: Address) -> Self { Self::Value(src)