Fixes #60.
self.docs = docs
class ComplexEnumVariantInfo:
- def __init__(self, var_name, fields, tuple_variant):
+ def __init__(self, var_name, var_docs, fields, tuple_variant):
self.var_name = var_name
+ self.var_docs = var_docs
self.fields = fields
self.tuple_variant = tuple_variant
elif idx == len(field_lines) - 1:
assert(struct_line == "")
assert struct_name.startswith("LDK")
- (c_out, native_file_out, native_out) = consts.native_c_unitary_enum_map(struct_name[3:], [x.strip().strip(",") for x, _ in field_lines[1:-3]], enum_doc_comment)
+ (c_out, native_file_out, native_out) = consts.native_c_unitary_enum_map(struct_name[3:], [(x.strip().strip(","), y) for x, y in field_lines[1:-3]], enum_doc_comment)
write_c(c_out)
out_java_enum.write(native_file_out)
out_java.write(native_out)
enum_variants = []
tag_field_lines = union_enum_items["field_lines"]
contains_trait = False
- for idx, (struct_line, _) in enumerate(tag_field_lines):
+ for idx, (struct_line, variant_docs) in enumerate(tag_field_lines):
if idx == 0:
assert(struct_line == "typedef enum %s_Tag {" % struct_name)
elif idx == len(tag_field_lines) - 3:
else:
field_conv = type_mapping_generator.map_type_with_info(field_ty, False, None, False, True, False)
fields.append((field_conv, field_docs))
- enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, False))
+ enum_variants.append(ComplexEnumVariantInfo(variant_name, variant_docs, fields, False))
elif camel_to_snake(variant_name) in inline_enum_variants:
# TODO: If we ever have a rust enum Variant(Option<Struct>) we need to pipe
# docs through to there, and then potentially mark the field nullable.
mapped = type_mapping_generator.map_type(inline_enum_variants[camel_to_snake(variant_name)] + " " + camel_to_snake(variant_name), False, None, False, True)
contains_trait |= mapped.ty_info.contains_trait
fields.append((mapped, None))
- enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True))
+ enum_variants.append(ComplexEnumVariantInfo(variant_name, variant_docs, fields, True))
else:
- enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True))
+ enum_variants.append(ComplexEnumVariantInfo(variant_name, variant_docs, fields, True))
complex_enums[struct_name] = contains_trait
with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum:
out_java_enum += "/**\n * " + enum_doc_comment.replace("\n", "\n * ") + "\n */\n"
out_java_enum += "public enum " + struct_name + " {\n"
ord_v = 0
- for var in variants:
- out_java_enum = out_java_enum + "\t" + var + ",\n"
+ for var, var_docs in variants:
+ if var_docs is not None:
+ out_java_enum += "\t/**\n\t * " + var_docs.replace("\n", "\n\t * ") + "\n\t */\n"
+ out_java_enum += "\t" + var + ",\n"
out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
ord_v = ord_v + 1
out_java_enum = out_java_enum + "\t; static native void init();\n"
out_c = out_c + "}\n"
out_c = out_c + "static jclass " + struct_name + "_class = NULL;\n"
- for var in variants:
+ for var, _ in variants:
out_c = out_c + "static jfieldID " + struct_name + "_" + var + " = NULL;\n"
out_c = out_c + self.c_fn_ty_pfx + "void JNICALL Java_org_ldk_enums_" + struct_name.replace("_", "_1") + "_init (" + self.c_fn_args_pfx + ") {\n"
out_c = out_c + "\t" + struct_name + "_class = (*env)->NewGlobalRef(env, clz);\n"
out_c = out_c + "\tCHECK(" + struct_name + "_class != NULL);\n"
- for var in variants:
+ for var, _ in variants:
out_c = out_c + "\t" + struct_name + "_" + var + " = (*env)->GetStaticFieldID(env, " + struct_name + "_class, \"" + var + "\", \"Lorg/ldk/enums/" + struct_name + ";\");\n"
out_c = out_c + "\tCHECK(" + struct_name + "_" + var + " != NULL);\n"
out_c = out_c + "}\n"
out_c = out_c + "static inline jclass LDK" + struct_name + "_to_java(JNIEnv *env, LDK" + struct_name + " val) {\n"
out_c = out_c + "\tswitch (val) {\n"
ord_v = 0
- for var in variants:
+ for var, _ in variants:
out_c = out_c + "\t\tcase " + var + ":\n"
out_c = out_c + "\t\t\treturn (*env)->GetStaticObjectField(env, " + struct_name + "_class, " + struct_name + "_" + var + ");\n"
ord_v = ord_v + 1
out_java += ("\t\tprivate " + struct_name + "() {}\n")
for var in variant_list:
out_java += ("\t\tpublic final static class " + var.var_name + " extends " + struct_name + " {\n")
- java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var.var_name + " extends " + java_hu_type + " {\n"
+ if var.var_docs is not None:
+ java_hu_subclasses += "\t/**\n\t * " + var.var_docs.replace("\n", "\n\t * ") + "\n\t */\n"
+ java_hu_subclasses += "\tpublic final static class " + var.var_name + " extends " + java_hu_type + " {\n"
out_java_enum += ("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var.var_name + ".class) {\n")
out_java_enum += ("\t\t\treturn new " + var.var_name + "(ptr, (bindings." + struct_name + "." + var.var_name + ")raw_val);\n")
init_meth_jty_str = ""
out_typescript_enum_fields = ""
- for var in variants:
+ for var, var_docs in variants:
out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var)
ord_v = ord_v + 1
+ if var_docs is not None:
+ out_typescript_enum_fields += f"/**\n * {var_docs}\n */\n"
out_typescript_enum_fields += f"{var},\n\t\t\t\t"
out_c = out_c + "\t}\n"
out_c = out_c + "\tabort();\n"
out_c = out_c + "static inline int32_t LDK" + struct_name + "_to_js(LDK" + struct_name + " val) {\n"
out_c = out_c + "\tswitch (val) {\n"
ord_v = 0
- for var in variants:
+ for var, _ in variants:
out_c = out_c + "\t\tcase " + var + ": return %d;\n" % ord_v
ord_v = ord_v + 1
out_c = out_c + "\t\tdefault: abort();\n"