use subxt::{error::ModuleError, events::EventDetails};
use crate::{GearConfig, Result};
pub trait IntoSubxt {
type Target;
fn into_subxt(self) -> Self::Target;
}
pub trait IntoSubstrate {
type Target;
fn into_substrate(self) -> Self::Target;
}
pub trait AsGear {
type Target;
fn as_gear(&self) -> Result<Self::Target>;
}
impl IntoSubxt for sp_runtime::AccountId32 {
type Target = subxt::utils::AccountId32;
fn into_subxt(self) -> Self::Target {
subxt::utils::AccountId32(self.into())
}
}
impl IntoSubstrate for subxt::utils::AccountId32 {
type Target = sp_runtime::AccountId32;
fn into_substrate(self) -> Self::Target {
self.0.into()
}
}
impl<A: IntoSubstrate, B> IntoSubstrate for subxt::utils::MultiAddress<A, B> {
type Target = sp_runtime::MultiAddress<A::Target, B>;
fn into_substrate(self) -> Self::Target {
match self {
Self::Id(id) => Self::Target::Id(id.into_substrate()),
Self::Index(index) => Self::Target::Index(index),
Self::Raw(items) => Self::Target::Raw(items),
Self::Address32(address) => Self::Target::Address32(address),
Self::Address20(address) => Self::Target::Address20(address),
}
}
}
impl<A: IntoSubxt, B> IntoSubxt for sp_runtime::MultiAddress<A, B> {
type Target = subxt::utils::MultiAddress<A::Target, B>;
fn into_subxt(self) -> Self::Target {
match self {
Self::Id(id) => Self::Target::Id(id.into_subxt()),
Self::Index(index) => Self::Target::Index(index),
Self::Raw(items) => Self::Target::Raw(items),
Self::Address32(address) => Self::Target::Address32(address),
Self::Address20(address) => Self::Target::Address20(address),
}
}
}
impl AsGear for EventDetails<GearConfig> {
type Target = crate::Event;
fn as_gear(&self) -> Result<Self::Target> {
Ok(self.as_root_event()?)
}
}
impl AsGear for ModuleError {
type Target = crate::RuntimeError;
fn as_gear(&self) -> Result<Self::Target> {
Ok(self.as_root_error()?)
}
}