defuse_token_id/
nep141.rs1use std::{fmt, str::FromStr};
2
3use near_sdk::{AccountId, near};
4use serde_with::{DeserializeFromStr, SerializeDisplay};
5
6use crate::{TokenIdType, error::TokenIdError};
7
8#[cfg_attr(any(feature = "arbitrary", test), derive(::arbitrary::Arbitrary))]
9#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, SerializeDisplay, DeserializeFromStr)]
10#[near(serializers = [borsh])]
11pub struct Nep141TokenId {
12 pub contract_id: AccountId,
13}
14
15impl Nep141TokenId {
16 pub fn new(contract_id: impl Into<AccountId>) -> Self {
17 Self {
18 contract_id: contract_id.into(),
19 }
20 }
21}
22
23impl std::fmt::Debug for Nep141TokenId {
24 #[inline]
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(f, "{}", &self.contract_id)
27 }
28}
29
30impl std::fmt::Display for Nep141TokenId {
31 #[inline]
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 fmt::Debug::fmt(&self, f)
34 }
35}
36
37impl FromStr for Nep141TokenId {
38 type Err = TokenIdError;
39
40 fn from_str(data: &str) -> Result<Self, Self::Err> {
41 Ok(Self {
42 contract_id: data.parse()?,
43 })
44 }
45}
46
47impl From<&Nep141TokenId> for TokenIdType {
48 #[inline]
49 fn from(_: &Nep141TokenId) -> Self {
50 Self::Nep141
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 use defuse_test_utils::random::make_arbitrary;
59 use rstest::rstest;
60
61 #[rstest]
62 #[trace]
63 fn display_from_str_roundtrip(#[from(make_arbitrary)] token_id: Nep141TokenId) {
64 let s = token_id.to_string();
65 let got: Nep141TokenId = s.parse().unwrap();
66 assert_eq!(got, token_id);
67 }
68}