use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_runtime::AccountId32;
use crate::account::AccountId20;
#[derive(
Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, MaxEncodedLen, TypeInfo, Serialize, Deserialize,
)]
pub enum LinkableAccountId {
AccountId20(AccountId20),
AccountId32(AccountId32),
}
impl From<AccountId20> for LinkableAccountId {
fn from(account_id: AccountId20) -> Self {
Self::AccountId20(account_id)
}
}
impl From<AccountId32> for LinkableAccountId {
fn from(account_id: AccountId32) -> Self {
Self::AccountId32(account_id)
}
}
impl From<[u8; 20]> for LinkableAccountId {
fn from(account_id: [u8; 20]) -> Self {
Self::AccountId20(account_id.into())
}
}
impl From<[u8; 32]> for LinkableAccountId {
fn from(account_id: [u8; 32]) -> Self {
Self::AccountId32(account_id.into())
}
}
impl AsRef<[u8]> for LinkableAccountId {
fn as_ref(&self) -> &[u8] {
match self {
LinkableAccountId::AccountId20(value) => &value.0,
LinkableAccountId::AccountId32(value) => value.as_ref(),
}
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for LinkableAccountId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AccountId20(account_id) => write!(f, "{}", account_id),
Self::AccountId32(account_id) => write!(f, "{}", account_id),
}
}
}
#[cfg(feature = "runtime-benchmarks")]
impl Default for LinkableAccountId {
fn default() -> Self {
AccountId32::new([0u8; 32]).into()
}
}