use super::{error::*, raw::*};
use std::convert::TryFrom;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RootAccountIndex(pub(super) RawAccountIndex);
impl RootAccountIndex {
#[inline]
pub fn new(raw: RawAccountIndex) -> Result<Self, AccountIdError> {
if (raw & !ROOT_ACCOUNT_INDEX_MASK) == 0 {
Ok(Self(raw))
} else {
Err(AccountIdError::IndexRange)
}
}
pub const unsafe fn new_unchecked(raw: RawAccountIndex) -> Self {
Self(raw)
}
#[inline]
pub const fn as_raw(self) -> RawAccountIndex {
self.0
}
}
impl TryFrom<RawAccountIndex> for RootAccountIndex {
type Error = AccountIdError;
#[inline]
fn try_from(raw: RawAccountIndex) -> Result<Self, Self::Error> {
Self::new(raw)
}
}
impl From<RootAccountIndex> for RawAccountIndex {
#[inline]
fn from(index: RootAccountIndex) -> Self {
index.as_raw()
}
}
impl_fmt_traits!(RootAccountIndex);
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct IssuanceAccountIndex(pub(super) RawAccountIndex);
impl IssuanceAccountIndex {
#[inline]
pub fn new(raw: RawAccountIndex) -> Result<Self, AccountIdError> {
if (raw & !ISSUANCE_ACCOUNT_INDEX_MASK) == 0 {
Ok(Self(raw))
} else {
Err(AccountIdError::IndexRange)
}
}
pub const unsafe fn new_unchecked(raw: RawAccountIndex) -> Self {
Self(raw)
}
#[inline]
pub fn as_raw(self) -> RawAccountIndex {
self.0
}
pub fn next(self) -> Self {
Self(self.0.checked_add(1).unwrap())
}
}
impl TryFrom<RawAccountIndex> for IssuanceAccountIndex {
type Error = AccountIdError;
#[inline]
fn try_from(raw: RawAccountIndex) -> Result<Self, Self::Error> {
Self::new(raw)
}
}
impl From<IssuanceAccountIndex> for RawAccountIndex {
#[inline]
fn from(index: IssuanceAccountIndex) -> Self {
index.as_raw()
}
}
impl_fmt_traits!(IssuanceAccountIndex);
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct LeafAccountIndex(pub(super) RawAccountIndex);
impl LeafAccountIndex {
#[inline]
pub fn new(raw: RawAccountIndex) -> Result<Self, AccountIdError> {
if (raw & !LEAF_ACCOUNT_INDEX_MASK) == 0 {
Ok(Self(raw))
} else {
Err(AccountIdError::IndexRange)
}
}
pub const unsafe fn new_unchecked(raw: RawAccountIndex) -> Self {
Self(raw)
}
#[inline]
pub fn as_raw(self) -> RawAccountIndex {
self.0
}
}
impl TryFrom<RawAccountIndex> for LeafAccountIndex {
type Error = AccountIdError;
#[inline]
fn try_from(raw: RawAccountIndex) -> Result<Self, Self::Error> {
Self::new(raw)
}
}
impl From<LeafAccountIndex> for RawAccountIndex {
#[inline]
fn from(index: LeafAccountIndex) -> Self {
index.as_raw()
}
}
impl_fmt_traits!(LeafAccountIndex);
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum AccountIndex {
Root(RootAccountIndex),
Issuance(IssuanceAccountIndex),
Leaf(LeafAccountIndex),
}
impl AccountIndex {
pub fn as_raw(self) -> RawAccountIndex {
match self {
Self::Root(index) => index.as_raw(),
Self::Issuance(index) => index.as_raw(),
Self::Leaf(index) => index.as_raw(),
}
}
pub fn as_root_account_index(self) -> Option<RootAccountIndex> {
match self {
Self::Root(index) => Some(index),
Self::Issuance(_) | Self::Leaf(_) => None,
}
}
pub fn as_issuance_account_index(self) -> Option<IssuanceAccountIndex> {
match self {
Self::Issuance(index) => Some(index),
Self::Root(_) | Self::Leaf(_) => None,
}
}
pub fn as_leaf_account_index(self) -> Option<LeafAccountIndex> {
match self {
Self::Leaf(index) => Some(index),
Self::Root(_) | Self::Issuance(_) => None,
}
}
pub fn as_non_root_account_index(self) -> Option<NonRootAccountIndex> {
match self {
Self::Root(_) => None,
Self::Issuance(index) => Some(NonRootAccountIndex::Issuance(index)),
Self::Leaf(index) => Some(NonRootAccountIndex::Leaf(index)),
}
}
pub fn is_root(self) -> bool {
match self {
Self::Root(_) => true,
Self::Issuance(_) | Self::Leaf(_) => false,
}
}
pub fn is_issuance(self) -> bool {
match self {
Self::Root(_) | Self::Leaf(_) => false,
Self::Issuance(_) => true,
}
}
pub fn is_leaf(self) -> bool {
match self {
Self::Root(_) | Self::Issuance(_) => false,
Self::Leaf(_) => true,
}
}
}
impl From<RootAccountIndex> for AccountIndex {
#[inline]
fn from(index: RootAccountIndex) -> Self {
Self::Root(index)
}
}
impl From<IssuanceAccountIndex> for AccountIndex {
#[inline]
fn from(index: IssuanceAccountIndex) -> Self {
Self::Issuance(index)
}
}
impl From<LeafAccountIndex> for AccountIndex {
#[inline]
fn from(index: LeafAccountIndex) -> Self {
Self::Leaf(index)
}
}
impl From<NonRootAccountIndex> for AccountIndex {
#[inline]
fn from(index: NonRootAccountIndex) -> Self {
match index {
NonRootAccountIndex::Issuance(index) => Self::Issuance(index),
NonRootAccountIndex::Leaf(index) => Self::Leaf(index),
}
}
}
impl TryFrom<AccountIndex> for RootAccountIndex {
type Error = TryFromAccountIndexError;
fn try_from(index: AccountIndex) -> Result<Self, Self::Error> {
index
.as_root_account_index()
.ok_or(TryFromAccountIndexError)
}
}
impl TryFrom<AccountIndex> for IssuanceAccountIndex {
type Error = TryFromAccountIndexError;
fn try_from(index: AccountIndex) -> Result<Self, Self::Error> {
index
.as_issuance_account_index()
.ok_or(TryFromAccountIndexError)
}
}
impl TryFrom<AccountIndex> for LeafAccountIndex {
type Error = TryFromAccountIndexError;
fn try_from(index: AccountIndex) -> Result<Self, Self::Error> {
index
.as_leaf_account_index()
.ok_or(TryFromAccountIndexError)
}
}
impl TryFrom<AccountIndex> for NonRootAccountIndex {
type Error = TryFromAccountIndexError;
fn try_from(index: AccountIndex) -> Result<Self, Self::Error> {
index
.as_non_root_account_index()
.ok_or(TryFromAccountIndexError)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum NonRootAccountIndex {
Issuance(IssuanceAccountIndex),
Leaf(LeafAccountIndex),
}
impl NonRootAccountIndex {
pub fn as_raw(self) -> RawAccountIndex {
match self {
Self::Issuance(index) => index.as_raw(),
Self::Leaf(index) => index.as_raw(),
}
}
pub fn as_issuance_account_index(self) -> Option<IssuanceAccountIndex> {
match self {
Self::Issuance(index) => Some(index),
Self::Leaf(_) => None,
}
}
pub fn as_leaf_account_index(self) -> Option<LeafAccountIndex> {
match self {
Self::Leaf(index) => Some(index),
Self::Issuance(_) => None,
}
}
pub fn is_issuance(self) -> bool {
match self {
Self::Leaf(_) => false,
Self::Issuance(_) => true,
}
}
pub fn is_leaf(self) -> bool {
match self {
Self::Leaf(_) => true,
Self::Issuance(_) => false,
}
}
}
impl From<IssuanceAccountIndex> for NonRootAccountIndex {
#[inline]
fn from(index: IssuanceAccountIndex) -> Self {
Self::Issuance(index)
}
}
impl From<LeafAccountIndex> for NonRootAccountIndex {
#[inline]
fn from(index: LeafAccountIndex) -> Self {
Self::Leaf(index)
}
}
impl TryFrom<NonRootAccountIndex> for IssuanceAccountIndex {
type Error = TryFromAccountIndexError;
fn try_from(index: NonRootAccountIndex) -> Result<Self, Self::Error> {
index
.as_issuance_account_index()
.ok_or(TryFromAccountIndexError)
}
}
impl TryFrom<NonRootAccountIndex> for LeafAccountIndex {
type Error = TryFromAccountIndexError;
fn try_from(index: NonRootAccountIndex) -> Result<Self, Self::Error> {
index
.as_leaf_account_index()
.ok_or(TryFromAccountIndexError)
}
}