|
1 | 1 | from src.binding_types import TypeInfo
|
2 | 2 | from src.type_parsing_regeces import TypeParsingRegeces
|
3 | 3 | from src.conversion_helper import ConversionHelper
|
| 4 | +from src.conversion_helper import array_accessor_types |
4 | 5 |
|
5 | 6 | var_is_arr_regex = TypeParsingRegeces.IS_VARIABLE_AN_ARRAY_REGEX
|
6 | 7 | var_ty_regex = TypeParsingRegeces.VARIABLE_TYPE_REGEX
|
@@ -37,104 +38,20 @@ def map_types_to_swift(fn_arg, ret_arr_len, java_c_types_none_allowed, tuple_typ
|
37 | 38 | # the way that Swift automatically maps C types when divergent from what people use, like const char * not being a String
|
38 | 39 | swift_raw_type = None
|
39 | 40 |
|
40 |
| - if fn_arg.startswith("LDKThirtyTwoBytes"): |
41 |
| - fn_arg = "uint8_t (*" + fn_arg[18:] + ")[32]" |
42 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
43 |
| - rust_obj = "LDKThirtyTwoBytes" |
44 |
| - swift_raw_type = rust_obj |
45 |
| - arr_access = "data" |
46 |
| - elif fn_arg.startswith("LDKPaymentPreimage"): # disable this conversion using x prefix |
47 |
| - prefix_length = len('LDKPaymentPreimage') + 1 |
48 |
| - fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + ")[32]" |
49 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
50 |
| - rust_obj = "LDKPaymentPreimage" |
51 |
| - swift_raw_type = rust_obj |
52 |
| - arr_access = "data" |
53 |
| - elif fn_arg.startswith("LDKPublicKey"): |
54 |
| - fn_arg = "uint8_t (*" + fn_arg[13:] + ")[33]" |
55 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
56 |
| - rust_obj = "LDKPublicKey" |
57 |
| - swift_raw_type = rust_obj |
58 |
| - arr_access = "compressed_form" |
59 |
| - elif fn_arg.startswith("LDKSecretKey"): |
60 |
| - fn_arg = "uint8_t (*" + fn_arg[13:] + ")[32]" |
61 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
62 |
| - rust_obj = "LDKSecretKey" |
63 |
| - swift_raw_type = rust_obj |
64 |
| - arr_access = "bytes" |
65 |
| - elif fn_arg.startswith("LDKSignature"): |
66 |
| - fn_arg = "uint8_t (*" + fn_arg[13:] + ")[64]" |
67 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
68 |
| - rust_obj = "LDKSignature" |
69 |
| - swift_raw_type = rust_obj |
70 |
| - arr_access = "compact_form" |
71 |
| - elif fn_arg.startswith("LDKThreeBytes"): |
72 |
| - fn_arg = "uint8_t (*" + fn_arg[14:] + ")[3]" |
73 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
74 |
| - rust_obj = "LDKThreeBytes" |
75 |
| - swift_raw_type = rust_obj |
76 |
| - arr_access = "data" |
77 |
| - elif fn_arg.startswith("LDKFourBytes"): |
78 |
| - fn_arg = "uint8_t (*" + fn_arg[13:] + ")[4]" |
79 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
80 |
| - rust_obj = "LDKFourBytes" |
81 |
| - swift_raw_type = rust_obj |
82 |
| - arr_access = "data" |
83 |
| - elif fn_arg.startswith("LDKTenBytes"): |
84 |
| - fn_arg = "uint8_t (*" + fn_arg[12:] + ")[10]" |
85 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
86 |
| - rust_obj = "LDKTenBytes" |
87 |
| - swift_raw_type = rust_obj |
88 |
| - arr_access = "data" |
89 |
| - elif fn_arg.startswith("LDKTwelveBytes"): |
90 |
| - fn_arg = "uint8_t (*" + fn_arg[len('LDKTwelveBytes '):] + ")[12]" |
91 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
92 |
| - rust_obj = "LDKTwelveBytes" |
93 |
| - swift_raw_type = rust_obj |
94 |
| - arr_access = "data" |
95 |
| - elif fn_arg.startswith("LDKSixteenBytes"): |
96 |
| - fn_arg = "uint8_t (*" + fn_arg[16:] + ")[16]" |
97 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
98 |
| - rust_obj = "LDKSixteenBytes" |
99 |
| - swift_raw_type = rust_obj |
100 |
| - arr_access = "data" |
101 |
| - elif fn_arg.startswith("LDKTwentyBytes"): |
102 |
| - fn_arg = "uint8_t (*" + fn_arg[15:] + ")[20]" |
103 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
104 |
| - rust_obj = "LDKTwentyBytes" |
105 |
| - swift_raw_type = rust_obj |
106 |
| - arr_access = "data" |
107 |
| - elif fn_arg.startswith("LDKRecoverableSignature"): |
108 |
| - fn_arg = "uint8_t (*serialized_form)[68]" |
109 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
110 |
| - rust_obj = "LDKRecoverableSignature" |
111 |
| - swift_raw_type = rust_obj |
112 |
| - arr_access = "serialized_form" |
113 |
| - elif fn_arg.startswith("LDKu8slice"): |
114 |
| - fn_arg = "uint8_t (*" + fn_arg[11:] + ")[datalen]" |
115 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
116 |
| - rust_obj = "LDKu8slice" |
117 |
| - arr_access = "data" |
118 |
| - elif fn_arg.startswith("LDKCVec_u8Z"): |
119 |
| - fn_arg = "uint8_t (*" + fn_arg[12:] + ")[datalen]" |
120 |
| - rust_obj = "LDKCVec_u8Z" |
121 |
| - swift_raw_type = rust_obj |
122 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
123 |
| - arr_access = "data" |
124 |
| - elif fn_arg.startswith("LDKCVec_u5Z"): |
125 |
| - prefix_length = len('LDKCVec_u5Z') + 1 |
126 |
| - fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + ")[data]" |
127 |
| - rust_obj = "LDKCVec_u5Z" |
128 |
| - swift_raw_type = rust_obj |
129 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
130 |
| - arr_access = "data" |
131 |
| - elif fn_arg.startswith("LDKTransaction ") or fn_arg == "LDKTransaction": |
132 |
| - fn_arg = "uint8_t (*" + fn_arg[15:] + ")[datalen]" |
133 |
| - rust_obj = "LDKTransaction" |
134 |
| - swift_raw_type = rust_obj |
135 |
| - assert var_is_arr_regex.match(fn_arg[8:]) |
136 |
| - arr_access = "data" |
137 |
| - elif fn_arg.startswith("LDKCVec_"): |
| 41 | + for type_name, access_details in array_accessor_types.items(): |
| 42 | + if fn_arg.startswith(f'{type_name} ') or fn_arg == type_name: # include space |
| 43 | + rust_obj = type_name |
| 44 | + arr_access = access_details.key |
| 45 | + prefix_length = len(type_name) + 1 |
| 46 | + if access_details.size > 0: |
| 47 | + fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + f")[{access_details.size}]" |
| 48 | + assert var_is_arr_regex.match(fn_arg[8:]) |
| 49 | + swift_raw_type = rust_obj |
| 50 | + elif access_details.size == -1 and access_details.length_key is not None: |
| 51 | + fn_arg = "uint8_t (*" + fn_arg[prefix_length:] + f")[{access_details.length_key}]" |
| 52 | + assert var_is_arr_regex.match(fn_arg[8:]) |
| 53 | + break |
| 54 | + if fn_arg.startswith("LDKCVec_"): |
138 | 55 | is_ptr = False
|
139 | 56 | if "*" in fn_arg:
|
140 | 57 | fn_arg = fn_arg.replace("*", "")
|
|
0 commit comments