|
2 | 2 |
|
3 | 3 | #![cfg_attr(any(), rustfmt::skip)]
|
4 | 4 |
|
5 |
| -use crate::trap::{TrapId, TrapEntry}; |
6 |
| -use codeql_extractor::trap; |
| 5 | +use crate::trap; |
7 | 6 | {{#classes}}
|
8 | 7 |
|
| 8 | +#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 9 | +pub struct {{name}}TrapLabel(trap::UntypedLabel); |
| 10 | + |
| 11 | +impl From<trap::UntypedLabel> for {{name}}TrapLabel { |
| 12 | + fn from(value: trap::UntypedLabel) -> Self { |
| 13 | + Self(value) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +impl From<{{name}}TrapLabel> for trap::TrapId<{{name}}> { |
| 18 | + fn from(value: {{name}}TrapLabel) -> Self { |
| 19 | + Self::Label(value) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl trap::Label for {{name}}TrapLabel { |
| 24 | + fn as_untyped(&self) -> trap::UntypedLabel { |
| 25 | + self.0 |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl From<{{name}}TrapLabel> for trap::Arg { |
| 30 | + fn from(value: {{name}}TrapLabel) -> Self { |
| 31 | + value.0.into() |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +{{#table_name}} |
9 | 36 | #[derive(Debug)]
|
10 | 37 | pub struct {{name}} {
|
11 |
| - pub id: TrapId, |
| 38 | + pub id: trap::TrapId<{{name}}>, |
12 | 39 | {{#fields}}
|
13 | 40 | pub {{field_name}}: {{type}},
|
14 | 41 | {{/fields}}
|
15 | 42 | }
|
16 | 43 |
|
17 |
| -impl TrapEntry for {{name}} { |
18 |
| - fn extract_id(&mut self) -> TrapId { |
19 |
| - std::mem::replace(&mut self.id, TrapId::Star) |
| 44 | +impl trap::TrapEntry for {{name}} { |
| 45 | + fn class_name() -> &'static str { "{{name}}" } |
| 46 | +
|
| 47 | + fn extract_id(&mut self) -> trap::TrapId<Self> { |
| 48 | + std::mem::replace(&mut self.id, trap::TrapId::Star) |
20 | 49 | }
|
21 | 50 |
|
22 |
| - fn emit(self, id: trap::Label, out: &mut trap::Writer) { |
| 51 | + fn emit(self, id: Self::Label, out: &mut trap::Writer) { |
23 | 52 | {{#single_field_entries}}
|
24 |
| - out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{#fields}}, self.{{field_name}}.into(){{/fields}}]); |
| 53 | + out.add_tuple("{{table_name}}", vec![id.into(){{#fields}}, self.{{field_name}}.into(){{/fields}}]); |
25 | 54 | {{/single_field_entries}}
|
26 | 55 | {{#fields}}
|
27 | 56 | {{#is_predicate}}
|
28 | 57 | if self.{{field_name}} {
|
29 |
| - out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id)]); |
| 58 | + out.add_tuple("{{table_name}}", vec![id.into()]); |
30 | 59 | }
|
31 | 60 | {{/is_predicate}}
|
32 | 61 | {{#is_optional}}
|
33 | 62 | {{^is_repeated}}
|
34 | 63 | if let Some(v) = self.{{field_name}} {
|
35 |
| - out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id), v.into()]); |
| 64 | + out.add_tuple("{{table_name}}", vec![id.into(), v.into()]); |
36 | 65 | }
|
37 | 66 | {{/is_repeated}}
|
38 | 67 | {{/is_optional}}
|
39 | 68 | {{#is_repeated}}
|
40 | 69 | for (i, v) in self.{{field_name}}.into_iter().enumerate() {
|
41 | 70 | {{^is_optional}}
|
42 |
| - out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]); |
| 71 | + out.add_tuple("{{table_name}}", vec![id.into(){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]); |
43 | 72 | {{/is_optional}}
|
44 | 73 | {{#is_optional}}
|
45 | 74 | if let Some(v) = v {
|
46 |
| - out.add_tuple("{{table_name}}", vec![trap::Arg::Label(id){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]); |
| 75 | + out.add_tuple("{{table_name}}", vec![id.into(){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]); |
47 | 76 | }
|
48 | 77 | {{/is_optional}}
|
49 | 78 | }
|
50 | 79 | {{/is_repeated}}
|
51 | 80 | {{/fields}}
|
52 | 81 | }
|
53 | 82 | }
|
| 83 | +{{/table_name}} |
| 84 | +{{^table_name}} |
| 85 | +{{! virtual class, make it unbuildable }} |
| 86 | +pub struct {{name}} { |
| 87 | + unused: () |
| 88 | +} |
| 89 | +{{/table_name}} |
| 90 | +
|
| 91 | +impl trap::TrapClass for {{name}} { |
| 92 | + type Label = {{name}}TrapLabel; |
| 93 | +} |
| 94 | +{{/classes}} |
| 95 | +
|
| 96 | +// Conversions |
| 97 | +{{#classes}} |
| 98 | +{{#ancestors}} |
| 99 | +impl From<{{name}}TrapLabel> for {{.}}TrapLabel { |
| 100 | + fn from(value: {{name}}TrapLabel) -> Self { |
| 101 | + value.0.into() |
| 102 | + } |
| 103 | +} |
| 104 | +{{/ancestors}} |
54 | 105 | {{/classes}}
|
0 commit comments