Skip to content

Commit f6bc6dc

Browse files
authored
Switch Rust codegen to use new ModuleDef (#1675)
1 parent f047bb1 commit f6bc6dc

File tree

12 files changed

+583
-523
lines changed

12 files changed

+583
-523
lines changed

crates/bindings-csharp/Runtime/Internal/Module.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ private static string GetFriendlyName(Type type) =>
2626

2727
private void RegisterTypeName<T>(AlgebraicType.Ref typeRef)
2828
{
29-
// If it's a table, it doesn't need an alias as name will be registered automatically.
30-
if (typeof(T).IsDefined(typeof(TableAttribute), false))
31-
{
32-
return;
33-
}
3429
MiscExports.Add(
3530
new MiscModuleExport.TypeAlias(new(GetFriendlyName(typeof(T)), (uint)typeRef.Ref_))
3631
);

crates/bindings-macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ fn table_impl(mut args: TableArgs, mut item: MutItem<syn::DeriveInput>) -> syn::
887887

888888
let deserialize_impl = derive_deserialize(&sats_ty);
889889
let serialize_impl = derive_serialize(&sats_ty);
890-
let schema_impl = derive_satstype(&sats_ty, *original_struct_ident != table_name);
890+
let schema_impl = derive_satstype(&sats_ty, true);
891891
let column_attrs = columns.iter().map(|col| {
892892
Ident::new(
893893
ColumnAttribute::FLAGS

crates/cli/examples/regen-csharp-moduledef.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ fn main() -> anyhow::Result<()> {
2121
module.add_type::<RawModuleDef>();
2222
});
2323

24-
let mut results = generate(module, Language::Csharp, "SpacetimeDB.Internal")?;
24+
let mut results = generate(
25+
RawModuleDef::V8BackCompat(module),
26+
Language::Csharp,
27+
"SpacetimeDB.Internal",
28+
)?;
2529

2630
// Someday we might replace custom BSATN types with autogenerated ones as well,
2731
// but for now they're not very large and our copies are somewhat more optimised.

crates/cli/src/subcommands/call.rs

Lines changed: 135 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::common_args;
22
use crate::config::Config;
33
use crate::edit_distance::{edit_distance, find_best_match_for_name};
4-
use crate::generate::rust::{write_arglist_no_delimiters, write_type};
54
use crate::util;
65
use crate::util::{add_auth_header_opt, database_address, get_auth_header_only};
76
use anyhow::{bail, Context, Error};
@@ -191,10 +190,10 @@ fn reducer_signature(schema_json: Value, reducer_name: &str) -> Option<String> {
191190
fn ctx(typespace: &Typespace, r: AlgebraicTypeRef) -> String {
192191
let ty = &typespace[r];
193192
let mut ty_str = String::new();
194-
write_type(&|r| ctx(typespace, r), &mut ty_str, ty).unwrap();
193+
write_type::write_type(&|r| ctx(typespace, r), &mut ty_str, ty).unwrap();
195194
ty_str
196195
}
197-
write_arglist_no_delimiters(&|r| ctx(&typespace, r), &mut args, &params, None).unwrap();
196+
write_type::write_arglist_no_delimiters(&|r| ctx(&typespace, r), &mut args, &params, None).unwrap();
198197
let args = args.trim().trim_end_matches(',').replace('\n', " ");
199198

200199
// Print the full signature to `reducer_fmt`.
@@ -322,3 +321,136 @@ fn typespace(value: &serde_json::Value) -> Option<Typespace> {
322321
let types = value.as_object()?.get("typespace")?;
323322
deserialize_from(types).map(Typespace::new).ok()
324323
}
324+
325+
// this is an old version of code in generate::rust that got
326+
// refactored, but reducer_signature() was using it
327+
// TODO: port reducer_signature() to use AlgebraicTypeUse et al, somehow.
328+
mod write_type {
329+
use super::*;
330+
use convert_case::{Case, Casing};
331+
use spacetimedb_lib::sats::ArrayType;
332+
use spacetimedb_lib::ProductType;
333+
use std::fmt;
334+
use std::ops::Deref;
335+
336+
pub fn write_type<W: fmt::Write>(
337+
ctx: &impl Fn(AlgebraicTypeRef) -> String,
338+
out: &mut W,
339+
ty: &AlgebraicType,
340+
) -> fmt::Result {
341+
match ty {
342+
p if p.is_identity() => write!(out, "Identity")?,
343+
p if p.is_address() => write!(out, "Address")?,
344+
p if p.is_schedule_at() => write!(out, "ScheduleAt")?,
345+
AlgebraicType::Sum(sum_type) => {
346+
if let Some(inner_ty) = sum_type.as_option() {
347+
write!(out, "Option<")?;
348+
write_type(ctx, out, inner_ty)?;
349+
write!(out, ">")?;
350+
} else {
351+
write!(out, "enum ")?;
352+
print_comma_sep_braced(out, &sum_type.variants, |out: &mut W, elem: &_| {
353+
if let Some(name) = &elem.name {
354+
write!(out, "{name}: ")?;
355+
}
356+
write_type(ctx, out, &elem.algebraic_type)
357+
})?;
358+
}
359+
}
360+
AlgebraicType::Product(ProductType { elements }) => {
361+
print_comma_sep_braced(out, elements, |out: &mut W, elem: &ProductTypeElement| {
362+
if let Some(name) = &elem.name {
363+
write!(out, "{name}: ")?;
364+
}
365+
write_type(ctx, out, &elem.algebraic_type)
366+
})?;
367+
}
368+
AlgebraicType::Bool => write!(out, "bool")?,
369+
AlgebraicType::I8 => write!(out, "i8")?,
370+
AlgebraicType::U8 => write!(out, "u8")?,
371+
AlgebraicType::I16 => write!(out, "i16")?,
372+
AlgebraicType::U16 => write!(out, "u16")?,
373+
AlgebraicType::I32 => write!(out, "i32")?,
374+
AlgebraicType::U32 => write!(out, "u32")?,
375+
AlgebraicType::I64 => write!(out, "i64")?,
376+
AlgebraicType::U64 => write!(out, "u64")?,
377+
AlgebraicType::I128 => write!(out, "i128")?,
378+
AlgebraicType::U128 => write!(out, "u128")?,
379+
AlgebraicType::I256 => write!(out, "i256")?,
380+
AlgebraicType::U256 => write!(out, "u256")?,
381+
AlgebraicType::F32 => write!(out, "f32")?,
382+
AlgebraicType::F64 => write!(out, "f64")?,
383+
AlgebraicType::String => write!(out, "String")?,
384+
AlgebraicType::Array(ArrayType { elem_ty }) => {
385+
write!(out, "Vec<")?;
386+
write_type(ctx, out, elem_ty)?;
387+
write!(out, ">")?;
388+
}
389+
AlgebraicType::Map(ty) => {
390+
write!(out, "Map<")?;
391+
write_type(ctx, out, &ty.key_ty)?;
392+
write!(out, ", ")?;
393+
write_type(ctx, out, &ty.ty)?;
394+
write!(out, ">")?;
395+
}
396+
AlgebraicType::Ref(r) => {
397+
write!(out, "{}", ctx(*r))?;
398+
}
399+
}
400+
Ok(())
401+
}
402+
403+
fn print_comma_sep_braced<W: fmt::Write, T>(
404+
out: &mut W,
405+
elems: &[T],
406+
on: impl Fn(&mut W, &T) -> fmt::Result,
407+
) -> fmt::Result {
408+
write!(out, "{{")?;
409+
410+
let mut iter = elems.iter();
411+
412+
// First factor.
413+
if let Some(elem) = iter.next() {
414+
write!(out, " ")?;
415+
on(out, elem)?;
416+
}
417+
// Other factors.
418+
for elem in iter {
419+
write!(out, ", ")?;
420+
on(out, elem)?;
421+
}
422+
423+
if !elems.is_empty() {
424+
write!(out, " ")?;
425+
}
426+
427+
write!(out, "}}")?;
428+
429+
Ok(())
430+
}
431+
432+
pub fn write_arglist_no_delimiters(
433+
ctx: &impl Fn(AlgebraicTypeRef) -> String,
434+
out: &mut impl Write,
435+
elements: &[ProductTypeElement],
436+
437+
// Written before each line. Useful for `pub`.
438+
prefix: Option<&str>,
439+
) -> fmt::Result {
440+
for elt in elements {
441+
if let Some(prefix) = prefix {
442+
write!(out, "{prefix} ")?;
443+
}
444+
445+
let Some(name) = &elt.name else {
446+
panic!("Product type element has no name: {elt:?}");
447+
};
448+
let name = name.deref().to_case(Case::Snake);
449+
450+
write!(out, "{name}: ")?;
451+
write_type(ctx, out, &elt.algebraic_type)?;
452+
writeln!(out, ",")?;
453+
}
454+
Ok(())
455+
}
456+
}

0 commit comments

Comments
 (0)