Skip to main content

moverox_sui/
sui_sdk.rs

1/// Conversions between [`sui_sdk_types`] and [`moverox::types`].
2#[sealed::sealed]
3pub trait Compat {
4    type To;
5
6    fn from_sui(value: Self::To) -> Self;
7
8    fn into_sui(self) -> Self::To;
9}
10
11#[sealed::sealed]
12impl Compat for moverox::types::Address {
13    type To = sui_sdk_types::Address;
14
15    fn from_sui(value: Self::To) -> Self {
16        Self::new(value.into_inner())
17    }
18
19    fn into_sui(self) -> Self::To {
20        sui_sdk_types::Address::new(self.into_inner())
21    }
22}
23
24#[sealed::sealed]
25impl Compat for moverox::types::Identifier {
26    type To = sui_sdk_types::Identifier;
27
28    fn from_sui(value: Self::To) -> Self {
29        Self::new(value.as_str()).expect("Compatible identifiers")
30    }
31
32    fn into_sui(self) -> Self::To {
33        sui_sdk_types::Identifier::new(self.into_inner()).expect("Compatible identifiers")
34    }
35}
36
37#[sealed::sealed]
38impl Compat for moverox::types::StructTag {
39    type To = sui_sdk_types::StructTag;
40
41    fn from_sui(value: Self::To) -> Self {
42        Self {
43            address: Compat::from_sui(*value.address()),
44            module: Compat::from_sui(value.module().clone()),
45            name: Compat::from_sui(value.name().clone()),
46            type_params: value
47                .type_params()
48                .iter()
49                .cloned()
50                .map(Compat::from_sui)
51                .collect(),
52        }
53    }
54
55    fn into_sui(self) -> Self::To {
56        let Self {
57            address,
58            module,
59            name,
60            type_params,
61        } = self;
62        sui_sdk_types::StructTag::new(
63            address.into_sui(),
64            module.into_sui(),
65            name.into_sui(),
66            type_params.into_iter().map(Compat::into_sui).collect(),
67        )
68    }
69}
70
71#[sealed::sealed]
72impl Compat for moverox::types::TypeTag {
73    type To = sui_sdk_types::TypeTag;
74    fn from_sui(value: Self::To) -> Self {
75        use sui_sdk_types::TypeTag as T;
76        match value {
77            T::U8 => Self::U8,
78            T::U16 => Self::U16,
79            T::U32 => Self::U32,
80            T::U64 => Self::U64,
81            T::U128 => Self::U128,
82            T::U256 => Self::U256,
83            T::Bool => Self::Bool,
84            T::Address => Self::Address,
85            T::Signer => Self::Signer,
86            T::Vector(t) => Self::Vector(Box::new(Compat::from_sui(*t))),
87            T::Struct(s) => Self::Struct(Box::new(Compat::from_sui(*s))),
88        }
89    }
90
91    fn into_sui(self) -> Self::To {
92        use sui_sdk_types::TypeTag as T;
93        match self {
94            Self::U8 => T::U8,
95            Self::U16 => T::U16,
96            Self::U32 => T::U32,
97            Self::U64 => T::U64,
98            Self::U128 => T::U128,
99            Self::U256 => T::U256,
100            Self::Bool => T::Bool,
101            Self::Address => T::Address,
102            Self::Signer => T::Signer,
103            Self::Vector(t) => T::Vector(Box::new(t.into_sui())),
104            Self::Struct(s) => T::Struct(Box::new(s.into_sui())),
105        }
106    }
107}