-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathrust_classes.mustache
More file actions
101 lines (92 loc) · 3.17 KB
/
rust_classes.mustache
File metadata and controls
101 lines (92 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// generated by {{generator}}, do not edit
#![cfg_attr(any(), rustfmt::skip)]
use crate::trap;
{{#classes}}
{{#is_entry}}
#[derive(Debug)]
pub struct {{name}} {
pub id: trap::TrapId<{{name}}>,
{{#fields}}
pub {{field_name}}: {{type}},
{{/fields}}
}
impl trap::TrapEntry for {{name}} {
fn extract_id(&mut self) -> trap::TrapId<Self> {
std::mem::replace(&mut self.id, trap::TrapId::Star)
}
fn emit(self, id: trap::Label<Self>, out: &mut trap::Writer) {
{{#single_field_entries}}
out.add_tuple("{{entry_table}}", vec![id.into(){{#fields}}, self.{{field_name}}.into(){{/fields}}]);
{{/single_field_entries}}
{{#fields}}
{{#is_predicate}}
if self.{{field_name}} {
out.add_tuple("{{table_name}}", vec![id.into()]);
}
{{/is_predicate}}
{{#is_optional}}
{{^is_repeated}}
if let Some(v) = self.{{field_name}} {
out.add_tuple("{{table_name}}", vec![id.into(), v.into()]);
}
{{/is_repeated}}
{{/is_optional}}
{{#is_repeated}}
for (i, v) in self.{{field_name}}.into_iter().enumerate() {
{{^is_optional}}
out.add_tuple("{{table_name}}", vec![id.into(){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
{{/is_optional}}
{{#is_optional}}
if let Some(v) = v {
out.add_tuple("{{table_name}}", vec![id.into(){{^is_unordered}}, i.into(){{/is_unordered}}, v.into()]);
}
{{/is_optional}}
}
{{/is_repeated}}
{{/fields}}
}
}
{{/is_entry}}
{{^is_entry}}
{{! virtual class, make it unbuildable }}
#[derive(Debug)]
pub struct {{name}} {
_unused: ()
}
{{/is_entry}}
{{#has_detached_fields}}
impl {{name}} {
{{#detached_fields}}
pub fn emit_{{singular_field_name}}(id: trap::Label<Self>{{^is_predicate}}{{#is_repeated}}{{^is_unordered}}, i: usize{{/is_unordered}}{{/is_repeated}}, value: {{base_type}}{{/is_predicate}}, out: &mut trap::Writer) {
out.add_tuple("{{table_name}}", vec![id.into(){{^is_predicate}}{{#is_repeated}}{{^is_unordered}}, i.into(){{/is_unordered}}{{/is_repeated}}, value.into(){{/is_predicate}}]);
}
{{#is_repeated}}
pub fn emit_{{field_name}}(id: trap::Label<Self>, values: impl IntoIterator<Item={{base_type}}>, out: &mut trap::Writer) {
values
.into_iter()
{{^is_unordered}}
.enumerate()
.for_each(|(i, value)| Self::emit_{{singular_field_name}}(id, i, value, out));
{{/is_unordered}}
{{#is_unordered}}
.for_each(|value| Self::emit_{{singular_field_name}}(id, value, out));
{{/is_unordered}}
}
{{/is_repeated}}
{{/detached_fields}}
}
{{/has_detached_fields}}
impl trap::TrapClass for {{name}} {
fn class_name() -> &'static str { "{{name}}" }
}
{{#ancestors}}
impl From<trap::Label<{{name}}>> for trap::Label<{{.}}> {
fn from(value: trap::Label<{{name}}>) -> Self {
// SAFETY: this is safe because in the dbscheme {{name}} is a subclass of {{.}}
unsafe {
Self::from_untyped(value.as_untyped())
}
}
}
{{/ancestors}}
{{/classes}}