Skip to main content

af_move_type/
any.rs

1//! Permissive type marker that accepts any Move type in its slot.
2
3use std::str::FromStr;
4
5use af_sui_types::TypeTag;
6use serde::{Deserialize, Deserializer, Serialize, Serializer};
7
8use crate::{MoveType, ParseTypeTagError, TypeTagError};
9
10/// Generic type that accepts **any** Move type argument in its slot,
11/// including phantom-paramed generics such as `VENDOR<X>` as well as
12/// non-struct types (primitives, vectors).
13///
14/// Where [`crate::otw::Otw`] requires `n_types_expected == 0` and therefore
15/// rejects phantom-paramed arguments, `AnyT` performs no validation — its
16/// associated [`AnyTTypeTag`] simply captures the raw [`TypeTag`] so it
17/// can be read back unchanged.
18///
19/// `AnyT` is intended for **phantom slots only** in generic Move types
20/// (e.g. `AuthorityCap<AnyT, AnyT>`). It is never instantiated as a real
21/// value; its [`MoveType`] impl exists so the derive-generated parsers can
22/// thread phantom-paramed type arguments through unchanged.
23///
24/// Note: `AnyT` deliberately does **not** implement [`MoveStruct`] —
25/// `MoveStructTag: TryFrom<StructTag>` is incompatible with carrying an
26/// arbitrary [`TypeTag`] (which may be non-struct). Use [`crate::otw::Otw`]
27/// when you need a `MoveStruct`-bounded slot type.
28///
29/// Unlike `Otw`, `AnyT` does **not** implement [`crate::StaticTypeTag`] or
30/// its `Static*` siblings: it has no statically known type tag, by design.
31/// Use it on the runtime decode path (e.g.
32/// [`MoveInstance::from_raw_type`](crate::MoveInstance::from_raw_type)).
33///
34/// [`MoveStruct`]: crate::MoveStruct
35#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq, Hash)]
36pub struct AnyT {
37    dummy_field: bool,
38}
39
40impl AnyT {
41    pub fn new() -> Self {
42        Self::default()
43    }
44}
45
46impl std::fmt::Display for AnyT {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "AnyT")
49    }
50}
51
52/// `TypeTag` companion for [`AnyT`]. Wraps the raw [`TypeTag`] from the slot
53/// with **no** validation of variant, address, module, name, or type-param
54/// count.
55#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
56pub struct AnyTTypeTag(pub TypeTag);
57
58impl From<AnyTTypeTag> for TypeTag {
59    fn from(value: AnyTTypeTag) -> Self {
60        value.0
61    }
62}
63
64impl TryFrom<TypeTag> for AnyTTypeTag {
65    type Error = TypeTagError;
66
67    fn try_from(value: TypeTag) -> Result<Self, Self::Error> {
68        Ok(Self(value))
69    }
70}
71
72impl FromStr for AnyTTypeTag {
73    type Err = ParseTypeTagError;
74
75    fn from_str(s: &str) -> Result<Self, Self::Err> {
76        let tag: TypeTag = s.parse()?;
77        Ok(Self(tag))
78    }
79}
80
81impl std::fmt::Display for AnyTTypeTag {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(f, "{}", self.0)
84    }
85}
86
87impl Serialize for AnyTTypeTag {
88    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
89        ser.collect_str(&self.0)
90    }
91}
92
93impl<'de> Deserialize<'de> for AnyTTypeTag {
94    fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
95        use serde::de::Error as _;
96        let s = String::deserialize(de)?;
97        s.parse().map_err(D::Error::custom)
98    }
99}
100
101impl MoveType for AnyT {
102    type TypeTag = AnyTTypeTag;
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    /// `AnyTTypeTag` accepts a struct tag whose own type params carry
110    /// phantom-paramed structs — the exact case `Otw` rejects.
111    #[test]
112    fn accepts_phantom_paramed_struct_tag() {
113        let raw: TypeTag = "0x1::authority::AuthorityCap<0x1::authority::VENDOR<0x2::aftermath::AFTERMATH>, 0x1::authority::ASSISTANT>"
114            .parse()
115            .unwrap();
116        let wrapped = AnyTTypeTag::try_from(raw.clone()).unwrap();
117        let back: TypeTag = wrapped.into();
118        assert_eq!(back, raw);
119    }
120
121    /// `AnyTTypeTag` is permissive over `TypeTag` variants — not just
122    /// `Struct(_)` — unlike a `MoveStruct`'s derived companion.
123    #[test]
124    fn accepts_non_struct_type_tag() {
125        for raw in [TypeTag::U64, TypeTag::Bool, TypeTag::Address] {
126            let wrapped = AnyTTypeTag::try_from(raw.clone()).unwrap();
127            assert_eq!(TypeTag::from(wrapped), raw);
128        }
129    }
130
131    /// `Display` / `FromStr` round-trip through the canonical TypeTag form.
132    #[test]
133    fn display_fromstr_roundtrip() {
134        let raw: TypeTag = "0x1::a::B<0x2::c::D<0x3::e::F>>".parse().unwrap();
135        let wrapped = AnyTTypeTag(raw.clone());
136        let s = wrapped.to_string();
137        let parsed = AnyTTypeTag::from_str(&s).unwrap();
138        assert_eq!(parsed.0, raw);
139    }
140
141    /// `Serialize` / `Deserialize` round-trip via JSON.
142    #[test]
143    fn serde_roundtrip_json() {
144        let raw: TypeTag = "0x1::a::B<0x2::c::D<0x3::e::F>>".parse().unwrap();
145        let wrapped = AnyTTypeTag(raw.clone());
146        let json = serde_json::to_string(&wrapped).unwrap();
147        let back: AnyTTypeTag = serde_json::from_str(&json).unwrap();
148        assert_eq!(back.0, raw);
149    }
150}