Skip to main content

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