diff --git a/crates/rpc-types/src/eth/call.rs b/crates/rpc-types/src/eth/call.rs index 73f48d62699..1c239ae0f38 100644 --- a/crates/rpc-types/src/eth/call.rs +++ b/crates/rpc-types/src/eth/call.rs @@ -168,32 +168,41 @@ impl CallInput { Self { input, data: None } } + /// Consumes the type and returns the optional input data. + #[inline] + pub fn into_input(self) -> Option { + self.input.or(self.data) + } + /// Consumes the type and returns the optional input data. /// /// Returns an error if both `data` and `input` fields are set and not equal. + #[inline] pub fn try_into_unique_input(self) -> Result, CallInputError> { - let Self { input, data } = self; - match (input, data) { - (Some(input), Some(data)) if input == data => Ok(Some(input)), - (Some(_), Some(_)) => Err(CallInputError::default()), - (Some(input), None) => Ok(Some(input)), - (None, Some(data)) => Ok(Some(data)), - (None, None) => Ok(None), - } + self.check_unique_input().map(|()| self.into_input()) } - /// Consumes the type and returns the optional input data. + /// Returns the optional input data. + #[inline] + pub fn input(&self) -> Option<&Bytes> { + self.input.as_ref().or(self.data.as_ref()) + } + + /// Returns the optional input data. /// /// Returns an error if both `data` and `input` fields are set and not equal. + #[inline] pub fn unique_input(&self) -> Result, CallInputError> { - let Self { input, data } = self; - match (input, data) { - (Some(input), Some(data)) if input == data => Ok(Some(input)), - (Some(_), Some(_)) => Err(CallInputError::default()), - (Some(input), None) => Ok(Some(input)), - (None, Some(data)) => Ok(Some(data)), - (None, None) => Ok(None), + self.check_unique_input().map(|()| self.input()) + } + + fn check_unique_input(&self) -> Result<(), CallInputError> { + if let (Some(input), Some(data)) = (&self.input, &self.data) { + if input != data { + return Err(CallInputError::default()); + } } + Ok(()) } }