1use std::str::FromStr;
2
3use af_sui_types::{Address, IdentStr, Identifier, StructTag, TypeTag};
4use serde::{Deserialize, Serialize};
5
6use crate::{
7 MoveStruct,
8 MoveType,
9 ParseStructTagError,
10 StaticAddress,
11 StaticModule,
12 StaticName,
13 StaticStructTag as _,
14 StaticTypeParams,
15 StaticTypeTag,
16 StructTagError,
17 TypeParamsError,
18 TypeTagError,
19};
20
21#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, PartialOrd, Ord, Serialize)]
22pub struct StringTypeTag;
23
24impl From<StringTypeTag> for TypeTag {
25 fn from(value: StringTypeTag) -> Self {
26 Self::Struct(Box::new(value.into()))
27 }
28}
29
30impl TryFrom<TypeTag> for StringTypeTag {
31 type Error = TypeTagError;
32
33 fn try_from(value: TypeTag) -> Result<Self, Self::Error> {
34 match value {
35 TypeTag::Struct(stag) => Ok((*stag).try_into()?),
36 other => Err(TypeTagError::Variant {
37 expected: "Struct(_)".to_owned(),
38 got: other,
39 }),
40 }
41 }
42}
43
44impl From<StringTypeTag> for StructTag {
45 fn from(_: StringTypeTag) -> Self {
46 String::struct_tag()
47 }
48}
49
50impl TryFrom<StructTag> for StringTypeTag {
51 type Error = StructTagError;
52
53 fn try_from(value: StructTag) -> Result<Self, Self::Error> {
54 use StructTagError::*;
55 let StructTag {
56 address,
57 module,
58 name,
59 type_params,
60 } = value;
61 let expected = String::struct_tag();
62 if address != expected.address {
63 return Err(Address {
64 expected: expected.address,
65 got: address,
66 });
67 }
68 if module != expected.module {
69 return Err(Module {
70 expected: expected.module,
71 got: module,
72 });
73 }
74 if name != expected.name {
75 return Err(Name {
76 expected: expected.name,
77 got: name,
78 });
79 }
80 if !type_params.is_empty() {
81 return Err(TypeParams(TypeParamsError::Number {
82 expected: 0,
83 got: type_params.len(),
84 }));
85 }
86 Ok(Self)
87 }
88}
89
90impl FromStr for StringTypeTag {
91 type Err = ParseStructTagError;
92
93 fn from_str(s: &str) -> Result<Self, Self::Err> {
94 let stag: StructTag = s.parse()?;
95 Ok(stag.try_into()?)
96 }
97}
98
99impl MoveType for String {
100 type TypeTag = StringTypeTag;
101}
102
103impl MoveStruct for String {
104 type StructTag = StringTypeTag;
105}
106
107impl StaticTypeTag for String {
108 fn type_() -> Self::TypeTag {
109 StringTypeTag {}
110 }
111}
112
113impl StaticAddress for String {
114 fn address() -> Address {
115 Address::new(af_sui_types::hex_address_bytes(b"0x1"))
116 }
117}
118
119impl StaticModule for String {
120 fn module() -> Identifier {
121 IdentStr::cast("string").to_owned()
122 }
123}
124
125impl StaticName for String {
126 fn name() -> Identifier {
127 IdentStr::cast("String").to_owned()
128 }
129}
130
131impl StaticTypeParams for String {
132 fn type_params() -> Vec<TypeTag> {
133 vec![]
134 }
135}