1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::{
    format::{gen_doc_string, gen_doc_string_opt},
    CodeText,
};

use super::{script_function::ScriptFunctionType, Codegen, CodegenContext};
use anyhow::*;
use move_idl::{IDLAbility, IDLError, IDLModule, IDLScriptFunction};
use serde::Serialize;
use std::collections::BTreeMap;

const PRELUDE: &str = "import * as p from \"@movingco/prelude\";\n";

#[derive(Serialize)]
struct ErrorInfo {
    /// Error code.
    code: u64,
    /// Error.
    #[serde(flatten)]
    error: IDLError,
}

pub struct IDLModuleGenerator<'info> {
    pub module: &'info IDLModule,
    pub script_fns: Vec<ScriptFunctionType<'info>>,
}

impl<'info> IDLModuleGenerator<'info> {
    pub fn new(module: &'info IDLModule) -> Self {
        let script_fns = module
            .functions
            .iter()
            .map(|script_fn| ScriptFunctionType::new(module, script_fn))
            .collect::<Vec<_>>();
        IDLModuleGenerator { module, script_fns }
    }

    fn generate_module_header(&self) -> String {
        format!("**Module ID:** `{}`", self.module.module_id)
    }

    pub fn generate_module_doc(&self) -> String {
        gen_doc_string(
            &[
                self.module.doc.clone().unwrap_or_default(),
                self.generate_module_header(),
                "@module".to_string(),
            ]
            .into_iter()
            .filter(|s| !s.is_empty())
            .collect::<Vec<_>>()
            .join("\n\n"),
        )
    }

    pub fn has_entrypoints(&self) -> bool {
        !self.module.functions.is_empty()
    }

    pub fn generate_entrypoint_bodies(&self, ctx: &CodegenContext) -> Result<CodeText> {
        ctx.try_join(&self.script_fns)
    }

    pub fn generate_entrypoint_module(&self, ctx: &CodegenContext) -> Result<CodeText> {
        Ok(format!(
            "{}{}\nimport * as mod from './index.js';\nimport * as payloads from './payloads.js';\n{}",
            gen_doc_string("Entrypoint builders.\n\n@module"),
            PRELUDE,
            self.generate_entrypoint_bodies(ctx)?
        )
        .into())
    }

    pub fn generate_idl_module(&self) -> Result<CodeText> {
        Ok(format!(
            "{}{}export const idl = {} as const;",
            gen_doc_string("The IDL of the module.\n\n@module"),
            gen_doc_string("The IDL of the module."),
            &serde_json::to_string(self.module)?,
        )
        .into())
    }

    pub fn generate_errors_module(&self) -> Result<Option<CodeText>> {
        if self.module.errors.is_empty() {
            return Ok(None);
        }

        let errors = self
            .module
            .errors
            .iter()
            .map(|(code, error)| -> Result<CodeText> {
                Ok(format!(
                    "{}export const {} = {} as const;",
                    gen_doc_string_opt(&error.doc),
                    error.name,
                    serde_json::to_string_pretty(&ErrorInfo {
                        code: *code,
                        error: error.clone()
                    })?
                )
                .into())
            })
            .collect::<Result<Vec<CodeText>>>()?;

        Ok(Some(
            format!(
                "{}{}",
                gen_doc_string("Module errors.\n\n@module"),
                CodeText::try_join_with_separator(&errors, "\n\n")?
            )
            .into(),
        ))
    }

    pub fn generate_entry_payloads_module(&self, ctx: &CodegenContext) -> Result<CodeText> {
        Ok(ctx
            .try_join(
                &self
                    .script_fns
                    .iter()
                    .map(|f| f.generate_entry_payload_struct(ctx))
                    .collect::<Result<Vec<_>>>()?,
            )?
            .module_docs("Entrypoint script function payloads."))
    }

    pub fn generate_entry_names_module(&self, ctx: &CodegenContext) -> Result<CodeText> {
        Ok(ctx
            .try_join(
                &self
                    .script_fns
                    .iter()
                    .map(|f| {
                        Ok(CodeText::new_const_export(f.name(), &f.full_name())?
                            .docs(&format!("Script function type for `{}`.", f.full_name())))
                    })
                    .collect::<Result<Vec<CodeText>>>()?,
            )?
            .module_docs("Names of all script functions."))
    }
}

impl Codegen for IDLModule {
    fn generate_typescript(&self, ctx: &CodegenContext) -> Result<String> {
        let gen = IDLModuleGenerator::new(self);
        let name = self.module_id.name();

        let function_payloads = ctx.try_join(
            &gen.script_fns
                .iter()
                .filter_map(|f| {
                    if f.should_render_payload_struct() {
                        Some(f.payload())
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>(),
        )?;

        let struct_types = ctx.try_join(&self.structs)?;

        let mut fn_map: BTreeMap<String, IDLScriptFunction> = BTreeMap::new();
        for script_fn in self.functions.iter() {
            fn_map.insert(script_fn.name.clone(), script_fn.clone());
        }

        let mut resources: BTreeMap<String, String> = BTreeMap::new();
        for struct_info in self
            .structs
            .iter()
            .filter(|s| s.abilities.contains(&IDLAbility::Key))
        {
            resources.insert(
                struct_info.name.name.to_string(),
                struct_info.name.to_string(),
            );
        }

        let mut structs: BTreeMap<String, String> = BTreeMap::new();
        for struct_info in self.structs.iter() {
            structs.insert(
                struct_info.name.name.to_string(),
                struct_info.name.to_string(),
            );
        }

        let ts = format!(
            r#"{}{}

{}

{}

{}

export {{ idl }} from "./idl.js";

/** The address of the module. */
export const ADDRESS = "{}" as const;
/** The full module name. */
export const FULL_NAME = "{}" as const;
/** The name of the module. */
export const NAME = "{}" as const;

/** Module ID information. */
export const id = {{
  ADDRESS,
  FULL_NAME,
  NAME,
}} as const;

{}

/** Module error codes. */
export const errorCodes = {} as const;

/** All module function IDLs. */
export const functions = {} as const;

/** All struct types with ability `key`. */
export const resources = {} as const;

/** All struct types. */
export const structs = {} as const;

/** Payload generators for module `{}`. */
const moduleImpl = {{
  ...id,
  errorCodes,
  functions,
  resources,
  structs,
}} as const;

{}export const moduleDefinition = moduleImpl as p.MoveModuleDefinition<"{}", "{}"> as typeof moduleImpl;
"#,
            gen.generate_module_doc(),
            PRELUDE,
            struct_types,
            function_payloads,
            if gen.has_entrypoints() {
                CodeText::try_join_with_separator(
                    &[
                        CodeText::new_reexport("entry", "./entry.js"),
                        CodeText::new_reexport("payloads", "./payloads.js"),
                        CodeText::new_reexport("entryNames", "./entryNames.js"),
                    ],
                    "\n",
                )?
            } else {
                CodeText::new("")
            },
            self.module_id.address().to_hex_literal(),
            self.module_id.short_str_lossless(),
            self.module_id.name(),
            if !gen.module.errors.is_empty() {
                "export * as errors from \"./errors.js\";"
            } else {
                ""
            },
            serde_json::to_string_pretty(&self.errors)?,
            serde_json::to_string_pretty(&fn_map)?,
            serde_json::to_string_pretty(&resources)?,
            serde_json::to_string_pretty(&structs)?,
            self.module_id.short_str_lossless(),
            gen_doc_string_opt(&self.doc),
            self.module_id.address().to_hex_literal(),
            name,
        );

        Ok(ts)
    }
}