mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Rust: archiving + skeleton def translator
This commit is contained in:
54
misc/codegen/templates/rust_classes.mustache
Normal file
54
misc/codegen/templates/rust_classes.mustache
Normal file
@@ -0,0 +1,54 @@
|
||||
// generated by {{generator}}
|
||||
|
||||
use crate::trap::{TrapLabel, TrapEntry, quoted};
|
||||
use std::io::Write;
|
||||
|
||||
{{#classes}}
|
||||
#[derive(Debug)]
|
||||
pub struct {{name}} {
|
||||
pub key: Option<String>,
|
||||
{{#fields}}
|
||||
pub {{field_name}}: {{type}},
|
||||
{{/fields}}
|
||||
}
|
||||
|
||||
impl TrapEntry for {{name}} {
|
||||
type Label = TrapLabel;
|
||||
|
||||
fn prefix() -> &'static str { "{{name}}_" }
|
||||
|
||||
fn key(&self) -> Option<&str> { self.key.as_ref().map(String::as_str) }
|
||||
|
||||
fn emit<W: Write>(&self, id: &Self::Label, out: &mut W) -> std::io::Result<()> {
|
||||
write!(out, "{{table_name}}({id}{{#single_fields}}, {}{{/single_fields}})\n"{{#single_fields}}, {{#emitter}}self.{{field_name}}{{/emitter}}{{/single_fields}})?;
|
||||
{{#fields}}
|
||||
{{#is_predicate}}
|
||||
if self.{{field_name}} {
|
||||
write!(out, "{{table_name}}({id})\n")?;
|
||||
}
|
||||
{{/is_predicate}}
|
||||
{{#is_optional}}
|
||||
{{^is_repeated}}
|
||||
if let Some(ref v) = &self.{{field_name}} {
|
||||
write!(out, "{{table_name}}({id}, {})\n", {{#emitter}}v{{/emitter}})?;
|
||||
}
|
||||
{{/is_repeated}}
|
||||
{{/is_optional}}
|
||||
{{#is_repeated}}
|
||||
for (i, &ref v) in self.{{field_name}}.iter().enumerate() {
|
||||
{{^is_optional}}
|
||||
write!(out, "{{table_name}}({id}, {{^is_unordered}}{}, {{/is_unordered}}{})\n", {{^is_unordered}}i, {{/is_unordered}}{{#emitter}}v{{/emitter}})?;
|
||||
{{/is_optional}}
|
||||
{{#is_optional}}
|
||||
if let Some(ref vv) = &v {
|
||||
write!(out, "{{table_name}}({id}, {{^is_unordered}}{}, {{/is_unordered}}{})\n", {{^is_unordered}}i, {{/is_unordered}}{{#emitter}}vv{{/emitter}})?;
|
||||
}
|
||||
{{/is_optional}}
|
||||
}
|
||||
{{/is_repeated}}
|
||||
{{/fields}}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
{{/classes}}
|
||||
Reference in New Issue
Block a user