Skip to content

Commit

Permalink
replace derive_more's 'From' in core
Browse files Browse the repository at this point in the history
  • Loading branch information
pkhry committed May 21, 2024
1 parent 7b82455 commit 53df0e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
17 changes: 10 additions & 7 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use alloc::boxed::Box;
use alloc::string::String;
use derive_more::{Display, From};
use derive_more::Display;
use subxt_metadata::StorageHasher;

/// The error emitted when something goes wrong.
#[derive(Debug, Display, From)]
#[derive(Debug, Display)]
pub enum Error {
/// Codec error.
#[display(fmt = "Scale codec error: {_0}")]
Expand Down Expand Up @@ -38,11 +38,14 @@ pub enum Error {
#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl From<scale_decode::visitor::DecodeError> for Error {
fn from(value: scale_decode::visitor::DecodeError) -> Self {
Error::Decode(value.into())
}
}
convert_error!(ExtrinsicParamsError as Error, Error::ExtrinsicParams);
convert_error!(BlockError as Error, Error::Block);
convert_error!(MetadataError as Error, Error::Metadata);
convert_error!(scale_decode::Error as Error, Error::Decode);
convert_error!(scale_decode::visitor::DecodeError as Error, Error::Decode);
convert_error!(scale_encode::Error as Error, Error::Encode);
convert_error!(StorageAddressError as Error, Error::StorageAddress);
convert_error!(codec::Error as Error, Error::Codec);

/// Block error
#[derive(Clone, Debug, Display, Eq, PartialEq)]
Expand Down
17 changes: 17 additions & 0 deletions core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,21 @@ macro_rules! cfg_substrate_compat {
};
}

macro_rules! convert_error {
($module_path:ty as $delegate_ty:ty, $expr:expr) => {
impl From<$module_path> for $delegate_ty {
fn from(val: $module_path) -> Self {
$expr(val.into())
}
}
};
($ty:ident $(<$( $param:ident ),+>)? as $delegate_ty:ty, $expr:expr) => {
impl From<$ty> for $delegate_ty {
fn from(val: $ty) -> Self {
$expr(val)
}
}
};
}

pub(crate) use {cfg_feature, cfg_substrate_compat};

0 comments on commit 53df0e8

Please sign in to comment.