| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,13 +72,28 @@ def __init__(self, data: schema.Schema): | |||
| 72 | 72 | ||
| 73 | 73 | def _get_class(self, name: str) -> rust.Class: | |
| 74 | 74 | cls = self._classmap[name] | |
| 75 | + properties = [ | ||
| 76 | + (c, p) | ||
| 77 | + for c, p in _get_properties(cls, self._classmap) | ||
| 78 | + if "rust_skip" not in p.pragmas and not p.synth | ||
| 79 | + ] | ||
| 80 | + fields = [] | ||
| 81 | + detached_fields = [] | ||
| 82 | + for c, p in properties: | ||
| 83 | + if "rust_detach" in p.pragmas: | ||
| 84 | + # only generate detached fields in the actual class defining them, not the derived ones | ||
| 85 | + if c is cls: | ||
| 86 | + # TODO lift this restriction if required (requires change in dbschemegen as well) | ||
| 87 | + assert c.derived or not p.is_single, \ | ||
| 88 | + f"property {p.name} in concrete class marked as detached but not optional" | ||
| 89 | + detached_fields.append(_get_field(c, p)) | ||
| 90 | + elif not cls.derived: | ||
| 91 | + # for non-detached ones, only generate fields in the concrete classes | ||
| 92 | + fields.append(_get_field(c, p)) | ||
| 75 | 93 | return rust.Class( | |
| 76 | 94 | name=name, | |
| 77 | - fields=[ | ||
| 78 | - _get_field(c, p) | ||
| 79 | - for c, p in _get_properties(cls, self._classmap) | ||
| 80 | - if "rust_skip" not in p.pragmas and not p.synth | ||
| 81 | - ] if not cls.derived else [], | ||
| 95 | + fields=fields, | ||
| 96 | + detached_fields=detached_fields, | ||
| 82 | 97 | ancestors=sorted(set(a.name for a in _get_ancestors(cls, self._classmap))), | |
| 83 | 98 | entry_table=inflection.tableize(cls.name) if not cls.derived else None, | |
| 84 | 99 | ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | import dataclasses | |
| 2 | 2 | import re | |
| 3 | 3 | import typing | |
| 4 | + import inflection | ||
| 4 | 5 | ||
| 5 | 6 | # taken from https://doc.rust-lang.org/reference/keywords.html | |
| 6 | 7 | keywords = { | |
@@ -106,12 +107,17 @@ def is_single(self): | |||
| 106 | 107 | def is_label(self): | |
| 107 | 108 | return self.base_type == "trap::Label" | |
| 108 | 109 | ||
| 110 | + @property | ||
| 111 | + def singular_field_name(self) -> str: | ||
| 112 | + return inflection.singularize(self.field_name.rstrip("_")) | ||
| 113 | + | ||
| 109 | 114 | ||
| 110 | 115 | @dataclasses.dataclass | |
| 111 | 116 | class Class: | |
| 112 | 117 | name: str | |
| 113 | 118 | entry_table: str | None = None | |
| 114 | 119 | fields: list[Field] = dataclasses.field(default_factory=list) | |
| 120 | + detached_fields: list[Field] = dataclasses.field(default_factory=list) | ||
| 115 | 121 | ancestors: list[str] = dataclasses.field(default_factory=list) | |
| 116 | 122 | ||
| 117 | 123 | @property | |
@@ -128,6 +134,10 @@ def single_field_entries(self) -> dict[str, list[dict]]: | |||
| 128 | 134 | ret.setdefault(f.table_name, []).append(f) | |
| 129 | 135 | return [{"table_name": k, "fields": v} for k, v in ret.items()] | |
| 130 | 136 | ||
| 137 | + @property | ||
| 138 | + def has_detached_fields(self) -> bool: | ||
| 139 | + return bool(self.detached_fields) | ||
| 140 | + | ||
| 131 | 141 | ||
| 132 | 142 | @dataclasses.dataclass | |
| 133 | 143 | class ClassList: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -237,6 +237,7 @@ def __getitem__(self, item): | |||
| 237 | 237 | ||
| 238 | 238 | cpp.add(_Pragma("skip")) | |
| 239 | 239 | ||
| 240 | + rust.add(_Pragma("detach")) | ||
| 240 | 241 | rust.add(_Pragma("skip_doc_test")) | |
| 241 | 242 | ||
| 242 | 243 | rust.add(_ParametrizedClassPragma("doc_test_signature", factory=lambda signature: signature)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,6 +59,16 @@ pub struct {{name}} { | |||
| 59 | 59 | _unused: () | |
| 60 | 60 | } | |
| 61 | 61 | {{/is_entry}} | |
| 62 | + {{#has_detached_fields}} | ||
| 63 | + | ||
| 64 | + impl {{name}} { | ||
| 65 | + {{#detached_fields}} | ||
| 66 | + pub fn emit_{{singular_field_name}}(id: trap::Label<Self>, {{#is_repeated}}{{^is_unordered}}i: usize, {{/is_unordered}}{{/is_repeated}}value: {{base_type}}, out: &mut trap::Writer) { | ||
| 67 | + out.add_tuple("{{table_name}}", vec![id.into(), {{#is_repeated}}{{^is_unordered}}i.into(), {{/is_unordered}}{{/is_repeated}}value.into()]); | ||
| 68 | + } | ||
| 69 | + {{/detached_fields}} | ||
| 70 | + } | ||
| 71 | + {{/has_detached_fields}} | ||
| 62 | 72 | ||
| 63 | 73 | impl trap::TrapClass for {{name}} { | |
| 64 | 74 | fn class_name() -> &'static str { "{{name}}" } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments