sui_gql_client/queries/model/
outputs.rs

1use af_sui_types::{StructTag, TypeTag, encode_base64_default};
2use derive_more::Display;
3use enum_as_inner::EnumAsInner;
4
5use crate::queries::model::fragments::ObjectKey;
6
7/// An instance of a dynamic field or dynamic object.
8#[derive(Clone, Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash, EnumAsInner)]
9pub enum DynamicField {
10    /// The object key (id, version) and the raw struct contents.
11    ///
12    /// The object key is useful to query further data about the object later.
13    /// It is also guaranteed that the contents are of a struct, since enums cannot be top-level
14    /// objects.
15    ///
16    /// Reference: <https://move-book.com/reference/enums.html>
17    #[display("DOF {{\n\t{_0:?}: {_1}\n}})")]
18    Object(ObjectKey, RawMoveStruct),
19    /// The raw Move value contents. Could be a primitive type, struct, or enum.
20    #[display("DF {{\n\t{_0}\n}})")]
21    Field(RawMoveValue),
22}
23
24/// Raw representation of a Move value
25#[derive(Clone, Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[display("{type_} {{\n\t{}\n}}", encode_base64_default(bcs))]
27pub struct RawMoveValue {
28    pub type_: TypeTag,
29    pub bcs: Vec<u8>,
30}
31
32impl<T: af_move_type::MoveType> TryFrom<RawMoveValue> for af_move_type::MoveInstance<T> {
33    type Error = af_move_type::FromRawTypeError;
34    fn try_from(RawMoveValue { type_, bcs }: RawMoveValue) -> Result<Self, Self::Error> {
35        Self::from_raw_type(type_, &bcs)
36    }
37}
38
39impl<T: af_move_type::MoveType> TryFrom<af_move_type::MoveInstance<T>> for RawMoveValue {
40    type Error = bcs::Error;
41    fn try_from(value: af_move_type::MoveInstance<T>) -> Result<Self, Self::Error> {
42        Ok(Self {
43            type_: value.type_.into(),
44            bcs: value.value.to_bcs()?,
45        })
46    }
47}
48
49/// Raw representation of a Move struct
50#[derive(Clone, Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)]
51#[display("{type_} {{\n\t{}\n}}", encode_base64_default(bcs))]
52pub struct RawMoveStruct {
53    pub type_: StructTag,
54    pub bcs: Vec<u8>,
55}
56
57impl<T: af_move_type::MoveStruct> TryFrom<RawMoveStruct> for af_move_type::MoveInstance<T> {
58    type Error = af_move_type::FromRawStructError;
59    fn try_from(RawMoveStruct { type_, bcs }: RawMoveStruct) -> Result<Self, Self::Error> {
60        Self::from_raw_struct(type_, &bcs)
61    }
62}
63
64impl<T: af_move_type::MoveStruct> TryFrom<af_move_type::MoveInstance<T>> for RawMoveStruct {
65    type Error = bcs::Error;
66    fn try_from(value: af_move_type::MoveInstance<T>) -> Result<Self, Self::Error> {
67        Ok(Self {
68            type_: value.type_.into(),
69            bcs: value.value.to_bcs()?,
70        })
71    }
72}