-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_rust_bindings.py
More file actions
51 lines (39 loc) · 1.18 KB
/
generate_rust_bindings.py
File metadata and controls
51 lines (39 loc) · 1.18 KB
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
import os
import shutil
import subprocess
def to_rust_file_name(schema: str):
name, ext = schema.split(".")
return f"{name}.rs"
def to_rust_mod_name(schema: str):
name, ext = schema.split(".")
return f"{name[5:]}_{name[0:4]}"
def generate_rust_bindings():
shutil.rmtree("rust/src/flatbuffers_generated/")
os.makedirs("rust/src/flatbuffers_generated/")
for schema in os.listdir("schemas"):
if not schema.endswith(".fbs"):
continue
subprocess.run(
[
"flatc",
"--rust",
"-o",
os.path.join("rust", "src", "flatbuffers_generated"),
"--filename-suffix",
"",
"--gen-all",
os.path.join("schemas", schema),
],
check=True,
)
with open("rust/src/flatbuffers_generated/mod.rs", "a") as f:
f.writelines(
[
f'#[path = "{to_rust_file_name(schema)}"]\n',
f"pub mod {to_rust_mod_name(schema)};\n",
]
)
def main():
generate_rust_bindings()
if __name__ == "__main__":
main()