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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use anyhow::{Context, Result};
use askama::Template;
use camino::Utf8PathBuf;
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::Write;
use uniffi_bindgen::ComponentInterface;
mod ci_list;
mod render;
use ci_list::{CallbackIds, ComponentUniverse, FunctionIds, ObjectIds};
use render::cpp::CPPScaffoldingTemplate;
use render::js::JSBindingsTemplate;
#[derive(Debug, Parser)]
#[clap(name = "uniffi-bindgen-gecko-js")]
#[clap(version = clap::crate_version!())]
#[clap(about = "JS bindings generator for Rust")]
#[clap(propagate_version = true)]
struct CliArgs {
// This is a really convoluted set of arguments, but we're only expecting to be called by
// `mach_commands.py`
#[clap(long, value_name = "FILE")]
js_dir: Utf8PathBuf,
#[clap(long, value_name = "FILE")]
fixture_js_dir: Utf8PathBuf,
#[clap(long, value_name = "FILE")]
cpp_path: Utf8PathBuf,
#[clap(long, value_name = "FILE")]
fixture_cpp_path: Utf8PathBuf,
}
/// Configuration for all components, read from `uniffi.toml`
type ConfigMap = HashMap<String, Config>;
/// Configuration for a single Component
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Config {
crate_name: String,
udl_file: String,
#[serde(default)]
fixture: bool,
#[serde(default)]
receiver_thread: ReceiverThreadConfig,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
struct ReceiverThreadConfig {
#[serde(default)]
default: Option<String>,
#[serde(default)]
main: HashSet<String>,
#[serde(default)]
worker: HashSet<String>,
}
fn render(out_path: Utf8PathBuf, template: impl Template) -> Result<()> {
println!("rendering {}", out_path);
let contents = template.render()?;
let mut f =
File::create(&out_path).context(format!("Failed to create {:?}", out_path.file_name()))?;
write!(f, "{}\n", contents).context(format!("Failed to write to {}", out_path))
}
fn render_cpp(
path: Utf8PathBuf,
prefix: &str,
components: &Vec<(ComponentInterface, Config)>,
function_ids: &FunctionIds,
object_ids: &ObjectIds,
callback_ids: &CallbackIds,
) -> Result<()> {
render(
path,
CPPScaffoldingTemplate {
prefix,
components,
function_ids,
object_ids,
callback_ids,
},
)
}
fn render_js(
out_dir: Utf8PathBuf,
components: &Vec<(ComponentInterface, Config)>,
function_ids: &FunctionIds,
object_ids: &ObjectIds,
callback_ids: &CallbackIds,
) -> Result<()> {
for (ci, config) in components {
let template = JSBindingsTemplate {
ci,
config,
function_ids,
object_ids,
callback_ids,
};
let path = out_dir.join(template.js_module_name());
render(path, template)?;
}
Ok(())
}
pub fn run_main() -> Result<()> {
let args = CliArgs::parse();
let config_map: ConfigMap =
toml::from_str(include_str!("../config.toml")).expect("Error parsing config.toml");
let components = ComponentUniverse::new(config_map)?;
let function_ids = FunctionIds::new(&components);
let object_ids = ObjectIds::new(&components);
let callback_ids = CallbackIds::new(&components);
render_cpp(
args.cpp_path,
"UniFFI",
&components.components,
&function_ids,
&object_ids,
&callback_ids,
)?;
render_cpp(
args.fixture_cpp_path,
"UniFFIFixtures",
&components.fixture_components,
&function_ids,
&object_ids,
&callback_ids,
)?;
render_js(
args.js_dir,
&components.components,
&function_ids,
&object_ids,
&callback_ids,
)?;
render_js(
args.fixture_js_dir,
&components.fixture_components,
&function_ids,
&object_ids,
&callback_ids,
)?;
Ok(())
}
|