chore(core): alias&export error as ParseChainError (#2022)
* chore(core): alias&export error as ParseChainError `pub use TryFromPrimitiveError as ParseChainError` for backwards compatibility * fix: comment * fully alias
This commit is contained in:
parent
7ddfd84e20
commit
2aa7bc3b99
|
@ -1,5 +1,4 @@
|
||||||
use super::{U128, U256, U512, U64};
|
use super::{U128, U256, U512, U64};
|
||||||
use num_enum::{TryFromPrimitive, TryFromPrimitiveError};
|
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
use std::{
|
use std::{
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
|
@ -8,6 +7,12 @@ use std::{
|
||||||
};
|
};
|
||||||
use strum::{AsRefStr, EnumString, EnumVariantNames};
|
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:
|
// When adding a new chain:
|
||||||
// 1. add new variant to the Chain enum;
|
// 1. add new variant to the Chain enum;
|
||||||
// 2. add extra information in the last `impl` block (explorer URLs, block time) when applicable;
|
// 2. add extra information in the last `impl` block (explorer URLs, block time) when applicable;
|
||||||
|
@ -133,7 +138,7 @@ macro_rules! impl_try_from_numeric {
|
||||||
($($native:ty)+ ; $($primitive:ty)*) => {
|
($($native:ty)+ ; $($primitive:ty)*) => {
|
||||||
$(
|
$(
|
||||||
impl TryFrom<$native> for Chain {
|
impl TryFrom<$native> for Chain {
|
||||||
type Error = TryFromPrimitiveError<Self>;
|
type Error = ParseChainError;
|
||||||
|
|
||||||
fn try_from(value: $native) -> Result<Self, Self::Error> {
|
fn try_from(value: $native) -> Result<Self, Self::Error> {
|
||||||
(value as u64).try_into()
|
(value as u64).try_into()
|
||||||
|
@ -143,13 +148,13 @@ macro_rules! impl_try_from_numeric {
|
||||||
|
|
||||||
$(
|
$(
|
||||||
impl TryFrom<$primitive> for Chain {
|
impl TryFrom<$primitive> for Chain {
|
||||||
type Error = TryFromPrimitiveError<Self>;
|
type Error = ParseChainError;
|
||||||
|
|
||||||
fn try_from(value: $primitive) -> Result<Self, Self::Error> {
|
fn try_from(value: $primitive) -> Result<Self, Self::Error> {
|
||||||
if value.bits() > 64 {
|
if value.bits() > 64 {
|
||||||
// `TryFromPrimitiveError` only has a `number` field which has the same type
|
// `TryFromPrimitiveError` only has a `number` field which has the same type
|
||||||
// as the `#[repr(_)]` attribute on the enum.
|
// 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()
|
value.low_u64().try_into()
|
||||||
}
|
}
|
||||||
|
@ -167,7 +172,7 @@ impl From<Chain> for u64 {
|
||||||
impl_into_numeric!(u128 U64 U128 U256 U512);
|
impl_into_numeric!(u128 U64 U128 U256 U512);
|
||||||
|
|
||||||
impl TryFrom<U64> for Chain {
|
impl TryFrom<U64> for Chain {
|
||||||
type Error = TryFromPrimitiveError<Self>;
|
type Error = ParseChainError;
|
||||||
|
|
||||||
fn try_from(value: U64) -> Result<Self, Self::Error> {
|
fn try_from(value: U64) -> Result<Self, Self::Error> {
|
||||||
value.low_u64().try_into()
|
value.low_u64().try_into()
|
||||||
|
|
Loading…
Reference in New Issue