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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! Language-specific naming methods for `ResolvedCrateConfig`.
use super::ResolvedCrateConfig;
impl ResolvedCrateConfig {
/// Get the Python module name.
pub fn python_module_name(&self) -> String {
self.python
.as_ref()
.and_then(|p| p.module_name.as_ref())
.cloned()
.unwrap_or_else(|| format!("_{}", self.name.replace('-', "_")))
}
/// Get the Node package name.
pub fn node_package_name(&self) -> String {
self.node
.as_ref()
.and_then(|n| n.package_name.as_ref())
.cloned()
.unwrap_or_else(|| self.name.clone())
}
/// Get the WASM npm package name.
pub fn wasm_package_name(&self) -> String {
self.wasm
.as_ref()
.and_then(|w| w.package_name.as_ref())
.cloned()
.unwrap_or_else(|| {
let node_pkg = self.node_package_name();
let base = node_pkg.strip_suffix("-node").unwrap_or(node_pkg.as_str());
format!("{base}-wasm")
})
}
/// Get the wasm-pack targets to build and publish for the WASM package.
/// Defaults to every wasm-pack target when unset.
pub fn wasm_targets(&self) -> Vec<String> {
self.wasm
.as_ref()
.map(|w| w.targets.clone())
.unwrap_or_else(crate::core::config::languages::default_wasm_targets)
}
/// Get the Ruby gem name.
pub fn ruby_gem_name(&self) -> String {
self.ruby
.as_ref()
.and_then(|r| r.gem_name.as_ref())
.cloned()
.unwrap_or_else(|| self.name.replace('-', "_"))
}
/// Get the Ruby native extension directory name (the `{name}` in `ext/{name}/native`).
///
/// ~keep Deliberately NOT [`Self::ruby_gem_name`]: the extension directory is always
/// `{core_crate_dir}_rb`, derived from the core crate's directory name regardless of any
/// configured `gem_name` override (see `scaffold::languages::ruby`, the single source of
/// truth this mirrors). `gem_name` names the published gem / Ruby module, a separate
/// namespace that can legitimately diverge from the crate directory name (e.g. a gem
/// renamed for RubyGems without renaming the crate). Before this helper existed,
/// `cli::pipeline::format`'s `cargo sort` residual step for Ruby called `ruby_gem_name()`
/// directly, so a configured `gem_name` silently pointed the residual at a directory the
/// scaffold never created -- reproduced for real in a consumer with `cargo sort -n
/// ext/<gem_name>/native` against a tree that only had `ext/<core_crate_dir>_rb/native`.
pub fn ruby_native_ext_name(&self) -> String {
format!("{}_rb", self.core_crate_dir().replace('-', "_"))
}
/// Get the PHP extension name.
pub fn php_extension_name(&self) -> String {
self.php
.as_ref()
.and_then(|p| p.extension_name.as_ref())
.cloned()
.unwrap_or_else(|| self.name.replace('-', "_"))
}
/// Get the PHP binding Cargo crate name (used for deriving the shared library filename).
pub fn php_cargo_crate_name(&self) -> Option<&str> {
self.php.as_ref().and_then(|p| p.cargo_crate_name.as_deref())
}
/// Get the Elixir app name.
pub fn elixir_app_name(&self) -> String {
self.elixir
.as_ref()
.and_then(|e| e.app_name.as_ref())
.cloned()
.unwrap_or_else(|| self.name.replace('-', "_"))
}
/// Get the Zig module name.
pub fn zig_module_name(&self) -> String {
self.zig
.as_ref()
.and_then(|z| z.module_name.as_ref())
.cloned()
.unwrap_or_else(|| self.name.replace('-', "_"))
}
/// Get the Dart pubspec package name.
///
/// Returns `[dart] pubspec_name` if set, otherwise derives a snake_case
/// name from the crate name by replacing hyphens with underscores.
pub fn dart_pubspec_name(&self) -> String {
self.dart
.as_ref()
.and_then(|d| d.pubspec_name.as_ref())
.cloned()
.unwrap_or_else(|| self.name.replace('-', "_"))
}
/// Get the Dart public library entrypoint name.
pub fn dart_library_name(&self) -> String {
self.dart
.as_ref()
.and_then(|d| d.lib_name.as_ref())
.cloned()
.unwrap_or_else(|| self.dart_pubspec_name())
}
/// Get the Dart FRB bridge class name.
///
/// Converts `[dart] lib_name` (falling back to `dart_pubspec_name()`) to
/// PascalCase and appends `"Bridge"`, matching the FRB v2 convention.
/// E.g. `lib_name = "sample-widget"` → `"SampleWidgetBridge"`.
pub fn dart_bridge_class_name(&self) -> String {
use heck::ToUpperCamelCase;
let lib_name = self
.dart
.as_ref()
.and_then(|d| d.lib_name.as_ref())
.cloned()
.unwrap_or_else(|| self.dart_pubspec_name());
format!("{}Bridge", lib_name.to_upper_camel_case())
}
/// Get the Swift module name.
///
/// Returns `[swift] module_name` if configured, otherwise derives a PascalCase
/// name from the crate name (e.g. `"my-lib"` → `"MyLib"`).
pub fn swift_module(&self) -> String {
self.swift
.as_ref()
.and_then(|s| s.module_name.as_ref())
.cloned()
.unwrap_or_else(|| {
use heck::ToUpperCamelCase;
self.name.to_upper_camel_case()
})
}
/// Get the SwiftPM package name (`Package(name: "...")`).
///
/// Defaults to [`Self::swift_module`]. This is a different coordinate from the module name
/// and carries a different grammar: real published packages use kebab-case
/// (`swift-argument-parser`), which is not a Swift identifier.
pub fn swift_package_name(&self) -> String {
self.swift
.as_ref()
.and_then(|s| s.package_name.as_ref())
.cloned()
.unwrap_or_else(|| self.swift_module())
}
/// Get the R package name.
pub fn r_package_name(&self) -> String {
self.r
.as_ref()
.and_then(|r| r.package_name.as_ref())
.cloned()
.unwrap_or_else(|| self.name.clone())
}
/// Get the WASM type name prefix (e.g. "Wasm" produces `WasmConversionOptions`).
/// Defaults to `"Wasm"`.
pub fn wasm_type_prefix(&self) -> String {
self.wasm
.as_ref()
.and_then(|w| w.type_prefix.as_ref())
.cloned()
.unwrap_or_else(|| "Wasm".to_string())
}
/// Get the Node/NAPI type name prefix (e.g. "Js" produces `JsConversionOptions`).
/// Defaults to `"Js"`.
pub fn node_type_prefix(&self) -> String {
self.node
.as_ref()
.and_then(|n| n.type_prefix.as_ref())
.cloned()
.unwrap_or_else(|| "Js".to_string())
}
}
#[cfg(test)]
mod tests {
use crate::core::config::new_config::NewAlefConfig;
fn resolved_one(toml: &str) -> super::super::ResolvedCrateConfig {
let cfg: NewAlefConfig = toml::from_str(toml).unwrap();
cfg.resolve().unwrap().remove(0)
}
fn minimal() -> super::super::ResolvedCrateConfig {
resolved_one(
r#"
[workspace]
languages = ["python", "node"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
"#,
)
}
#[test]
fn python_module_name_defaults_to_underscore_prefix() {
let r = minimal();
assert_eq!(r.python_module_name(), "_test_lib");
}
#[test]
fn python_module_name_explicit_override() {
let r = resolved_one(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
[crates.python]
module_name = "mymod"
"#,
);
assert_eq!(r.python_module_name(), "mymod");
}
#[test]
fn node_package_name_defaults_to_crate_name() {
let r = minimal();
assert_eq!(r.node_package_name(), "test-lib");
}
#[test]
fn wasm_package_name_defaults_from_node_package_name() {
let r = resolved_one(
r#"
[workspace]
languages = ["node", "wasm"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
[crates.node]
package_name = "@scope/test-lib-node"
"#,
);
assert_eq!(r.wasm_package_name(), "@scope/test-lib-wasm");
}
#[test]
fn wasm_package_name_uses_explicit_override() {
let r = resolved_one(
r#"
[workspace]
languages = ["wasm"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
[crates.wasm]
package_name = "@scope/explicit-wasm"
"#,
);
assert_eq!(r.wasm_package_name(), "@scope/explicit-wasm");
}
#[test]
fn ruby_gem_name_replaces_hyphens() {
let r = minimal();
assert_eq!(r.ruby_gem_name(), "test_lib");
}
#[test]
fn ruby_native_ext_name_appends_rb_suffix() {
let r = minimal();
assert_eq!(r.ruby_native_ext_name(), "test_lib_rb");
}
/// The bug this method exists to fix: a configured `gem_name` must not change the native
/// extension directory name, since the scaffold that actually creates it never reads
/// `gem_name` either.
#[test]
fn ruby_native_ext_name_ignores_a_configured_gem_name() {
let r = resolved_one(
r#"
[workspace]
languages = ["ruby"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
[crates.ruby]
gem_name = "totally_different_gem"
"#,
);
assert_eq!(r.ruby_gem_name(), "totally_different_gem");
assert_eq!(r.ruby_native_ext_name(), "test_lib_rb");
}
#[test]
fn php_extension_name_replaces_hyphens() {
let r = minimal();
assert_eq!(r.php_extension_name(), "test_lib");
}
#[test]
fn elixir_app_name_replaces_hyphens() {
let r = minimal();
assert_eq!(r.elixir_app_name(), "test_lib");
}
#[test]
fn zig_module_name_replaces_hyphens() {
let r = minimal();
assert_eq!(r.zig_module_name(), "test_lib");
}
#[test]
fn dart_pubspec_name_replaces_hyphens() {
let r = minimal();
assert_eq!(r.dart_pubspec_name(), "test_lib");
}
#[test]
fn dart_library_name_resolves_entrypoint() {
for (config, expected) in [
(minimal(), "test_lib"),
(
resolved_one(
r#"
[workspace]
languages = ["dart"]
[[crates]]
name = "test-lib"
sources = ["src/lib.rs"]
[crates.dart]
pubspec_name = "sample_package"
lib_name = "sample_entrypoint"
"#,
),
"sample_entrypoint",
),
] {
assert_eq!(config.dart_library_name(), expected);
}
}
#[test]
fn swift_module_is_pascal_case() {
let r = minimal();
assert_eq!(r.swift_module(), "TestLib");
}
#[test]
fn r_package_name_defaults_to_crate_name() {
let r = minimal();
assert_eq!(r.r_package_name(), "test-lib");
}
#[test]
fn wasm_type_prefix_defaults_to_wasm() {
let r = minimal();
assert_eq!(r.wasm_type_prefix(), "Wasm");
}
#[test]
fn node_type_prefix_defaults_to_js() {
let r = minimal();
assert_eq!(r.node_type_prefix(), "Js");
}
}