Skip to content

Commit

Permalink
small refactoring (alloy-rs#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored May 11, 2024
1 parent 6c58a0a commit af25c53
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 77 deletions.
14 changes: 7 additions & 7 deletions crates/json-rpc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ impl Ord for Id {
// strings < null
// null == null
match (self, other) {
(Id::Number(a), Id::Number(b)) => a.cmp(b),
(Id::Number(_), _) => std::cmp::Ordering::Less,
(Self::Number(a), Self::Number(b)) => a.cmp(b),
(Self::Number(_), _) => std::cmp::Ordering::Less,

(Id::String(_), Id::Number(_)) => std::cmp::Ordering::Greater,
(Id::String(a), Id::String(b)) => a.cmp(b),
(Id::String(_), Id::None) => std::cmp::Ordering::Less,
(Self::String(_), Self::Number(_)) => std::cmp::Ordering::Greater,
(Self::String(a), Self::String(b)) => a.cmp(b),
(Self::String(_), Self::None) => std::cmp::Ordering::Less,

(Id::None, Id::None) => std::cmp::Ordering::Equal,
(Id::None, _) => std::cmp::Ordering::Greater,
(Self::None, Self::None) => std::cmp::Ordering::Equal,
(Self::None, _) => std::cmp::Ordering::Greater,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/json-rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where

/// Instantiate a new `LocalUsageError` from a custom error.
pub fn local_usage(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::LocalUsageError(Box::new(err))
Self::LocalUsageError(err.into())
}

/// Instantiate a new `LocalUsageError` from a custom error message.
Expand Down
4 changes: 2 additions & 2 deletions crates/json-rpc/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub enum PubSubItem {

impl From<Response> for PubSubItem {
fn from(response: Response) -> Self {
PubSubItem::Response(response)
Self::Response(response)
}
}

impl From<EthNotification> for PubSubItem {
fn from(notification: EthNotification) -> Self {
PubSubItem::Notification(notification)
Self::Notification(notification)
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rpc-types/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,9 @@ pub enum BlockTransactionsKind {
impl From<bool> for BlockTransactionsKind {
fn from(is_full: bool) -> Self {
if is_full {
BlockTransactionsKind::Full
Self::Full
} else {
BlockTransactionsKind::Hashes
Self::Hashes
}
}
}
Expand All @@ -473,7 +473,7 @@ pub type RichBlock = Rich<Block>;

impl From<Block> for RichBlock {
fn from(block: Block) -> Self {
Rich { inner: block, extra_info: Default::default() }
Self { inner: block, extra_info: Default::default() }
}
}

Expand All @@ -482,7 +482,7 @@ pub type RichHeader = Rich<Header>;

impl From<Header> for RichHeader {
fn from(header: Header) -> Self {
Rich { inner: header, extra_info: Default::default() }
Self { inner: header, extra_info: Default::default() }
}
}

Expand Down
14 changes: 7 additions & 7 deletions crates/rpc-types/src/eth/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ pub enum TransactionIndex {
impl TransactionIndex {
/// Returns true if this is the all variant
pub const fn is_all(&self) -> bool {
matches!(self, TransactionIndex::All)
matches!(self, Self::All)
}

/// Returns the index if this is the index variant
pub const fn index(&self) -> Option<usize> {
match self {
TransactionIndex::All => None,
TransactionIndex::Index(idx) => Some(*idx),
Self::All => None,
Self::Index(idx) => Some(*idx),
}
}
}
Expand All @@ -77,8 +77,8 @@ impl Serialize for TransactionIndex {
S: Serializer,
{
match self {
TransactionIndex::All => serializer.serialize_i8(-1),
TransactionIndex::Index(idx) => idx.serialize(serializer),
Self::All => serializer.serialize_i8(-1),
Self::Index(idx) => idx.serialize(serializer),
}
}
}
Expand All @@ -89,12 +89,12 @@ impl<'de> Deserialize<'de> for TransactionIndex {
D: Deserializer<'de>,
{
match isize::deserialize(deserializer)? {
-1 => Ok(TransactionIndex::All),
-1 => Ok(Self::All),
idx if idx < -1 => Err(serde::de::Error::custom(format!(
"Invalid transaction index, expected -1 or positive integer, got {}",
idx
))),
idx => Ok(TransactionIndex::Index(idx as usize)),
idx => Ok(Self::Index(idx as usize)),
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/rpc-types/src/eth/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ impl EthRpcErrorCode {
/// Returns the error code as `i32`
pub const fn code(&self) -> i32 {
match *self {
EthRpcErrorCode::TransactionRejected => -32003,
EthRpcErrorCode::ExecutionError => 3,
EthRpcErrorCode::InvalidInput => -32000,
EthRpcErrorCode::ResourceNotFound => -32001,
EthRpcErrorCode::UnknownBlock => -39001,
Self::TransactionRejected => -32003,
Self::ExecutionError => 3,
Self::InvalidInput => -32000,
Self::ResourceNotFound => -32001,
Self::UnknownBlock => -39001,
}
}
}
14 changes: 7 additions & 7 deletions crates/rpc-types/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,13 +945,13 @@ pub enum FilterId {

impl From<u64> for FilterId {
fn from(num: u64) -> Self {
FilterId::Num(num)
Self::Num(num)
}
}

impl From<String> for FilterId {
fn from(str: String) -> Self {
FilterId::Str(str)
Self::Str(str)
}
}

Expand Down Expand Up @@ -998,8 +998,8 @@ impl Serialize for PendingTransactionFilterKind {
S: Serializer,
{
match self {
PendingTransactionFilterKind::Hashes => false.serialize(serializer),
PendingTransactionFilterKind::Full => true.serialize(serializer),
Self::Hashes => false.serialize(serializer),
Self::Full => true.serialize(serializer),
}
}
}
Expand All @@ -1008,14 +1008,14 @@ impl<'a> Deserialize<'a> for PendingTransactionFilterKind {
/// Deserializes a boolean value into `PendingTransactionFilterKind`:
/// - `false` becomes `Hashes`
/// - `true` becomes `Full`
fn deserialize<D>(deserializer: D) -> Result<PendingTransactionFilterKind, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'a>,
{
let val = Option::<bool>::deserialize(deserializer)?;
match val {
Some(true) => Ok(PendingTransactionFilterKind::Full),
_ => Ok(PendingTransactionFilterKind::Hashes),
Some(true) => Ok(Self::Full),
_ => Ok(Self::Hashes),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc-types/src/eth/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl From<Index> for U256 {

impl From<usize> for Index {
fn from(idx: usize) -> Self {
Index(idx)
Self(idx)
}
}

Expand All @@ -38,7 +38,7 @@ impl Serialize for Index {
}

impl<'a> Deserialize<'a> for Index {
fn deserialize<D>(deserializer: D) -> Result<Index, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'a>,
{
Expand Down
28 changes: 14 additions & 14 deletions crates/rpc-types/src/eth/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ impl Serialize for SubscriptionResult {
S: Serializer,
{
match *self {
SubscriptionResult::Header(ref header) => header.serialize(serializer),
SubscriptionResult::Log(ref log) => log.serialize(serializer),
SubscriptionResult::TransactionHash(ref hash) => hash.serialize(serializer),
SubscriptionResult::FullTransaction(ref tx) => tx.serialize(serializer),
SubscriptionResult::SyncState(ref sync) => sync.serialize(serializer),
Self::Header(ref header) => header.serialize(serializer),
Self::Log(ref log) => log.serialize(serializer),
Self::TransactionHash(ref hash) => hash.serialize(serializer),
Self::FullTransaction(ref tx) => tx.serialize(serializer),
Self::SyncState(ref sync) => sync.serialize(serializer),
}
}
}
Expand Down Expand Up @@ -114,13 +114,13 @@ impl Params {
/// Returns true if it's a bool parameter.
#[inline]
pub const fn is_bool(&self) -> bool {
matches!(self, Params::Bool(_))
matches!(self, Self::Bool(_))
}

/// Returns true if it's a log parameter.
#[inline]
pub const fn is_logs(&self) -> bool {
matches!(self, Params::Logs(_))
matches!(self, Self::Logs(_))
}
}

Expand All @@ -130,30 +130,30 @@ impl Serialize for Params {
S: Serializer,
{
match self {
Params::None => (&[] as &[serde_json::Value]).serialize(serializer),
Params::Logs(logs) => logs.serialize(serializer),
Params::Bool(full) => full.serialize(serializer),
Self::None => (&[] as &[serde_json::Value]).serialize(serializer),
Self::Logs(logs) => logs.serialize(serializer),
Self::Bool(full) => full.serialize(serializer),
}
}
}

impl<'a> Deserialize<'a> for Params {
fn deserialize<D>(deserializer: D) -> Result<Params, D::Error>
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'a>,
{
let v = serde_json::Value::deserialize(deserializer)?;

if v.is_null() {
return Ok(Params::None);
return Ok(Self::None);
}

if let Some(val) = v.as_bool() {
return Ok(Params::Bool(val));
return Ok(Self::Bool(val));
}

serde_json::from_value(v)
.map(|f| Params::Logs(Box::new(f)))
.map(|f| Self::Logs(Box::new(f)))
.map_err(|e| D::Error::custom(format!("Invalid Pub-Sub parameters: {e}")))
}
}
Expand Down
Loading

0 comments on commit af25c53

Please sign in to comment.