Skip to content

Commit

Permalink
Auto-generate C# ModuleDef bindings from Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 6, 2024
1 parent df5ef6c commit 0b6c0f0
Show file tree
Hide file tree
Showing 65 changed files with 1,602 additions and 507 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/module_bindings/** linguist-generated=true eol=lf
*.verified.cs linguist-generated=true eol=lf
/crates/bindings-csharp/Runtime/Internal/Autogen/*.cs linguist-generated=true
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions crates/bindings-csharp/BSATN.Codegen/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ IEnumerable<MemberDeclaration> members
}

public static string GenerateDefs(IEnumerable<MemberDeclaration> members) =>
$$"""
new SpacetimeDB.BSATN.AggregateElement[] {
{{string.Join(
",\n",
members.Select(m => $"new(nameof({m.Name}), {m.Name}.GetAlgebraicType(registrar))")
)}}
}
""";
string.Join(
",\n",
members.Select(m => $"new(nameof({m.Name}), {m.Name}.GetAlgebraicType(registrar))")
);
}

public enum TypeKind
Expand Down Expand Up @@ -230,9 +226,9 @@ public void Write(System.IO.BinaryWriter writer, {{ShortName}} value) {
}
public SpacetimeDB.BSATN.AlgebraicType GetAlgebraicType(SpacetimeDB.BSATN.ITypeRegistrar registrar) =>
registrar.RegisterType<{{ShortName}}>(_ => new SpacetimeDB.BSATN.AlgebraicType.{{Kind}}(
registrar.RegisterType<{{ShortName}}>(_ => new SpacetimeDB.BSATN.AlgebraicType.{{Kind}}(new SpacetimeDB.BSATN.AggregateElement[] {
{{MemberDeclaration.GenerateDefs(Members)}}
));
}));
}
"""
);
Expand Down
5 changes: 3 additions & 2 deletions crates/bindings-csharp/Codegen.Tests/TestInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ public static void Initialize()
unformattedCode,
new() { IncludeGenerated = true, EndOfLine = CSharpier.EndOfLine.LF }
);
sb.Append(result.Code);
// Print errors in the end so that their line numbers are still meaningful.
if (result.CompilationErrors.Any())
{
sb.AppendLine();
sb.AppendLine("// Generated code produced compilation errors:");
foreach (var diag in result.CompilationErrors)
{
sb.Append("// ").AppendLine(diag.ToString());
}
sb.AppendLine();
}
sb.Append(result.Code);
},
ScrubberLocation.Last
);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0b6c0f0

Please sign in to comment.