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
//! Go/Java/Kotlin/C# identifier methods and reverse-DNS derivations.
use super::ResolvedCrateConfig;
use crate::core::config::derive::{derive_go_module_from_repo, derive_reverse_dns_package};
impl ResolvedCrateConfig {
/// Get the GitHub repository URL, returning an error when no source has it set.
///
/// Resolution order:
/// 1. `[e2e.registry] github_repo`
/// 2. `[package_metadata] repository`
/// 3. `[scaffold] repository`
pub fn try_github_repo(&self) -> Result<String, String> {
if let Some(e2e) = &self.e2e
&& let Some(url) = &e2e.registry.github_repo
{
return Ok(url.clone());
}
if let Some(url) = self.package_metadata.as_ref().and_then(|p| p.repository.as_ref()) {
return Ok(url.clone());
}
if let Some(url) = self.scaffold.as_ref().and_then(|s| s.repository.as_ref()) {
return Ok(url.clone());
}
Err(format!(
"no repository URL configured — set `[scaffold] repository = \"...\"` (or `[e2e.registry] github_repo`) for crate `{}`",
self.name
))
}
/// Get the GitHub repository URL with a vendor-neutral placeholder fallback.
pub fn github_repo(&self) -> String {
self.try_github_repo()
.unwrap_or_else(|_| format!("https://example.invalid/{}", self.name))
}
/// Get the Go module path, returning an error when neither `[go].module`
/// nor a derivable repository URL is configured.
pub(crate) fn try_go_module(&self) -> Result<String, String> {
if let Some(module) = self.go.as_ref().and_then(|g| g.module.as_ref()) {
return Ok(module.clone());
}
if let Ok(repo) = self.try_github_repo()
&& let Some(module) = derive_go_module_from_repo(&repo)
{
return Ok(module);
}
Err(format!(
"no Go module configured — set `[go] module = \"...\"` or `[scaffold] repository = \"https://<host>/<org>/...\"` for crate `{}`",
self.name
))
}
/// Get the Go module path with a vendor-neutral placeholder fallback.
pub fn go_module(&self) -> String {
self.try_go_module()
.unwrap_or_else(|_| format!("example.invalid/{}", self.name))
}
/// Get the Go package name (e.g. `"samplecrate"`).
///
/// Returns `[go] package_name` if set, otherwise derives it from the last segment of
/// [`Self::go_module`]. This is the single source every Go-targeting generator (the Go
/// backend's binding file, the error-type generator, the e2e/docs snippet generator) must
/// call for the package name, so they can never disagree about it.
pub fn go_package_name(&self) -> String {
self.go
.as_ref()
.and_then(|g| g.package_name.clone())
.unwrap_or_else(|| crate::codegen::naming::go_package_name_from_module(&self.go_module()))
}
/// Get the Java package name, returning an error when neither `[java].package`
/// nor a derivable repository URL is configured.
pub(crate) fn try_java_package(&self) -> Result<String, String> {
if let Some(pkg) = self.java.as_ref().and_then(|j| j.package.as_ref()) {
return Ok(pkg.clone());
}
if let Ok(repo) = self.try_github_repo()
&& let Some(pkg) = derive_reverse_dns_package(&repo)
{
return Ok(pkg);
}
Err(format!(
"no Java package configured — set `[java] package = \"...\"` or `[scaffold] repository = \"https://<host>/<org>/...\"` for crate `{}`",
self.name
))
}
/// Get the Java package name with a vendor-neutral placeholder fallback.
pub fn java_package(&self) -> String {
self.try_java_package()
.unwrap_or_else(|_| "unconfigured.alef".to_string())
}
/// Get the Java Maven groupId.
///
/// Prefers the explicit `[java] group_id` override when set; otherwise falls back to
/// the Java package name (most projects publish under `groupId == java package`).
pub fn java_group_id(&self) -> String {
if let Some(gid) = self.java.as_ref().and_then(|j| j.group_id.as_ref()) {
return gid.clone();
}
self.java_package()
}
/// Get the Java Maven artifactId.
///
/// Prefers the explicit `[java] artifact_id` override; otherwise falls back to the
/// crate name (`[[crates]] name`).
pub fn java_artifact_id(&self) -> String {
self.java
.as_ref()
.and_then(|j| j.artifact_id.as_ref())
.cloned()
.unwrap_or_else(|| self.name.clone())
}
/// Get the Kotlin package name, returning an error when neither
/// `[kotlin].package` nor a derivable repository URL is configured.
pub(crate) fn try_kotlin_package(&self) -> Result<String, String> {
if let Some(pkg) = self.kotlin.as_ref().and_then(|k| k.package.as_ref()) {
return Ok(pkg.clone());
}
if let Ok(repo) = self.try_github_repo()
&& let Some(pkg) = derive_reverse_dns_package(&repo)
{
return Ok(pkg);
}
Err(format!(
"no Kotlin package configured — set `[kotlin] package = \"...\"` or `[scaffold] repository = \"https://<host>/<org>/...\"` for crate `{}`",
self.name
))
}
/// Get the Kotlin package name with a vendor-neutral placeholder fallback.
pub fn kotlin_package(&self) -> String {
self.try_kotlin_package()
.unwrap_or_else(|_| "unconfigured.alef".to_string())
}
/// Get the C# namespace.
pub fn csharp_namespace(&self) -> String {
self.csharp
.as_ref()
.and_then(|c| c.namespace.as_ref())
.cloned()
.unwrap_or_else(|| {
use heck::ToPascalCase;
self.name.to_pascal_case()
})
}
/// Get the NuGet package ID.
///
/// Prefers the explicit `[csharp] package_id` override; otherwise falls back to
/// [`Self::csharp_namespace`] (most projects publish under `packageId ==
/// namespace`). The single source of truth for this coordinate, so
/// `validate_dotnet_coordinates` and the cross-crate NuGet collision check in
/// [`crate::core::config::new_config::NewAlefConfig::resolve`] can never disagree about it.
pub fn nuget_package_id(&self) -> String {
self.csharp
.as_ref()
.and_then(|config| config.package_id.clone())
.unwrap_or_else(|| self.csharp_namespace())
}
}
#[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 with_repo(name: &str, repo: &str) -> super::super::ResolvedCrateConfig {
resolved_one(&format!(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "{name}"
sources = ["src/lib.rs"]
[crates.scaffold]
repository = "{repo}"
"#
))
}
#[test]
fn go_module_derives_from_repo() {
let r = with_repo("my-lib", "https://github.com/foo/my-lib");
assert_eq!(r.go_module(), "github.com/foo/my-lib");
}
#[test]
fn go_module_explicit_wins_over_repo() {
let r = resolved_one(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
[crates.scaffold]
repository = "https://github.com/foo/my-lib"
[crates.go]
module = "custom.example.com/my-lib"
"#,
);
assert_eq!(r.go_module(), "custom.example.com/my-lib");
}
#[test]
fn github_repo_uses_package_metadata_repository() {
let r = resolved_one(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
[crates.package_metadata]
repository = "https://gitlab.example.invalid/acme/my-lib"
"#,
);
assert_eq!(
r.try_github_repo().as_deref(),
Ok("https://gitlab.example.invalid/acme/my-lib")
);
}
#[test]
fn java_package_derives_from_repo() {
let r = with_repo("my-lib", "https://github.com/foo-org/my-lib");
assert_eq!(r.java_package(), "com.github.foo_org");
}
#[test]
fn java_package_explicit_wins() {
let r = resolved_one(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
[crates.java]
package = "dev.sample_crate"
"#,
);
assert_eq!(r.java_package(), "dev.sample_crate");
}
#[test]
fn kotlin_package_falls_back_to_placeholder() {
let r = resolved_one(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
"#,
);
assert_eq!(r.kotlin_package(), "unconfigured.alef");
}
#[test]
fn csharp_namespace_derives_pascal_case() {
let r = resolved_one(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
"#,
);
assert_eq!(r.csharp_namespace(), "MyLib");
}
#[test]
fn java_group_id_equals_package() {
let r = with_repo("my-lib", "https://github.com/foo-org/my-lib");
assert_eq!(r.java_group_id(), r.java_package());
}
// -----------------------------------------------------------------------------------------
// Property coverage: no proptest/quickcheck dependency exists in this crate, so this drives a
// deliberately adversarial, table-driven input list -- rather than a randomized generator --
// through `csharp_namespace()`'s unset-override default (`self.name.to_pascal_case()`,
// exercised here via `heck::ToPascalCase` directly rather than round-tripping through
// `NewAlefConfig::resolve()`, so the property holds independent of whatever the crate-name
// path-safety gate in `new_config` currently allows or rejects as a `name` value -- this is
// deliberately not relying on that gate as the only line of defense, matching
// `validate_path_segment_field`'s own "does not rely on that incidental protection"
// reasoning). For every input, the backend's own `namespace.replace('.', "/")` step (see
// `src/backends/csharp/gen_bindings/mod.rs`, `service_api.rs`) must not turn the pascal-cased
// output into an absolute path or a `..` path component, and the raw output must never carry
// a path separator or NUL byte. Confirmed empirically against the real `heck` 0.5.0
// dependency (not just reasoned from its docs) before writing this assertion: every fragment
// below strips to only alphanumeric characters (heck's word-boundary algorithm treats `.`,
// `-`, `_`, `/`, `\`, NUL, and whitespace purely as separators, never part of the output), so
// this test is expected to pass on current code -- it is defense-in-depth coverage for a
// property already true, not a fix for a discovered break.
#[test]
fn csharp_namespace_default_is_never_path_hazardous_for_any_crate_name() {
use heck::ToPascalCase;
const ADVERSARIAL_NAMES: &[&str] = &[
"..",
".",
"...",
"....",
"a..b",
".leading",
"trailing.",
"..leading-trailing..",
"back\\slash",
"back\\\\slash",
"with\0null",
"unicode-\u{65e5}\u{672c}\u{8a9e}-\u{4f60}\u{597d}",
" spaces ",
"-",
"--",
"___",
"UPPER-CASE",
"MixedCase.Name",
"a.b.c.d.e",
"%2e%2e",
"..%2f..",
"a/b",
"////",
"...-...-",
"-.-.-",
"a-.-b",
"",
];
for name in ADVERSARIAL_NAMES {
let namespace = name.to_pascal_case();
assert!(
!namespace.contains('/') && !namespace.contains('\\') && !namespace.contains('\0'),
"csharp_namespace default must never carry a raw path separator or NUL: \
name={name:?} namespace={namespace:?}"
);
let transformed = namespace.replace('.', "/");
let transformed_path = std::path::Path::new(&transformed);
assert!(
!transformed_path.is_absolute(),
"csharp_namespace default must not become absolute after the backend's \
dot-to-slash transform: name={name:?} namespace={namespace:?} \
transformed={transformed:?}"
);
assert!(
!transformed_path
.components()
.any(|c| matches!(c, std::path::Component::ParentDir)),
"csharp_namespace default must not contain a `..` component after the backend's \
dot-to-slash transform: name={name:?} namespace={namespace:?} \
transformed={transformed:?}"
);
}
}
}