Skip to content

Commit 1b58a03

Browse files
committed
Add .into() for optional fields in Rust template
Introduces a new macro to append .into() for optional fields and updates the derivative attribute logic to use it. This ensures correct type conversion for fields that are not required.
1 parent 6ac22a9 commit 1b58a03

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

templates/rust.jinja

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@
9191
{%- endif -%}
9292
{% endmacro %}
9393

94+
{# Adds .into() to optional fields #}
95+
{% macro into(attr)%}
96+
{%- if not attr.required -%}
97+
.into()
98+
{%- endif -%}
99+
{% endmacro %}
100+
94101
{# Determines the appropriate derivative attribute for a field #}
95102
{% macro get_derivative_attribute(attr) %}
96103
{%- if attr.default and attr.dtypes | length == 1 -%}
97104
{%- if attr.dtypes[0] == "string" -%}
98105
#[derivative(Default(value = "\"{{ attr.default }}\".to_string()"))]
99106
{%- else -%}
100-
#[derivative(Default(value = "{{ attr.default }}{{- to_string(attr) -}}.into()"))]
107+
#[derivative(Default(value = "{{ attr.default }}{{- to_string(attr) -}}{{- into(attr) -}}"))]
101108
{%- endif -%}
102109
{%- else -%}
103110
#[derivative(Default)]

0 commit comments

Comments
 (0)