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
use anyhow::*;
use docstring::normalize_doc_string;
use move_binary_format::file_format::Ability;
use move_core_types::{identifier::Identifier, language_storage::StructTag};
use move_idl_types::{IDLAbility, IDLField, IDLStruct, IDLTypeParam};
use move_model::model::{GlobalEnv, StructEnv};
use crate::convert::get_idl_type_for_type;
pub fn generate_idl_for_struct(env: &GlobalEnv, struct_env: &StructEnv) -> Result<IDLStruct> {
let symbol_pool = env.symbol_pool();
let fields = struct_env
.get_fields()
.map(|field| {
Ok(IDLField {
name: symbol_pool.string(field.get_name()).to_string(),
doc: normalize_doc_string(field.get_doc()),
ty: get_idl_type_for_type(env, &field.get_type())?,
})
})
.collect::<Result<Vec<_>>>()?;
let type_params: Vec<IDLTypeParam> = struct_env
.get_named_type_parameters()
.iter()
.enumerate()
.map(|(i, tp)| IDLTypeParam {
name: symbol_pool.string(tp.0).to_string(),
is_phantom: struct_env.is_phantom_parameter(i),
})
.collect();
let module_id = &struct_env.module_env.get_verified_module().self_id();
Ok(IDLStruct {
name: StructTag {
address: *module_id.address(),
module: module_id.name().to_owned(),
name: Identifier::new(symbol_pool.string(struct_env.get_name()).to_string())?,
type_params: vec![],
}
.into(),
doc: normalize_doc_string(struct_env.get_doc()),
fields,
type_params,
abilities: struct_env
.get_abilities()
.into_iter()
.map(|a| match a {
Ability::Copy => IDLAbility::Copy,
Ability::Drop => IDLAbility::Drop,
Ability::Store => IDLAbility::Store,
Ability::Key => IDLAbility::Key,
})
.collect(),
})
}