af_move_type/
primitives.rs1use std::str::FromStr;
2
3use af_sui_types::{Address, TypeTag, U256};
4use serde::{Deserialize, Serialize};
5
6use crate::{MoveType, ParseTypeTagError, StaticTypeTag, TypeTagError};
7
8macro_rules! impl_primitive_type_tags {
9    ($($typ:ty: ($type_:ident, $variant:ident)),*) => {
10        $(
11            #[derive(
12                Clone,
13                Debug,
14                PartialEq,
15                Eq,
16                Hash,
17                Deserialize,
18                PartialOrd,
19                Ord,
20                Serialize
21            )]
22            pub struct $type_;
23
24            impl From<$type_> for TypeTag {
25                fn from(_value: $type_) -> Self {
26                    Self::$variant
27                }
28            }
29
30            impl TryFrom<TypeTag> for $type_ {
31                type Error = TypeTagError;
32
33                fn try_from(value: TypeTag) -> Result<Self, Self::Error> {
34                    match value {
35                        TypeTag::$variant => Ok(Self),
36                        _ => Err(TypeTagError::Variant {
37                            expected: stringify!($variant).to_owned(),
38                            got: value }
39                        )
40                    }
41                }
42            }
43
44            impl FromStr for $type_ {
45                type Err = ParseTypeTagError;
46
47                fn from_str(s: &str) -> Result<Self, Self::Err> {
48                    let tag: TypeTag = s.parse()?;
49                    Ok(tag.try_into()?)
50                }
51            }
52
53            impl MoveType for $typ {
54                type TypeTag = $type_;
55            }
56
57            impl StaticTypeTag for $typ {
58                fn type_() -> Self::TypeTag {
59                    $type_ {}
60                }
61            }
62        )*
63    };
64}
65
66impl_primitive_type_tags! {
67    Address: (AddressTypeTag, Address),
68    bool: (BoolTypeTag, Bool),
69    u8: (U8TypeTag, U8),
70    u16: (U16TypeTag, U16),
71    u32: (U32TypeTag, U32),
72    u64: (U64TypeTag, U64),
73    u128: (U128TypeTag, U128),
74    U256: (U256TypeTag, U256)
75}