11use super :: util:: fmt_fn;
22
33use std:: fmt:: { self , Write } ;
4+ use std:: ops:: Deref ;
45
56use convert_case:: { Case , Casing } ;
67use spacetimedb_lib:: sats:: db:: def:: TableSchema ;
@@ -110,7 +111,7 @@ fn ty_fmt<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace: &'a str) -> imp
110111 let name = csharp_typename ( ctx, * r) ;
111112 match & ctx. typespace . types [ r. idx ( ) ] {
112113 AlgebraicType :: Sum ( sum_type) => {
113- if is_enum ( sum_type) {
114+ if sum_type. is_simple_enum ( ) {
114115 let parts: Vec < & str > = name. split ( '.' ) . collect ( ) ;
115116 if parts. len ( ) >= 2 {
116117 let enum_namespace = parts[ 0 ] ;
@@ -253,7 +254,7 @@ fn convert_type<'a>(
253254 let algebraic_type = & ctx. typespace . types [ r. idx ( ) ] ;
254255 match algebraic_type {
255256 AlgebraicType :: Sum ( sum) => {
256- if is_enum ( sum) {
257+ if sum. is_simple_enum ( ) {
257258 let split: Vec < & str > = name. split ( '.' ) . collect ( ) ;
258259 if split. len ( ) >= 2 {
259260 assert_eq ! (
@@ -317,7 +318,7 @@ fn convert_algebraic_type<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace:
317318 let name = csharp_typename ( ctx, * r) ;
318319 match & ctx. typespace . types [ r. idx ( ) ] {
319320 AlgebraicType :: Sum ( sum_type) => {
320- if is_enum ( sum_type) {
321+ if sum_type. is_simple_enum ( ) {
321322 let parts: Vec < & str > = name. split ( '.' ) . collect ( ) ;
322323 if parts. len ( ) >= 2 {
323324 let enum_namespace = parts[ 0 ] ;
@@ -349,7 +350,7 @@ fn convert_product_type<'a>(
349350 "SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]"
350351 ) ?;
351352 writeln ! ( f, "{{" ) ?;
352- for elem in & product_type. elements {
353+ for elem in & * product_type. elements {
353354 writeln ! (
354355 f,
355356 "{INDENT}new SpacetimeDB.SATS.ProductTypeElement({}, {})," ,
@@ -371,7 +372,7 @@ fn convert_sum_type<'a>(ctx: &'a GenCtx, sum_type: &'a SumType, namespace: &'a s
371372 "SpacetimeDB.SATS.AlgebraicType.CreateSumType(new System.Collections.Generic.List<SpacetimeDB.SATS.SumTypeVariant>"
372373 ) ?;
373374 writeln ! ( f, "{{" ) ?;
374- for elem in & sum_type. variants {
375+ for elem in & * sum_type. variants {
375376 writeln ! (
376377 f,
377378 "\t new SpacetimeDB.SATS.SumTypeVariant({}, {})," ,
@@ -386,23 +387,8 @@ fn convert_sum_type<'a>(ctx: &'a GenCtx, sum_type: &'a SumType, namespace: &'a s
386387 } )
387388}
388389
389- pub fn is_enum ( sum_type : & SumType ) -> bool {
390- for variant in sum_type. clone ( ) . variants {
391- match variant. algebraic_type {
392- AlgebraicType :: Product ( product) => {
393- if product. elements . is_empty ( ) {
394- continue ;
395- }
396- }
397- _ => return false ,
398- }
399- }
400-
401- true
402- }
403-
404390pub fn autogen_csharp_sum ( ctx : & GenCtx , name : & str , sum_type : & SumType , namespace : & str ) -> String {
405- if is_enum ( sum_type) {
391+ if sum_type. is_simple_enum ( ) {
406392 autogen_csharp_enum ( ctx, name, sum_type, namespace)
407393 } else {
408394 unimplemented ! ( ) ;
@@ -467,7 +453,7 @@ pub fn autogen_csharp_enum(ctx: &GenCtx, name: &str, sum_type: &SumType, namespa
467453 writeln ! ( output, "{{" ) . unwrap ( ) ;
468454 {
469455 indent_scope ! ( output) ;
470- for variant in & sum_type. variants {
456+ for variant in & * sum_type. variants {
471457 let variant_name = variant
472458 . name
473459 . as_ref ( )
@@ -500,7 +486,7 @@ pub fn autogen_csharp_enum(ctx: &GenCtx, name: &str, sum_type: &SumType, namespa
500486 . unwrap ( ) ;
501487 }
502488 None => {
503- for variant in & sum_type. variants {
489+ for variant in & * sum_type. variants {
504490 let variant_name = variant
505491 . name
506492 . as_ref ( )
@@ -614,7 +600,7 @@ fn autogen_csharp_product_table_common(
614600 {
615601 indent_scope ! ( output) ;
616602
617- for field in & product_type. elements {
603+ for field in & * product_type. elements {
618604 let field_name = field
619605 . name
620606 . as_ref ( )
@@ -641,7 +627,7 @@ fn autogen_csharp_product_table_common(
641627 AlgebraicType :: Ref ( type_ref) => {
642628 let ref_type = & ctx. typespace . types [ type_ref. idx ( ) ] ;
643629 if let AlgebraicType :: Sum ( sum_type) = ref_type {
644- if is_enum ( sum_type) {
630+ if sum_type. is_simple_enum ( ) {
645631 writeln ! ( output, "[SpacetimeDB.Enum]" ) . unwrap ( ) ;
646632 } else {
647633 unimplemented ! ( )
@@ -1307,7 +1293,7 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
13071293 AlgebraicType :: Ref ( type_ref) => {
13081294 let ref_type = & ctx. typespace . types [ type_ref. idx ( ) ] ;
13091295 if let AlgebraicType :: Sum ( sum_type) = ref_type {
1310- if is_enum ( sum_type) {
1296+ if sum_type. is_simple_enum ( ) {
13111297 json_args. push_str (
13121298 format ! ( "new SpacetimeDB.EnumWrapper<{}>({})" , arg_type_str, arg_name) . as_str ( ) ,
13131299 ) ;
@@ -1403,7 +1389,8 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
14031389 let arg_name = arg
14041390 . name
14051391 . clone ( )
1406- . unwrap_or_else ( || format ! ( "arg_{}" , i) )
1392+ . unwrap_or_else ( || format ! ( "arg_{}" , i) . into ( ) )
1393+ . deref ( )
14071394 . to_case ( Case :: Pascal ) ;
14081395 let arg_type_str = ty_fmt ( ctx, & arg. algebraic_type , namespace) ;
14091396 writeln ! ( output, ",({arg_type_str})args.{arg_name}" ) . unwrap ( ) ;
@@ -1441,7 +1428,8 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
14411428 let arg_name = arg
14421429 . name
14431430 . clone ( )
1444- . unwrap_or_else ( || format ! ( "arg_{}" , i) )
1431+ . unwrap_or_else ( || format ! ( "arg_{}" , i) . into ( ) )
1432+ . deref ( )
14451433 . to_case ( Case :: Pascal ) ;
14461434 let algebraic_type = convert_algebraic_type ( ctx, & arg. algebraic_type , namespace) ;
14471435 writeln ! (
@@ -1472,7 +1460,8 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
14721460 let arg_name = arg
14731461 . name
14741462 . clone ( )
1475- . unwrap_or_else ( || format ! ( "arg_{}" , i) )
1463+ . unwrap_or_else ( || format ! ( "arg_{}" , i) . into ( ) )
1464+ . deref ( )
14761465 . to_case ( Case :: Pascal ) ;
14771466 let cs_type = ty_fmt ( ctx, & arg. algebraic_type , namespace) ;
14781467 writeln ! ( output, "public {cs_type} {arg_name};" ) . unwrap ( ) ;
@@ -1503,7 +1492,7 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
15031492 . collect ( ) ;
15041493 let reducer_names: Vec < String > = reducers
15051494 . iter ( )
1506- . map ( |reducer| reducer. name . to_case ( Case :: Pascal ) )
1495+ . map ( |reducer| reducer. name . deref ( ) . to_case ( Case :: Pascal ) )
15071496 . collect ( ) ;
15081497
15091498 let use_namespace = true ;
@@ -1570,7 +1559,7 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
15701559 writeln ! ( output) . unwrap ( ) ;
15711560 // Properties for reducer args
15721561 for reducer in & reducers {
1573- let reducer_name = reducer. name . to_case ( Case :: Pascal ) ;
1562+ let reducer_name = reducer. name . deref ( ) . to_case ( Case :: Pascal ) ;
15741563 writeln ! ( output, "public {reducer_name}ArgsStruct {reducer_name}Args" ) . unwrap ( ) ;
15751564 writeln ! ( output, "{{" ) . unwrap ( ) ;
15761565 {
@@ -1598,7 +1587,7 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
15981587 {
15991588 indent_scope ! ( output) ;
16001589 for reducer in & reducers {
1601- let reducer_name = reducer. name . to_case ( Case :: Pascal ) ;
1590+ let reducer_name = reducer. name . deref ( ) . to_case ( Case :: Pascal ) ;
16021591 writeln ! ( output, "case ReducerType.{reducer_name}:" ) . unwrap ( ) ;
16031592 writeln ! ( output, "{{" ) . unwrap ( ) ;
16041593 {
@@ -1611,7 +1600,8 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(St
16111600 let arg_name = arg
16121601 . name
16131602 . clone ( )
1614- . unwrap_or_else ( || format ! ( "arg_{}" , i) )
1603+ . unwrap_or_else ( || format ! ( "arg_{}" , i) . into ( ) )
1604+ . deref ( )
16151605 . to_case ( Case :: Pascal ) ;
16161606 writeln ! ( output, "args.{arg_name}," ) . unwrap ( ) ;
16171607 }
0 commit comments