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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use anyhow::{anyhow, Result};
use convert_case::{Case, Casing};
use serde::Deserialize;
use structopt::clap::AppSettings;
use structopt::StructOpt;
use toml::Value;
#[derive(StructOpt, Debug, PartialEq, Deserialize, Default)]
#[structopt(setting(AppSettings::DeriveDisplayOrder))]
pub struct RawOpts {
#[structopt(short, long)]
pub rust_input: String,
#[structopt(short, long)]
pub dart_output: String,
#[structopt(long)]
pub dart_decl_output: Option<String>,
#[structopt(short, long)]
pub c_output: Option<Vec<String>>,
#[structopt(long)]
pub rust_crate_dir: Option<String>,
#[structopt(long)]
pub rust_output: Option<String>,
#[structopt(long)]
pub class_name: Option<String>,
#[structopt(long)]
pub dart_format_line_length: Option<i32>,
#[structopt(long)]
pub skip_add_mod_to_lib: bool,
#[structopt(long)]
pub llvm_path: Option<Vec<String>>,
#[structopt(long)]
pub llvm_compiler_opts: Option<String>,
#[structopt(long)]
pub dart_root: Option<String>,
#[structopt(long)]
pub no_build_runner: bool,
#[structopt(short, long)]
pub verbose: bool,
}
#[derive(Debug)]
pub struct Opts {
pub rust_input_path: String,
pub dart_output_path: String,
pub dart_decl_output_path: Option<String>,
pub c_output_path: Vec<String>,
pub rust_crate_dir: String,
pub rust_output_path: String,
pub class_name: String,
pub dart_format_line_length: i32,
pub skip_add_mod_to_lib: bool,
pub llvm_path: Vec<String>,
pub llvm_compiler_opts: String,
pub manifest_path: String,
pub dart_root: Option<String>,
pub build_runner: bool,
}
pub fn parse(raw: RawOpts) -> Opts {
let rust_input_path = canon_path(&raw.rust_input);
let rust_crate_dir = canon_path(&raw.rust_crate_dir.unwrap_or_else(|| {
fallback_rust_crate_dir(&rust_input_path)
.unwrap_or_else(|_| panic!("{}", format_fail_to_guess_error("rust_crate_dir")))
}));
let manifest_path = {
let mut path = std::path::PathBuf::from_str(&rust_crate_dir).unwrap();
path.push("Cargo.toml");
path_to_string(path).unwrap()
};
let rust_output_path = canon_path(&raw.rust_output.unwrap_or_else(|| {
fallback_rust_output_path(&rust_input_path)
.unwrap_or_else(|_| panic!("{}", format_fail_to_guess_error("rust_output")))
}));
let class_name = raw.class_name.unwrap_or_else(|| {
fallback_class_name(&*rust_crate_dir)
.unwrap_or_else(|_| panic!("{}", format_fail_to_guess_error("class_name")))
});
let c_output_path = raw
.c_output
.map(|outputs| {
outputs
.iter()
.map(|output| canon_path(output))
.collect::<Vec<_>>()
})
.unwrap_or_else(|| {
vec![fallback_c_output_path()
.unwrap_or_else(|_| panic!("{}", format_fail_to_guess_error("c_output")))]
});
let dart_root = {
let dart_output = &raw.dart_output;
raw.dart_root
.as_deref()
.map(canon_path)
.or_else(|| fallback_dart_root(dart_output).ok())
};
Opts {
rust_input_path,
dart_output_path: canon_path(&raw.dart_output),
dart_decl_output_path: raw
.dart_decl_output
.as_ref()
.map(|s| canon_path(s.as_str())),
c_output_path,
rust_crate_dir,
rust_output_path,
class_name,
dart_format_line_length: raw.dart_format_line_length.unwrap_or(80),
skip_add_mod_to_lib: raw.skip_add_mod_to_lib,
llvm_path: raw.llvm_path.unwrap_or_else(|| {
vec![
"/opt/homebrew/opt/llvm".to_owned(),
"/usr/local/opt/llvm".to_owned(),
"/usr/lib/llvm-9".to_owned(),
"/usr/lib/llvm-10".to_owned(),
"/usr/lib/llvm-11".to_owned(),
"/usr/lib/llvm-12".to_owned(),
"/usr/lib/llvm-13".to_owned(),
"/usr/lib/llvm-14".to_owned(),
"/usr/lib/".to_owned(),
"/usr/lib64/".to_owned(),
"C:/Program Files/llvm".to_owned(),
"C:/Program Files/LLVM".to_owned(),
"C:/msys64/mingw64".to_owned(),
]
}),
llvm_compiler_opts: raw.llvm_compiler_opts.unwrap_or_else(|| "".to_string()),
manifest_path,
dart_root,
build_runner: !raw.no_build_runner,
}
}
fn format_fail_to_guess_error(name: &str) -> String {
format!(
"fail to guess {}, please specify it manually in command line arguments",
name
)
}
fn fallback_rust_crate_dir(rust_input_path: &str) -> Result<String> {
let mut dir_curr = Path::new(rust_input_path)
.parent()
.ok_or_else(|| anyhow!(""))?;
loop {
let path_cargo_toml = dir_curr.join("Cargo.toml");
if path_cargo_toml.exists() {
return Ok(dir_curr
.as_os_str()
.to_str()
.ok_or_else(|| anyhow!(""))?
.to_string());
}
if let Some(next_parent) = dir_curr.parent() {
dir_curr = next_parent;
} else {
break;
}
}
Err(anyhow!(
"look at parent directories but none contains Cargo.toml"
))
}
fn fallback_c_output_path() -> Result<String> {
let named_temp_file = Box::leak(Box::new(tempfile::Builder::new().suffix(".h").tempfile()?));
Ok(named_temp_file
.path()
.to_str()
.ok_or_else(|| anyhow!(""))?
.to_string())
}
fn fallback_rust_output_path(rust_input_path: &str) -> Result<String> {
Ok(Path::new(rust_input_path)
.parent()
.ok_or_else(|| anyhow!(""))?
.join("bridge_generated.rs")
.to_str()
.ok_or_else(|| anyhow!(""))?
.to_string())
}
fn fallback_dart_root(dart_output_path: &str) -> Result<String> {
let mut res = canon_pathbuf(dart_output_path);
while res.pop() {
if res.join("pubspec.yaml").is_file() {
return res
.to_str()
.map(ToString::to_string)
.ok_or_else(|| anyhow!("Non-utf8 path"));
}
}
Err(anyhow!(
"Root of Dart library could not be inferred from Dart output"
))
}
fn fallback_class_name(rust_crate_dir: &str) -> Result<String> {
let cargo_toml_path = Path::new(rust_crate_dir).join("Cargo.toml");
let cargo_toml_content = fs::read_to_string(cargo_toml_path)?;
let cargo_toml_value = cargo_toml_content.parse::<Value>()?;
let package_name = cargo_toml_value
.get("package")
.ok_or_else(|| anyhow!("no `package` in Cargo.toml"))?
.get("name")
.ok_or_else(|| anyhow!("no `name` in Cargo.toml"))?
.as_str()
.ok_or_else(|| anyhow!(""))?;
Ok(package_name.to_case(Case::Pascal))
}
fn canon_path(sub_path: &str) -> String {
let path = canon_pathbuf(sub_path);
path_to_string(path).unwrap_or_else(|_| panic!("fail to parse path: {}", sub_path))
}
fn canon_pathbuf(sub_path: &str) -> PathBuf {
let mut path =
env::current_dir().unwrap_or_else(|_| panic!("fail to parse path: {}", sub_path));
path.push(sub_path);
path
}
fn path_to_string(path: PathBuf) -> Result<String, OsString> {
path.into_os_string().into_string()
}
impl Opts {
pub fn dart_api_class_name(&self) -> String {
self.class_name.clone()
}
pub fn dart_api_impl_class_name(&self) -> String {
format!("{}Impl", self.class_name)
}
pub fn dart_wire_class_name(&self) -> String {
format!("{}Wire", self.class_name)
}
pub fn dart_output_path_name(&self) -> Option<&str> {
let name = Path::new(&self.dart_output_path);
let root = name.file_name()?.to_str()?;
if let Some((name, _)) = root.rsplit_once('.') {
Some(name)
} else {
Some(root)
}
}
pub fn dart_output_freezed_path(&self) -> Option<String> {
Some(
Path::new(&self.dart_output_path)
.with_extension("freezed.dart")
.to_str()?
.to_owned(),
)
}
}