|
| 1 | +""" |
| 2 | +Rust trap class generation |
| 3 | +""" |
| 4 | + |
| 5 | +import functools |
| 6 | +import typing |
| 7 | + |
| 8 | +import inflection |
| 9 | + |
| 10 | +from misc.codegen.lib import rust, schema |
| 11 | +from misc.codegen.loaders import schemaloader |
| 12 | + |
| 13 | + |
| 14 | +def _get_type(t: str) -> str: |
| 15 | + match t: |
| 16 | + case None | "boolean": # None means a predicate |
| 17 | + return "bool" |
| 18 | + case "string": |
| 19 | + return "String" |
| 20 | + case "int": |
| 21 | + return "i32" |
| 22 | + case _ if t[0].isupper(): |
| 23 | + return "TrapLabel" |
| 24 | + case _: |
| 25 | + return t |
| 26 | + |
| 27 | + |
| 28 | +def _get_field(cls: schema.Class, p: schema.Property) -> rust.Field: |
| 29 | + table_name = None |
| 30 | + if not p.is_single: |
| 31 | + table_name = f"{cls.name}_{p.name}" |
| 32 | + if p.is_predicate: |
| 33 | + table_name = inflection.underscore(table_name) |
| 34 | + else: |
| 35 | + table_name = inflection.tableize(table_name) |
| 36 | + args = dict( |
| 37 | + field_name=p.name + ("_" if p.name in rust.keywords else ""), |
| 38 | + base_type=_get_type(p.type), |
| 39 | + is_optional=p.is_optional, |
| 40 | + is_repeated=p.is_repeated, |
| 41 | + is_predicate=p.is_predicate, |
| 42 | + is_unordered=p.is_unordered, |
| 43 | + table_name=table_name, |
| 44 | + ) |
| 45 | + args.update(rust.get_field_override(p.name)) |
| 46 | + return rust.Field(**args) |
| 47 | + |
| 48 | + |
| 49 | +def _get_properties( |
| 50 | + cls: schema.Class, lookup: dict[str, schema.Class] |
| 51 | +) -> typing.Iterable[schema.Property]: |
| 52 | + for b in cls.bases: |
| 53 | + yield from _get_properties(lookup[b], lookup) |
| 54 | + yield from cls.properties |
| 55 | + |
| 56 | + |
| 57 | +class Processor: |
| 58 | + def __init__(self, data: schema.Schema): |
| 59 | + self._classmap = data.classes |
| 60 | + |
| 61 | + def _get_class(self, name: str) -> rust.Class: |
| 62 | + cls = self._classmap[name] |
| 63 | + return rust.Class( |
| 64 | + name=name, |
| 65 | + fields=[ |
| 66 | + _get_field(cls, p) |
| 67 | + for p in _get_properties(cls, self._classmap) |
| 68 | + if "rust_skip" not in p.pragmas and not p.synth |
| 69 | + ], |
| 70 | + table_name=inflection.tableize(cls.name), |
| 71 | + ) |
| 72 | + |
| 73 | + def get_classes(self): |
| 74 | + ret = {"": []} |
| 75 | + for k, cls in self._classmap.items(): |
| 76 | + if not cls.synth and not cls.derived: |
| 77 | + ret.setdefault(cls.group, []).append(self._get_class(cls.name)) |
| 78 | + return ret |
| 79 | + |
| 80 | + |
| 81 | +def generate(opts, renderer): |
| 82 | + assert opts.rust_output |
| 83 | + processor = Processor(schemaloader.load_file(opts.schema)) |
| 84 | + out = opts.rust_output |
| 85 | + groups = set() |
| 86 | + for group, classes in processor.get_classes().items(): |
| 87 | + group = group or "top" |
| 88 | + groups.add(group) |
| 89 | + renderer.render( |
| 90 | + rust.ClassList( |
| 91 | + classes, |
| 92 | + opts.schema, |
| 93 | + ), |
| 94 | + out / f"{group}.rs", |
| 95 | + ) |
| 96 | + renderer.render( |
| 97 | + rust.ModuleList( |
| 98 | + groups, |
| 99 | + opts.schema, |
| 100 | + ), |
| 101 | + out / f"mod.rs", |
| 102 | + ) |
0 commit comments