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, MoveTypeTag, 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 MoveTypeTag for AnyTTypeTag {}
73
74impl FromStr for AnyTTypeTag {
75    type Err = ParseTypeTagError;
76
77    fn from_str(s: &str) -> Result<Self, Self::Err> {
78        let tag: TypeTag = s.parse()?;
79        Ok(Self(tag))
80    }
81}
82
83impl std::fmt::Display for AnyTTypeTag {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(f, "{}", self.0)
86    }
87}
88
89impl Serialize for AnyTTypeTag {
90    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
91        ser.collect_str(&self.0)
92    }
93}
94
95impl<'de> Deserialize<'de> for AnyTTypeTag {
96    fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
97        use serde::de::Error as _;
98        let s = String::deserialize(de)?;
99        s.parse().map_err(D::Error::custom)
100    }
101}
102
103impl MoveType for AnyT {
104    type TypeTag = AnyTTypeTag;
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    /// `AnyTTypeTag` accepts a struct tag whose own type params carry
112    /// phantom-paramed structs — the exact case `Otw` rejects.
113    #[test]
114    fn accepts_phantom_paramed_struct_tag() {
115        let raw: TypeTag = "0x1::authority::AuthorityCap<0x1::authority::VENDOR<0x2::aftermath::AFTERMATH>, 0x1::authority::ASSISTANT>"
116            .parse()
117            .unwrap();
118        let wrapped = AnyTTypeTag::try_from(raw.clone()).unwrap();
119        let back: TypeTag = wrapped.into();
120        assert_eq!(back, raw);
121    }
122
123    /// `AnyTTypeTag` is permissive over `TypeTag` variants — not just
124    /// `Struct(_)` — unlike a `MoveStruct`'s derived companion.
125    #[test]
126    fn accepts_non_struct_type_tag() {
127        for raw in [TypeTag::U64, TypeTag::Bool, TypeTag::Address] {
128            let wrapped = AnyTTypeTag::try_from(raw.clone()).unwrap();
129            assert_eq!(TypeTag::from(wrapped), raw);
130        }
131    }
132
133    /// `Display` / `FromStr` round-trip through the canonical TypeTag form.
134    #[test]
135    fn display_fromstr_roundtrip() {
136        let raw: TypeTag = "0x1::a::B<0x2::c::D<0x3::e::F>>".parse().unwrap();
137        let wrapped = AnyTTypeTag(raw.clone());
138        let s = wrapped.to_string();
139        let parsed = AnyTTypeTag::from_str(&s).unwrap();
140        assert_eq!(parsed.0, raw);
141    }
142
143    /// `Serialize` / `Deserialize` round-trip via JSON.
144    #[test]
145    fn serde_roundtrip_json() {
146        let raw: TypeTag = "0x1::a::B<0x2::c::D<0x3::e::F>>".parse().unwrap();
147        let wrapped = AnyTTypeTag(raw.clone());
148        let json = serde_json::to_string(&wrapped).unwrap();
149        let back: AnyTTypeTag = serde_json::from_str(&json).unwrap();
150        assert_eq!(back.0, raw);
151    }
152}