Skip to content

Commit 9c53055

Browse files
committed
improving the JavaScript target with exporting of the IDL type objects and deprecations of the factory functions
1 parent bc3e680 commit 9c53055

File tree

16 files changed

+826
-19
lines changed

16 files changed

+826
-19
lines changed

rust/candid_parser/src/bindings/javascript.rs

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,27 @@ fn pp_defs<'a>(
200200
env: &'a TypeEnv,
201201
def_list: &'a [&'a str],
202202
recs: &'a BTreeSet<&'a str>,
203+
export: bool,
203204
) -> RcDoc<'a> {
204-
let recs_doc = lines(
205-
recs.iter()
206-
.map(|id| kwd("const").append(ident(id)).append(" = IDL.Rec();")),
207-
);
205+
let export_prefix = if export { str("export ") } else { RcDoc::nil() };
206+
207+
let recs_doc = lines(recs.iter().map(|id| {
208+
export_prefix
209+
.clone()
210+
.append(kwd("const"))
211+
.append(ident(id))
212+
.append(" = IDL.Rec();")
213+
}));
208214
let defs = lines(def_list.iter().map(|id| {
209215
let ty = env.find_type(id).unwrap();
210216
if recs.contains(id) {
211217
ident(id)
212218
.append(".fill")
213219
.append(enclose("(", pp_ty(ty), ");"))
214220
} else {
215-
kwd("const")
221+
export_prefix
222+
.clone()
223+
.append(kwd("const"))
216224
.append(ident(id))
217225
.append(" = ")
218226
.append(pp_ty(ty))
@@ -237,38 +245,84 @@ fn pp_actor<'a>(ty: &'a Type, recs: &'a BTreeSet<&'a str>) -> RcDoc<'a> {
237245
}
238246
}
239247

248+
fn pp_deprecation_comment<'a>() -> RcDoc<'a> {
249+
str("/**").append(RcDoc::hardline())
250+
.append(" * @deprecated Import IDL types directly from this module instead of using this factory function.")
251+
.append(RcDoc::hardline())
252+
.append(" */")
253+
.append(RcDoc::hardline())
254+
}
255+
240256
pub fn compile(env: &TypeEnv, actor: &Option<Type>) -> String {
241257
match actor {
242258
None => {
243259
let def_list: Vec<_> = env.0.iter().map(|pair| pair.0.as_ref()).collect();
244260
let recs = infer_rec(env, &def_list).unwrap();
245-
let doc = pp_defs(env, &def_list, &recs);
246-
doc.pretty(LINE_WIDTH).to_string()
261+
let import_doc = str("import { IDL } from '@dfinity/candid';");
262+
let doc = pp_defs(env, &def_list, &recs, true);
263+
let result = import_doc
264+
.append(RcDoc::hardline())
265+
.append(RcDoc::hardline())
266+
.append(doc)
267+
.pretty(LINE_WIDTH)
268+
.to_string();
269+
270+
result
247271
}
248272
Some(actor) => {
249273
let def_list = chase_actor(env, actor).unwrap();
250274
let recs = infer_rec(env, &def_list).unwrap();
251-
let defs = pp_defs(env, &def_list, &recs);
252275
let init = if let TypeInner::Class(ref args, _) = actor.as_ref() {
253276
args.as_slice()
254277
} else {
255278
&[][..]
256279
};
257-
let actor = kwd("return").append(pp_actor(actor, &recs)).append(";");
258-
let body = defs.append(actor);
259-
let doc = str("export const idlFactory = ({ IDL }) => ")
260-
.append(enclose_space("{", body, "};"));
261-
// export init args
280+
281+
let import_doc = str("import { IDL } from '@dfinity/candid';");
282+
let defs = pp_defs(env, &def_list, &recs, true);
283+
let actor = pp_actor(actor, &recs);
284+
285+
let idl_service = str("export const idlService = ")
286+
.append(actor.clone())
287+
.append(";");
288+
289+
let idl_init_args = str("export const idlInitArgs = ")
290+
.append(pp_rets(init))
291+
.append(";");
292+
293+
let idl_factory_return = kwd("return").append(actor).append(";");
294+
let idl_factory_body = pp_defs(env, &def_list, &recs, false).append(idl_factory_return);
295+
let idl_factory_doc = pp_deprecation_comment()
296+
.append(str("export const idlFactory = ({ IDL }) => "))
297+
.append(enclose_space("{", idl_factory_body, "};"));
298+
262299
let init_defs = chase_types(env, init).unwrap();
263300
let init_recs = infer_rec(env, &init_defs).unwrap();
264-
let init_defs_doc = pp_defs(env, &init_defs, &init_recs);
301+
let init_defs_doc = pp_defs(env, &init_defs, &init_recs, false);
265302
let init_doc = kwd("return").append(pp_rets(init)).append(";");
266303
let init_doc = init_defs_doc.append(init_doc);
267-
let init_doc =
268-
str("export const init = ({ IDL }) => ").append(enclose_space("{", init_doc, "};"));
304+
let init_doc = pp_deprecation_comment()
305+
.append(str("export const init = ({ IDL }) => "))
306+
.append(enclose_space("{", init_doc, "};"));
269307
let init_doc = init_doc.pretty(LINE_WIDTH).to_string();
270-
let doc = doc.append(RcDoc::hardline()).append(init_doc);
271-
doc.pretty(LINE_WIDTH).to_string()
308+
309+
let result = import_doc
310+
.append(RcDoc::hardline())
311+
.append(RcDoc::hardline())
312+
.append(defs)
313+
.append(RcDoc::hardline())
314+
.append(idl_service)
315+
.append(RcDoc::hardline())
316+
.append(RcDoc::hardline())
317+
.append(idl_init_args)
318+
.append(RcDoc::hardline())
319+
.append(RcDoc::hardline())
320+
.append(idl_factory_doc)
321+
.append(RcDoc::hardline())
322+
.append(RcDoc::hardline())
323+
.append(init_doc);
324+
325+
result.pretty(LINE_WIDTH).to_string()
272326
}
273327
}
274328
}

rust/candid_parser/tests/assets/ok/actor.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
import { IDL } from '@dfinity/candid';
2+
3+
export const o = IDL.Rec();
4+
export const f = IDL.Func([IDL.Int8], [IDL.Int8], []);
5+
export const h = IDL.Func([f], [f], []);
6+
export const g = f;
7+
o.fill(IDL.Opt(o));
8+
9+
export const idlService = IDL.Service({
10+
'f' : IDL.Func([IDL.Nat], [h], []),
11+
'g' : f,
12+
'h' : g,
13+
'h2' : h,
14+
'o' : IDL.Func([o], [o], []),
15+
});
16+
17+
export const idlInitArgs = [];
18+
19+
/**
20+
* @deprecated Import IDL types directly from this module instead of using this factory function.
21+
*/
122
export const idlFactory = ({ IDL }) => {
223
const o = IDL.Rec();
324
const f = IDL.Func([IDL.Int8], [IDL.Int8], []);
@@ -12,4 +33,8 @@ export const idlFactory = ({ IDL }) => {
1233
'o' : IDL.Func([o], [o], []),
1334
});
1435
};
36+
37+
/**
38+
* @deprecated Import IDL types directly from this module instead of using this factory function.
39+
*/
1540
export const init = ({ IDL }) => { return []; };

rust/candid_parser/tests/assets/ok/class.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
import { IDL } from '@dfinity/candid';
2+
3+
export const List = IDL.Rec();
4+
List.fill(IDL.Opt(IDL.Tuple(IDL.Int, List)));
5+
export const Profile = IDL.Record({ 'age' : IDL.Nat8, 'name' : IDL.Text });
6+
7+
export const idlService = IDL.Service({
8+
'get' : IDL.Func([], [List], []),
9+
'set' : IDL.Func([List], [List], []),
10+
});
11+
12+
export const idlInitArgs = [IDL.Int, List, Profile];
13+
14+
/**
15+
* @deprecated Import IDL types directly from this module instead of using this factory function.
16+
*/
117
export const idlFactory = ({ IDL }) => {
218
const List = IDL.Rec();
319
List.fill(IDL.Opt(IDL.Tuple(IDL.Int, List)));
@@ -7,6 +23,10 @@ export const idlFactory = ({ IDL }) => {
723
'set' : IDL.Func([List], [List], []),
824
});
925
};
26+
27+
/**
28+
* @deprecated Import IDL types directly from this module instead of using this factory function.
29+
*/
1030
export const init = ({ IDL }) => {
1131
const List = IDL.Rec();
1232
List.fill(IDL.Opt(IDL.Tuple(IDL.Int, List)));
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
const id = IDL.Nat8;
1+
import { IDL } from '@dfinity/candid';
2+
3+
export const id = IDL.Nat8;
24

rust/candid_parser/tests/assets/ok/cyclic.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
import { IDL } from '@dfinity/candid';
2+
3+
export const A = IDL.Rec();
4+
export const C = A;
5+
export const B = IDL.Opt(C);
6+
A.fill(IDL.Opt(B));
7+
export const Z = A;
8+
export const Y = Z;
9+
export const X = Y;
10+
11+
export const idlService = IDL.Service({
12+
'f' : IDL.Func([A, B, C, X, Y, Z], [], []),
13+
});
14+
15+
export const idlInitArgs = [];
16+
17+
/**
18+
* @deprecated Import IDL types directly from this module instead of using this factory function.
19+
*/
120
export const idlFactory = ({ IDL }) => {
221
const A = IDL.Rec();
322
const C = A;
@@ -8,4 +27,8 @@ export const idlFactory = ({ IDL }) => {
827
const X = Y;
928
return IDL.Service({ 'f' : IDL.Func([A, B, C, X, Y, Z], [], []) });
1029
};
30+
31+
/**
32+
* @deprecated Import IDL types directly from this module instead of using this factory function.
33+
*/
1134
export const init = ({ IDL }) => { return []; };

rust/candid_parser/tests/assets/ok/empty.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
import { IDL } from '@dfinity/candid';
2+
3+
export const T = IDL.Rec();
4+
T.fill(IDL.Tuple(T));
5+
6+
export const idlService = IDL.Service({
7+
'f' : IDL.Func([IDL.Record({})], [IDL.Variant({})], []),
8+
'g' : IDL.Func([T], [IDL.Variant({ 'a' : T })], []),
9+
'h' : IDL.Func(
10+
[IDL.Tuple(T, IDL.Empty)],
11+
[IDL.Variant({ 'a' : T, 'b' : IDL.Record({}) })],
12+
[],
13+
),
14+
});
15+
16+
export const idlInitArgs = [];
17+
18+
/**
19+
* @deprecated Import IDL types directly from this module instead of using this factory function.
20+
*/
121
export const idlFactory = ({ IDL }) => {
222
const T = IDL.Rec();
323
T.fill(IDL.Tuple(T));
@@ -11,4 +31,8 @@ export const idlFactory = ({ IDL }) => {
1131
),
1232
});
1333
};
34+
35+
/**
36+
* @deprecated Import IDL types directly from this module instead of using this factory function.
37+
*/
1438
export const init = ({ IDL }) => { return []; };

rust/candid_parser/tests/assets/ok/escape.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
import { IDL } from '@dfinity/candid';
2+
3+
export const t = IDL.Record({
4+
'\"' : IDL.Nat,
5+
'\'' : IDL.Nat,
6+
'\"\'' : IDL.Nat,
7+
'\\\n\'\"' : IDL.Nat,
8+
});
9+
10+
export const idlService = IDL.Service({
11+
'\n\'\"\'\'\"\"\r\t' : IDL.Func([t], [], []),
12+
});
13+
14+
export const idlInitArgs = [];
15+
16+
/**
17+
* @deprecated Import IDL types directly from this module instead of using this factory function.
18+
*/
119
export const idlFactory = ({ IDL }) => {
220
const t = IDL.Record({
321
'\"' : IDL.Nat,
@@ -7,4 +25,8 @@ export const idlFactory = ({ IDL }) => {
725
});
826
return IDL.Service({ '\n\'\"\'\'\"\"\r\t' : IDL.Func([t], [], []) });
927
};
28+
29+
/**
30+
* @deprecated Import IDL types directly from this module instead of using this factory function.
31+
*/
1032
export const init = ({ IDL }) => { return []; };

0 commit comments

Comments
 (0)