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:
DaniPopes 2023-01-07 10:14:25 +01:00 committed by GitHub
parent 7ddfd84e20
commit 2aa7bc3b99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 5 deletions

View File

@ -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},
@ -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;
@ -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()
@ -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()
}
@ -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()