@@ -299,7 +299,7 @@ def java_c_types(fn_arg, ret_arr_len):
299299 else :
300300 c_ty = consts .ptr_c_ty
301301 java_ty = consts .ptr_native_ty
302- java_hu_ty = ma .group (1 ).strip ().replace ("LDKCResult" , "Result" ).replace ("LDK" , "" )
302+ java_hu_ty = ma .group (1 ).strip ().replace ("LDKCOption" , "Option" ). replace ( " LDKCResult" , "Result" ).replace ("LDK" , "" )
303303 fn_ty_arg = "J"
304304 fn_arg = ma .group (2 ).strip ()
305305 rust_obj = ma .group (1 ).strip ()
@@ -381,7 +381,10 @@ def map_fn(line, re_match, ret_arr_len, c_call_string, doc_comment):
381381 method_arguments = method_comma_separated_arguments .split (',' )
382382
383383 is_free = method_name .endswith ("_free" )
384- struct_meth = method_name .split ("_" )[0 ]
384+ if method_name .startswith ("COption" ) or method_name .startswith ("CResult" ):
385+ struct_meth = method_name .rsplit ("Z" , 1 )[0 ][1 :] + "Z"
386+ else :
387+ struct_meth = method_name .split ("_" )[0 ]
385388
386389 return_type_info = type_mapping_generator .map_type (method_return_type , True , ret_arr_len , False , False )
387390
@@ -437,7 +440,11 @@ def map_fn(line, re_match, ret_arr_len, c_call_string, doc_comment):
437440 write_c (out_c_delta )
438441
439442 out_java_struct = None
440- if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs ) and not is_free :
443+ expected_struct = "LDK" + struct_meth
444+ expected_cstruct = "LDKC" + struct_meth
445+ if (expected_struct in opaque_structs or expected_struct in trait_structs
446+ or expected_struct in complex_enums or expected_cstruct in complex_enums
447+ or expected_cstruct in result_types ) and not is_free :
441448 out_java_struct = open (f"{ sys .argv [3 ]} /structs/{ struct_meth } { consts .file_ext } " , "a" )
442449 elif method_name .startswith ("C2Tuple_" ) and method_name .endswith ("_read" ):
443450 struct_meth = method_name .rsplit ("_" , 1 )[0 ]
@@ -462,8 +469,8 @@ def map_unitary_enum(struct_name, field_lines, enum_doc_comment):
462469 out_java_enum .write (native_file_out )
463470 out_java .write (native_out )
464471
465- def map_complex_enum (struct_name , union_enum_items , enum_doc_comment ):
466- java_hu_type = struct_name .replace ("LDK" , "" )
472+ def map_complex_enum (struct_name , union_enum_items , inline_enum_variants , enum_doc_comment ):
473+ java_hu_type = struct_name .replace ("LDK" , "" ). replace ( "COption" , "Option" )
467474 complex_enums .add (struct_name )
468475
469476 enum_variants = []
@@ -485,13 +492,12 @@ def map_complex_enum(struct_name, union_enum_items, enum_doc_comment):
485492 for idx , field in enumerate (enum_var_lines ):
486493 if idx != 0 and idx < len (enum_var_lines ) - 2 :
487494 fields .append (type_mapping_generator .map_type (field .strip (' ;' ), False , None , False , True ))
488- else :
489- # TODO: Assert line format
490- pass
495+ enum_variants .append (ComplexEnumVariantInfo (variant_name , fields , False ))
496+ elif camel_to_snake (variant_name ) in inline_enum_variants :
497+ fields .append (type_mapping_generator .map_type (inline_enum_variants [camel_to_snake (variant_name )] + " " + camel_to_snake (variant_name ), False , None , False , True ))
498+ enum_variants .append (ComplexEnumVariantInfo (variant_name , fields , True ))
491499 else :
492- # TODO: Assert line format
493- pass
494- enum_variants .append (ComplexEnumVariantInfo (variant_name , fields ))
500+ enum_variants .append (ComplexEnumVariantInfo (variant_name , fields , True ))
495501
496502 with open (f"{ sys .argv [3 ]} /structs/{ java_hu_type } { consts .file_ext } " , "w" ) as out_java_enum :
497503 (out_java_addendum , out_java_enum_addendum , out_c_addendum ) = consts .map_complex_enum (struct_name , enum_variants , camel_to_snake , enum_doc_comment )
@@ -600,17 +606,7 @@ def map_result(struct_name, res_ty, err_ty):
600606 else :
601607 out_java_struct .write ("\t \t \t this.res = bindings." + struct_name + "_get_ok(ptr);\n " )
602608 out_java_struct .write ("\t \t }\n " )
603- if struct_name .startswith ("LDKCResult_None" ):
604- out_java_struct .write ("\t \t public " + human_ty + "_OK() {\n \t \t \t this(null, bindings.C" + human_ty + "_ok());\n " )
605- else :
606- out_java_struct .write ("\t \t public " + human_ty + "_OK(" + res_map .java_hu_ty + " res) {\n " )
607- if res_map .from_hu_conv is not None :
608- out_java_struct .write ("\t \t \t this(null, bindings.C" + human_ty + "_ok(" + res_map .from_hu_conv [0 ] + "));\n " )
609- if res_map .from_hu_conv [1 ] != "" :
610- out_java_struct .write ("\t \t \t " + res_map .from_hu_conv [1 ].replace ("\n " , "\n \t \t \t " ) + ";\n " )
611- else :
612- out_java_struct .write ("\t \t \t this(null, bindings.C" + human_ty + "_ok(res));\n " )
613- out_java_struct .write ("\t \t }\n \t }\n \n " )
609+ out_java_struct .write ("\t }\n \n " )
614610
615611 out_java .write ("\t public static native " + err_map .java_ty + " " + struct_name + "_get_err(long arg);\n " )
616612 write_c (consts .c_fn_ty_pfx + err_map .c_ty + " " + consts .c_fn_name_define_pfx (struct_name + "_get_err" , True ) + consts .ptr_c_ty + " arg) {\n " )
@@ -638,17 +634,7 @@ def map_result(struct_name, res_ty, err_ty):
638634 out_java_struct .write ("\t \t \t this.err = bindings." + struct_name + "_get_err(ptr);\n " )
639635 out_java_struct .write ("\t \t }\n " )
640636
641- if struct_name .endswith ("NoneZ" ):
642- out_java_struct .write ("\t \t public " + human_ty + "_Err() {\n \t \t \t this(null, bindings.C" + human_ty + "_err());\n " )
643- else :
644- out_java_struct .write ("\t \t public " + human_ty + "_Err(" + err_map .java_hu_ty + " err) {\n " )
645- if err_map .from_hu_conv is not None :
646- out_java_struct .write ("\t \t \t this(null, bindings.C" + human_ty + "_err(" + err_map .from_hu_conv [0 ] + "));\n " )
647- if err_map .from_hu_conv [1 ] != "" :
648- out_java_struct .write ("\t \t \t " + err_map .from_hu_conv [1 ].replace ("\n " , "\n \t \t \t " ) + ";\n " )
649- else :
650- out_java_struct .write ("\t \t \t this(null, bindings.C" + human_ty + "_err(err));\n " )
651- out_java_struct .write ("\t \t }\n \t }\n }\n " )
637+ out_java_struct .write ("\t }\n \n " )
652638
653639 def map_tuple (struct_name , field_lines ):
654640 out_java .write ("\t public static native long " + struct_name + "_new(" )
@@ -864,7 +850,25 @@ def map_tuple(struct_name, field_lines):
864850 enum_var_name = struct_name .split ("_" )
865851 union_enum_items [enum_var_name [0 ]][enum_var_name [1 ]] = field_lines
866852 elif struct_name in union_enum_items :
867- map_complex_enum (struct_name , union_enum_items [struct_name ], last_block_comment )
853+ tuple_variants = {}
854+ elem_items = - 1
855+ for line in field_lines :
856+ if line == " struct {" :
857+ elem_items = 0
858+ elif line == " };" :
859+ elem_items = - 1
860+ elif elem_items > - 1 :
861+ line = line .strip ()
862+ if line .startswith ("struct " ):
863+ line = line [7 :]
864+ split = line .split (" " )
865+ assert len (split ) == 2
866+ tuple_variants [split [1 ].strip (";" )] = split [0 ]
867+ elem_items += 1
868+ if elem_items > 1 :
869+ # We don't currently support tuple variant with more than one element
870+ assert False
871+ map_complex_enum (struct_name , union_enum_items [struct_name ], tuple_variants , last_block_comment )
868872 last_block_comment = None
869873 elif is_unitary_enum :
870874 map_unitary_enum (struct_name , field_lines , last_block_comment )
@@ -927,6 +931,12 @@ def map_tuple(struct_name, field_lines):
927931 for struct_name in trait_structs :
928932 with open (f"{ sys .argv [3 ]} /structs/{ struct_name .replace ('LDK' , '' )} { consts .file_ext } " , "a" ) as out_java_struct :
929933 out_java_struct .write ("}\n " )
934+ for struct_name in complex_enums :
935+ with open (f"{ sys .argv [3 ]} /structs/{ struct_name .replace ('LDK' , '' ).replace ('COption' , 'Option' )} { consts .file_ext } " , "a" ) as out_java_struct :
936+ out_java_struct .write ("}\n " )
937+ for struct_name in result_types :
938+ with open (f"{ sys .argv [3 ]} /structs/{ struct_name .replace ('LDKCResult' , 'Result' )} { consts .file_ext } " , "a" ) as out_java_struct :
939+ out_java_struct .write ("}\n " )
930940
931941with open (sys .argv [4 ], "w" ) as out_c :
932942 out_c .write (consts .c_file_pfx )
0 commit comments