use std::str::FromStr;
use borsh::{BorshDeserialize, BorshSerialize};
use derive_more::{Display, From, Into};
use near_primitives::account::id as near_account_id;
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
use solders_macros::{common_methods, pyhash, richcmp_eq_only};
use solders_traits::{common_methods_default, PyHash, RichcmpEqualityOnly};
use crate::{
error::exception::handle_py_value_err, py_from_bytes_general_via_borsh,
pybytes_general_via_borsh,
};
#[pyclass(module = "pyonear.account_id", subclass)]
#[derive(
Clone,
Debug,
PartialEq,
Eq,
From,
Into,
Hash,
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
Display,
)]
pub struct AccountId(pub near_account_id::AccountId);
impl RichcmpEqualityOnly for AccountId {}
pybytes_general_via_borsh!(AccountId);
py_from_bytes_general_via_borsh!(AccountId);
common_methods_default!(AccountId);
impl PyHash for AccountId {}
impl AsRef<near_account_id::AccountId> for AccountId {
fn as_ref(&self) -> &near_account_id::AccountId {
&self.0
}
}
#[richcmp_eq_only]
#[common_methods]
#[pyhash]
#[pymethods]
impl AccountId {
#[classattr]
pub const MIN_LEN: usize = near_account_id::AccountId::MIN_LEN;
#[classattr]
pub const MAX_LEN: usize = near_account_id::AccountId::MAX_LEN;
#[new]
pub fn new(account_id: &str) -> PyResult<Self> {
handle_py_value_err(near_account_id::AccountId::from_str(account_id))
}
pub fn is_top_level(&self) -> bool {
self.0.is_top_level()
}
pub fn is_sub_account_of(&self, parent: &AccountId) -> bool {
self.0.is_sub_account_of(parent.as_ref())
}
pub fn is_implicit(&self) -> bool {
self.0.is_implicit()
}
pub fn is_system(&self) -> bool {
self.0.is_system()
}
#[staticmethod]
pub fn validate(account_id: &str) -> PyResult<()> {
handle_py_value_err(near_account_id::AccountId::validate(account_id))
}
}
pub(crate) fn create_account_id_mod(py: Python<'_>) -> PyResult<&PyModule> {
let m = PyModule::new(py, "account_id")?;
m.add_class::<AccountId>()?;
Ok(m)
}