|
| 1 | +import re |
| 2 | +import os |
| 3 | + |
| 4 | + |
| 5 | +class VectorGenerator: |
| 6 | + |
| 7 | + def __init__(self) -> None: |
| 8 | + super().__init__() |
| 9 | + template_path = f'{os.path.dirname(__file__)}/../../templates/BindingsTemplate.swift' |
| 10 | + with open(template_path, 'r') as template_handle: |
| 11 | + self.bindings_template = template_handle.read() |
| 12 | + self.vector_template_regex = re.compile( |
| 13 | + "(\/\* VECTOR_METHODS_START \*\/\n)(.*)(\n[\t ]*\/\* VECTOR_METHODS_END \*\/)", |
| 14 | + flags=re.MULTILINE | re.DOTALL) |
| 15 | + self.template = self.vector_template_regex.search(self.bindings_template).group(2) |
| 16 | + self.mutating_vector_methods = '' |
| 17 | + |
| 18 | + def generate_vector(self, vector_name, vector_type_details): |
| 19 | + if not vector_type_details.is_primitive: |
| 20 | + # TODO: add non-primitive tuple support |
| 21 | + return |
| 22 | + mutating_current_vector_methods = self.template |
| 23 | + mutating_current_vector_methods = mutating_current_vector_methods.replace('LDKCVec_rust_primitive', vector_name) |
| 24 | + mutating_current_vector_methods = mutating_current_vector_methods.replace('SwiftPrimitive', |
| 25 | + vector_type_details.primitive_swift_counterpart) |
| 26 | + self.mutating_vector_methods += "\n"+mutating_current_vector_methods+"\n" |
| 27 | + |
| 28 | + def finalize(self): |
| 29 | + filled_template = self.vector_template_regex.sub(f'\g<1>{self.mutating_vector_methods}\g<3>', |
| 30 | + self.bindings_template) |
| 31 | + |
| 32 | + # store the output |
| 33 | + output_path = f'{os.path.dirname(__file__)}/../../output/LDK/Bindings.swift' |
| 34 | + output_directory = os.path.dirname(output_path) |
| 35 | + if not os.path.exists(output_directory): |
| 36 | + os.makedirs(output_directory) |
| 37 | + with open(output_path, "w") as f: |
| 38 | + f.write(filled_template) |
0 commit comments