iota_graphql_rpc/types/
datatype.rs

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