1
0

feat: add rust support for code gen

This commit is contained in:
2026-02-11 10:27:44 +08:00
parent fdf2a4fc22
commit 3310cac100
3 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
{%- for fct in payload.fcts %}
/// {{ fct.fct_name }}
///
/// # Parameters
///
{%- for param in fct.fct_params %}
/// - `{{ param.var_name }}`: Direction: {% if param.is_input -%} input {%- else -%} output {%- endif %}. C++ type: `{{ param.var_type.to_c_type() }}`. {{ param.var_desc }}
{%- endfor %}
///
/// # Return
///
/// True if no error, otherwise False.
pub unsafe fn {{ fct.fct_name }}() -> bool;
{%- endfor %}

View File

@@ -83,6 +83,25 @@ class RenderUtils:
"""
return RenderUtils.REGEX_CS_TO_LITERAL_NUMBER.sub("", numstr, 1)
@staticmethod
def to_rs_num_literal(numstr: str) -> str:
"""
Convert given string into Rust number literal style.
Number literal declaration in Rust is slightly different with C++.
C++ support U and L but Rust use another complete suffix mode to decide the type of numeric literal (u32, i32 and etc).
However, Rust can properly deduce the correct type of number literal,
so we just need simply remove any suffix.
This function is only served for C# code generation.
:param numstr: The captured number.
:return: The Rust style number string.
"""
# We reuse existing function
return RenderUtils.to_py_num_literal(numstr)
REGEX_PY_EXT_HUMANRDABLE_ENTRY_NAME: typing.ClassVar[re.Pattern] = re.compile("^[a-zA-Z0-9]+_")
@staticmethod

View File

@@ -0,0 +1,32 @@
{%- for benum in payload.iter_enums() %}
{%- if benum.get_enum_comment() is not none %}
{{ benum.get_enum_comment() | block_comment('/// ') }}
{%- endif %}
{%- if not benum.get_use_flags() %}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr({% if benum.get_can_unsigned() -%} u32 {%- else -%} i32 {%- endif %})]
pub enum {{ benum.get_enum_name() }} {
{%- for entry in benum.iter_entries() %}
{%- if entry.get_entry_comment() is not none %}
/// {{ entry.get_entry_comment() | line_comment }}
{%- endif %}
{{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ utils.to_py_num_literal(entry.get_entry_value()) }} {%- endif %},
{%- endfor %}
}
{%- else %}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct {{ benum.get_enum_name() }}({% if benum.get_can_unsigned() -%} u32 {%- else -%} i32 {%- endif %});
bitflags! {
impl {{ benum.get_enum_name() }}: {% if benum.get_can_unsigned() -%} u32 {%- else -%} i32 {%- endif %} {
{%- for entry in benum.iter_entries() %}
{%- if entry.get_entry_comment() is not none %}
/// {{ entry.get_entry_comment() | line_comment }}
{%- endif %}
const {{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ utils.to_py_num_literal(entry.get_entry_value()) }} {%- endif %};
{%- endfor %}
}
}
{%- endif %}
{%- endfor %}