Files
codeql/misc/codegen/templates/rust_classes.mustache
Paolo Tranquilli c74b6be136 Rust/Codegen: allow to "detach" property emission
By using the `rust.detach` pragma on a property, we make that property
not appear in the generated struct as a field, and provide instead
a `generated::Class::emit_property` function that can be used to emit
the corresponding TRAP entry independently.
2024-09-20 16:06:22 +02:00

88 lines
2.6 KiB
Plaintext

// 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_repeated}}{{^is_unordered}}i: usize, {{/is_unordered}}{{/is_repeated}}value: {{base_type}}, out: &mut trap::Writer) {
out.add_tuple("{{table_name}}", vec![id.into(), {{#is_repeated}}{{^is_unordered}}i.into(), {{/is_unordered}}{{/is_repeated}}value.into()]);
}
{{/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}}