Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(csharp): name unit's real Rust namespaces and class alias in wrapper, README, and shadow advice (#1434)

m3lon952d0c685724
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c1cf5439..5d01aab4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Unreleased
+- C# wrapper headers, generated `README.md`, and `shadow` refusals now name the unit's actual `<namespace>.Rust` namespace(s) and first real class alias instead of `<namespace>.Rust` and `Acme.Text.Rust.Slug` (#1381 by @m3lonxd).
- `gate`'s `accepted diffs` check now prints a single `--accept-known-diffs <N>` command when both rules fail (naming the count that clears both), and updates `fix:`, `gate --help`, and MCP tool documentation to clarify entries vs. accepted cases (#1349 by @m3lonxd).
- `profile` and `go` on a C repository say they do not run C workloads yet and name Instruments' Time Profiler on macOS alongside `perf record -g` on Linux, matching other unsupported languages (#1398 by @m3lonxd).
- Python: `time.localtime()`, `time.gmtime()`, `time.strftime()`, `time.ctime()`,
diff --git a/src/bridge.rs b/src/bridge.rs
index 373ac5fc..87cde42b 100644
--- a/src/bridge.rs
+++ b/src/bridge.rs
@@ -2809,8 +2809,9 @@ fn readme(m: &Manifest) -> String {
),
Lang::Csharp => format!(
"\nC# has no shadow module: once `diff` passes, compile `{}` into the project and call \
- the `<namespace>.Rust` classes, with the built library next to the app.\n",
- crate::codegen_csharp::wrapper_path(m)
+ the {} classes, with the built library next to the app.\n",
+ crate::codegen_csharp::wrapper_path(m),
+ crate::codegen_csharp::rust_namespaces_text(m)
),
Lang::Swift => format!(
"\nSwift has no shadow module: once `diff` passes, compile `{}` into the target in place \
diff --git a/src/codegen_csharp.rs b/src/codegen_csharp.rs
index 3260ead4..3b3aee5d 100644
--- a/src/codegen_csharp.rs
+++ b/src/codegen_csharp.rs
@@ -38,6 +38,50 @@ pub fn rust_namespace(ns: &str) -> String {
}
}
+/// Unique `<original>.Rust` namespaces produced for a C# unit's exports, in source order.
+pub fn rust_namespaces(m: &Manifest) -> Vec<String> {
+ let mut out = Vec::new();
+ for f in &m.exports {
+ let rns = rust_namespace(&qualified(f).0);
+ if !out.contains(&rns) {
+ out.push(rns);
+ }
+ }
+ if out.is_empty() {
+ out.push("Rust".into());
+ }
+ out
+}
+
+/// Backticked list of wrapper namespaces for the wrapper header, README, and `shadow` refusal.
+pub fn rust_namespaces_text(m: &Manifest) -> String {
+ let namespaces: Vec<String> = rust_namespaces(m)
+ .into_iter()
+ .map(|ns| format!("`{ns}`"))
+ .collect();
+ match namespaces.len() {
+ 0 => "`Rust`".into(),
+ 1 => namespaces[0].clone(),
+ 2 => format!("{} and {}", namespaces[0], namespaces[1]),
+ n => format!(
+ "{} and {}",
+ namespaces[..n - 1].join(", "),
+ namespaces[n - 1]
+ ),
+ }
+}
+
+fn alias_example(m: &Manifest) -> Option<String> {
+ m.exports.iter().find_map(|f| {
+ let (ns, class, _) = qualified(f);
+ if class.is_empty() {
+ return None;
+ }
+ let rns = rust_namespace(&ns);
+ Some(format!("using {class} = {rns}.{class};"))
+ })
+}
+
/// `{ name: { original: "Ns.Class", rust: "Ns.Rust.Class", method } }` for the driver.
pub fn targets(m: &Manifest) -> serde_json::Value {
serde_json::Value::Object(
@@ -104,10 +148,15 @@ fn codec(class: &str, visibility: &str) -> String {
pub fn wrapper(m: &Manifest, api: &crate::codegen::PortApi) -> String {
let native = native_class(m);
let codec_name = codec_class(m);
+ let ns_text = rust_namespaces_text(m);
+ let alias_hint = match alias_example(m) {
+ Some(alias) => format!(" (or alias them: `{alias}`)"),
+ None => String::new(),
+ };
let mut out = format!(
"// The Rust port of `{unit}`, generated by rust-anything: the same classes and methods under\n\
- // `<namespace>.Rust`, each calling the Rust library through P/Invoke. Call them in place of\n\
- // the originals (or alias them: `using Slug = Acme.Text.Rust.Slug;`).\n\
+ // {ns_text}, each calling the Rust library through P/Invoke. Call them in place of\n\
+ // the originals{alias_hint}.\n\
// Regenerate with `rust-anything refresh`.\n\n\
#nullable enable\n\n",
unit = m.unit,
@@ -1181,4 +1230,70 @@ mod tests {
"{cs}"
);
}
+
+ #[test]
+ fn wrapper_header_and_namespaces_use_real_namespaces_instead_of_placeholders() {
+ let exp = |name: &str, proto: &str| ExportedFn {
+ name: name.into(),
+ prototype: Some(proto.into()),
+ ..Default::default()
+ };
+ let manifest_with = |exports: Vec<ExportedFn>| Manifest {
+ schema: String::new(),
+ root: std::path::PathBuf::new(),
+ root_written: None,
+ unit: "src/Text/".into(),
+ language: crate::lang::Lang::Csharp,
+ kind: crate::bridge::Kind::Ffi,
+ crate_name: "text-rs".into(),
+ exports,
+ classes: Vec::new(),
+ js_only: Vec::new(),
+ batches: Vec::new(),
+ allocator: Default::default(),
+ float_tolerance: None,
+ ignore_key_order: false,
+ messages: false,
+ end_state: None,
+ };
+
+ let single = manifest_with(vec![exp(
+ "Make",
+ "Acme.Textkit.Text.Slug.Make(string text)",
+ )]);
+ assert_eq!(rust_namespaces(&single), ["Acme.Textkit.Text.Rust"]);
+ assert_eq!(rust_namespaces_text(&single), "`Acme.Textkit.Text.Rust`");
+ let cs = wrapper(&single, &crate::codegen::PortApi::default());
+ let header: String = cs.lines().take(4).collect::<Vec<_>>().join("\n");
+ assert!(header.contains("`Acme.Textkit.Text.Rust`"), "{header}");
+ assert!(
+ header.contains("using Slug = Acme.Textkit.Text.Rust.Slug;"),
+ "{header}"
+ );
+ assert!(!header.contains("<namespace>"), "{header}");
+ assert!(!header.contains("Acme.Text.Rust"), "{header}");
+
+ let multi = manifest_with(vec![
+ exp("One", "Alpha.Core.Parser.One()"),
+ exp("Two", "Alpha.Core.Lexer.Two()"),
+ exp("Three", "Beta.Util.Helper.Three()"),
+ ]);
+ assert_eq!(
+ rust_namespaces(&multi),
+ ["Alpha.Core.Rust", "Beta.Util.Rust"]
+ );
+ assert_eq!(
+ rust_namespaces_text(&multi),
+ "`Alpha.Core.Rust` and `Beta.Util.Rust`"
+ );
+
+ let global = manifest_with(vec![exp("Run", "GlobalTool.Run()")]);
+ assert_eq!(rust_namespaces(&global), ["Rust"]);
+ let global_cs = wrapper(&global, &crate::codegen::PortApi::default());
+ let g_hdr: String = global_cs.lines().take(4).collect::<Vec<_>>().join("\n");
+ assert!(
+ g_hdr.contains("using GlobalTool = Rust.GlobalTool;"),
+ "{g_hdr}"
+ );
+ }
}
diff --git a/src/shadow.rs b/src/shadow.rs
index d9cb193a..81e9b2df 100644
--- a/src/shadow.rs
+++ b/src/shadow.rs
@@ -41,11 +41,12 @@ pub(crate) fn manual_switch_advice(crate_dir: &Path, manifest: &Manifest) -> Opt
),
Lang::Csharp => format!(
"C# binds calls when it compiles, so there is no shadow module for C# yet; once `diff` \
- passes, compile {} into the project and call the `<namespace>.Rust` classes (or alias \
+ passes, compile {} into the project and call the {} classes (or alias \
them with `using`), with the built library next to the app",
crate_dir
.join(crate::codegen_csharp::wrapper_path(manifest))
- .display()
+ .display(),
+ crate::codegen_csharp::rust_namespaces_text(manifest)
),
_ => return None,
};
diff --git a/tests/loop.rs b/tests/loop.rs
index 57b33c26..02cb220c 100644
--- a/tests/loop.rs
+++ b/tests/loop.rs
@@ -4243,11 +4243,25 @@ fn csharp_static_methods_bridge_record_and_diff() {
] {
assert!(wrapper.contains(expected), "{expected}\n{wrapper}");
}
+ let header = wrapper.lines().take(4).collect::<Vec<_>>().join("\n");
+ assert!(header.contains("Acme.Geo.Rust"), "{header}");
+ assert!(
+ header.contains("using Geometry = Acme.Geo.Rust.Geometry;"),
+ "{header}"
+ );
+ assert!(!wrapper.contains("<namespace>"), "{wrapper}");
+ assert!(!wrapper.contains("Acme.Text.Rust"), "{wrapper}");
+ let readme = std::fs::read_to_string(dir.join("README.md")).unwrap();
+ assert!(readme.contains("Acme.Geo.Rust"), "{readme}");
+ assert!(!readme.contains("<namespace>"), "{readme}");
+ assert!(!readme.contains("Acme.Text.Rust"), "{readme}");
let refused = shadow::shadow(&dir, None).unwrap_err().to_string();
assert!(
refused.contains("C# binds calls when it compiles"),
"{refused}"
);
+ assert!(refused.contains("Acme.Geo.Rust"), "{refused}");
+ assert!(!refused.contains("<namespace>"), "{refused}");
if !common::have("dotnet") {
return;
}