-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserde-derive.ts
67 lines (64 loc) · 1.35 KB
/
serde-derive.ts
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
import { Context } from "../emit/types.ts";
import { Type } from "../types.ts";
import { Plugin } from "./types.ts";
export const plugin: Plugin = {
hooks: {
preEmitHook(
context,
name,
type,
preferInline,
code,
) {
if (context.lang !== "rust") {
return code;
}
switch (type.kind) {
case "struct":
return preEmitStructType(
context,
name,
type,
preferInline,
code,
);
case "union":
return preEmitUnionType(
context,
name,
type,
preferInline,
code,
);
case "map":
case "option":
case "array":
case "primitive":
case "null":
case "unknown":
return code;
}
},
},
};
export function preEmitStructType(
context: Context,
_name: string,
_type: Type,
_preferInline: boolean,
code: string,
) {
context.emitAnnotation("#[derive(serde::Serialize, serde::Deserialize)]");
return code;
}
export function preEmitUnionType(
context: Context,
_name: string,
_type: Type,
_preferInline: boolean,
code: string,
) {
context.emitAnnotation("#[derive(serde::Serialize, serde::Deserialize)]");
context.emitAnnotation("#[serde(untagged)]");
return code;
}