sui_graphql_rpc/types/
datatype.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use async_graphql::*;
5
6use super::move_enum::MoveEnum;
7use super::move_module::MoveModule;
8use super::move_struct::{MoveStruct, MoveStructTypeParameter};
9use super::open_move_type::MoveAbility;
10
11/// Interface implemented by all GraphQL types that represent a Move datatype (either structs or
12/// enums). This interface is used to provide a way to access fields that are shared by both
13/// structs and enums, e.g., the module that the datatype belongs to, the name of the datatype,
14/// type parameters etc.
15#[derive(Interface)]
16#[graphql(
17    name = "IMoveDatatype",
18    field(
19        name = "module",
20        ty = "MoveModule",
21        desc = "The module that the datatype belongs to."
22    ),
23    field(name = "name", ty = "String", desc = "The name of the datatype."),
24    field(
25        name = "abilities",
26        ty = "Option<&Vec<MoveAbility>>",
27        desc = "The abilities of the datatype."
28    ),
29    field(
30        name = "type_parameters",
31        ty = "Option<&Vec<MoveStructTypeParameter>>",
32        desc = "The type parameters of the datatype."
33    )
34)]
35pub(crate) enum IMoveDatatype {
36    Datatype(MoveDatatype),
37    Struct(MoveStruct),
38    Enum(MoveEnum),
39}
40
41pub(crate) enum MoveDatatype {
42    Struct(MoveStruct),
43    Enum(MoveEnum),
44}
45
46/// The generic representation of a Move datatype (either a struct or an enum) which exposes common
47/// fields and information (module, name, abilities, type parameters etc.) that is shared across
48/// them.
49#[Object]
50impl MoveDatatype {
51    async fn module(&self, ctx: &Context<'_>) -> Result<MoveModule> {
52        match self {
53            MoveDatatype::Struct(s) => s.module(ctx).await,
54            MoveDatatype::Enum(e) => e.module(ctx).await,
55        }
56    }
57
58    async fn name(&self, ctx: &Context<'_>) -> Result<&str> {
59        match self {
60            MoveDatatype::Struct(s) => s.name(ctx).await,
61            MoveDatatype::Enum(e) => e.name(ctx).await,
62        }
63    }
64
65    async fn abilities(&self, ctx: &Context<'_>) -> Result<Option<&Vec<MoveAbility>>> {
66        match self {
67            MoveDatatype::Struct(s) => s.abilities(ctx).await,
68            MoveDatatype::Enum(e) => e.abilities(ctx).await,
69        }
70    }
71
72    async fn type_parameters(
73        &self,
74        ctx: &Context<'_>,
75    ) -> Result<Option<&Vec<MoveStructTypeParameter>>> {
76        match self {
77            MoveDatatype::Struct(s) => s.type_parameters(ctx).await,
78            MoveDatatype::Enum(e) => e.type_parameters(ctx).await,
79        }
80    }
81
82    async fn as_move_enum(&self) -> Option<&MoveEnum> {
83        match self {
84            MoveDatatype::Enum(e) => Some(e),
85            _ => None,
86        }
87    }
88
89    async fn as_move_struct(&self) -> Option<&MoveStruct> {
90        match self {
91            MoveDatatype::Struct(s) => Some(s),
92            _ => None,
93        }
94    }
95}
96
97impl From<MoveStruct> for MoveDatatype {
98    fn from(value: MoveStruct) -> Self {
99        MoveDatatype::Struct(value)
100    }
101}
102
103impl From<MoveEnum> for MoveDatatype {
104    fn from(value: MoveEnum) -> Self {
105        MoveDatatype::Enum(value)
106    }
107}