Skip to content

Commit

Permalink
feat: improve CallInput
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Dec 18, 2023
1 parent 4ed367e commit 29c837a
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions crates/rpc-types/src/eth/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes> {
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<Option<Bytes>, 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<Option<&Bytes>, 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(())
}
}

Expand Down

0 comments on commit 29c837a

Please sign in to comment.