Add Variant-Level docs on complex and unitary enums.
authorMatt Corallo <git@bluematt.me>
Sun, 28 Nov 2021 23:17:52 +0000 (23:17 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 28 Nov 2021 23:17:52 +0000 (23:17 +0000)
Fixes #60.

bindingstypes.py
genbindings.py
java_strings.py
typescript_strings.py

index 44b4182eda41814785c219754e51db1891176dee..8c3f0db8e7e520a5cbde12a338e1e4fcb2b4ebfe 100644 (file)
@@ -78,7 +78,8 @@ class TraitMethInfo:
         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
index a2c66863677fe9d7fe758553c1d9a595561af8f8..042f5b92f4d92e061dfaa69e74a109e561a972bd 100755 (executable)
@@ -539,7 +539,7 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}",
                 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)
@@ -550,7 +550,7 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}",
         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:
@@ -573,16 +573,16 @@ with open(sys.argv[1]) as in_h, open(f"{sys.argv[2]}/bindings{consts.file_ext}",
                             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:
index 15c3ac4783ad222aef4647d40fcf1ebb6bec7204..a83671db48a27c1d23cd9e3875d5736765afca88 100644 (file)
@@ -614,8 +614,10 @@ import javax.annotation.Nullable;
             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"
@@ -627,19 +629,19 @@ import javax.annotation.Nullable;
         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
@@ -1049,7 +1051,9 @@ import javax.annotation.Nullable;
         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 = ""
index 54159d8d85099d3c0db71e0710dd7194d5618c2f..270eeccc85c30ad486e782abc82509f5b62c35b4 100644 (file)
@@ -472,9 +472,11 @@ const decodeString = (stringPointer, free = true) => {
 
         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"
@@ -483,7 +485,7 @@ const decodeString = (stringPointer, free = true) => {
         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"