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