Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
chore(core): alias&export error as ParseChainError (#2022)
Browse files Browse the repository at this point in the history
* chore(core): alias&export error as ParseChainError

`pub use TryFromPrimitiveError as ParseChainError`
for backwards compatibility

* fix: comment

* fully alias
  • Loading branch information
DaniPopes authored Jan 7, 2023
1 parent 7ddfd84 commit 2aa7bc3
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ethers-core/src/types/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{U128, U256, U512, U64};
use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
use serde::{Deserialize, Serialize, Serializer};
use std::{
convert::{TryFrom, TryInto},
Expand All @@ -8,6 +7,12 @@ use std::{
};
use strum::{AsRefStr, EnumString, EnumVariantNames};

// compatibility re-export
#[doc(hidden)]
pub use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
#[doc(hidden)]
pub type ParseChainError = TryFromPrimitiveError<Chain>;

// When adding a new chain:
// 1. add new variant to the Chain enum;
// 2. add extra information in the last `impl` block (explorer URLs, block time) when applicable;
Expand Down Expand Up @@ -133,7 +138,7 @@ macro_rules! impl_try_from_numeric {
($($native:ty)+ ; $($primitive:ty)*) => {
$(
impl TryFrom<$native> for Chain {
type Error = TryFromPrimitiveError<Self>;
type Error = ParseChainError;

fn try_from(value: $native) -> Result<Self, Self::Error> {
(value as u64).try_into()
Expand All @@ -143,13 +148,13 @@ macro_rules! impl_try_from_numeric {

$(
impl TryFrom<$primitive> for Chain {
type Error = TryFromPrimitiveError<Self>;
type Error = ParseChainError;

fn try_from(value: $primitive) -> Result<Self, Self::Error> {
if value.bits() > 64 {
// `TryFromPrimitiveError` only has a `number` field which has the same type
// as the `#[repr(_)]` attribute on the enum.
return Err(TryFromPrimitiveError { number: value.low_u64() })
return Err(ParseChainError { number: value.low_u64() })
}
value.low_u64().try_into()
}
Expand All @@ -167,7 +172,7 @@ impl From<Chain> for u64 {
impl_into_numeric!(u128 U64 U128 U256 U512);

impl TryFrom<U64> for Chain {
type Error = TryFromPrimitiveError<Self>;
type Error = ParseChainError;

fn try_from(value: U64) -> Result<Self, Self::Error> {
value.low_u64().try_into()
Expand Down

0 comments on commit 2aa7bc3

Please sign in to comment.