af_move_type/
string.rs

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 expected = String::struct_tag();
56        if *value.address() != *expected.address() {
57            return Err(Address {
58                expected: *expected.address(),
59                got: *value.address(),
60            });
61        }
62        if value.module() != expected.module() {
63            return Err(Module {
64                expected: expected.module().clone(),
65                got: value.module().clone(),
66            });
67        }
68        if value.name() != expected.name() {
69            return Err(Name {
70                expected: expected.name().clone(),
71                got: value.name().clone(),
72            });
73        }
74        if !value.type_params().is_empty() {
75            return Err(TypeParams(TypeParamsError::Number {
76                expected: 0,
77                got: value.type_params().len(),
78            }));
79        }
80        Ok(Self)
81    }
82}
83
84impl FromStr for StringTypeTag {
85    type Err = ParseStructTagError;
86
87    fn from_str(s: &str) -> Result<Self, Self::Err> {
88        let stag: StructTag = s.parse()?;
89        Ok(stag.try_into()?)
90    }
91}
92
93impl MoveType for String {
94    type TypeTag = StringTypeTag;
95}
96
97impl MoveStruct for String {
98    type StructTag = StringTypeTag;
99}
100
101impl StaticTypeTag for String {
102    fn type_() -> Self::TypeTag {
103        StringTypeTag {}
104    }
105}
106
107impl StaticAddress for String {
108    fn address() -> Address {
109        Address::from_static("0x1")
110    }
111}
112
113impl StaticModule for String {
114    fn module() -> Identifier {
115        IdentStr::cast("string").to_owned()
116    }
117}
118
119impl StaticName for String {
120    fn name() -> Identifier {
121        IdentStr::cast("String").to_owned()
122    }
123}
124
125impl StaticTypeParams for String {
126    fn type_params() -> Vec<TypeTag> {
127        vec![]
128    }
129}