1use crate::Nep245TokenId as TokenId;
2use std::{fmt, str::FromStr};
3
4use near_account_id::AccountId;
5
6use crate::{TokenIdType, error::TokenIdError};
7
8#[cfg_attr(any(feature = "arbitrary", test), derive(::arbitrary::Arbitrary))]
10#[cfg_attr(
11 feature = "borsh",
12 derive(::borsh::BorshSerialize, ::borsh::BorshDeserialize),
13 cfg_attr(feature = "abi", derive(::borsh::BorshSchema))
14)]
15#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct ImtTokenId {
17 pub minter_id: AccountId,
18
19 pub token_id: TokenId,
20}
21
22impl ImtTokenId {
23 pub fn new(minter_id: impl Into<AccountId>, token_id: impl Into<TokenId>) -> Self {
24 Self {
25 minter_id: minter_id.into(),
26 token_id: token_id.into(),
27 }
28 }
29}
30
31impl std::fmt::Debug for ImtTokenId {
32 #[inline]
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 write!(f, "{}:{}", &self.minter_id, &self.token_id)
35 }
36}
37
38impl std::fmt::Display for ImtTokenId {
39 #[inline]
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 fmt::Debug::fmt(&self, f)
42 }
43}
44
45impl FromStr for ImtTokenId {
46 type Err = TokenIdError;
47
48 fn from_str(data: &str) -> Result<Self, Self::Err> {
49 let (minter_id, token_id) = data
50 .split_once(':')
51 .ok_or(strum::ParseError::VariantNotFound)?;
52 Ok(Self::new(minter_id.parse::<AccountId>()?, token_id))
53 }
54}
55
56impl From<&ImtTokenId> for TokenIdType {
57 #[inline]
58 fn from(_: &ImtTokenId) -> Self {
59 Self::Imt
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 use defuse_test_utils::random::make_arbitrary;
68 use rstest::rstest;
69
70 #[rstest]
71 #[trace]
72 fn display_from_str_roundtrip(#[from(make_arbitrary)] token_id: ImtTokenId) {
73 let s = token_id.to_string();
74 let got: ImtTokenId = s.parse().unwrap();
75 assert_eq!(got, token_id);
76 }
77}