-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new model changes for Rust code generation (#147)
* Adds changes for Rust templates * Add `string_representation` on `FullyQualifiedTypeReference` which will have namespace represented based on programming language * Add tests for Rust code generation
- Loading branch information
Showing
10 changed files
with
271 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 5 additions & 130 deletions
135
src/bin/ion/commands/generate/templates/rust/nested_type.templ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,8 @@ | ||
{% import "util_macros.templ" as util_macros %} | ||
|
||
{# following macro defines an anonymous type as children class for its parent type definition #} | ||
{% macro nested_type(target_kind_name, fields, abstract_data_type, nested_anonymous_types) -%} | ||
#[derive(Debug, Clone, Default)] | ||
pub struct {{ target_kind_name }} { | ||
{% for field in fields -%} | ||
{{ field.name | snake | indent(first = true) }}: {{ field.value_type }}, | ||
{% endfor %} | ||
} | ||
|
||
impl {{ target_kind_name }} { | ||
pub fn new({% for field in fields | sort(attribute="name") -%}{{ field.name | snake }}: {{ field.value_type }},{% endfor %}) -> Self { | ||
Self { | ||
{% for field in fields -%} | ||
{{ field.name | snake }}, | ||
{% endfor %} | ||
} | ||
} | ||
|
||
|
||
{% for field in fields -%}pub fn {{ field.name | snake }}(&self) -> &{{ field.value_type }} { | ||
&self.{{ field.name | snake }} | ||
} | ||
{% endfor %} | ||
|
||
|
||
pub fn read_from(reader: &mut Reader) -> SerdeResult<Self> { | ||
let mut abstract_data_type = {{ target_kind_name }}::default(); | ||
{% if abstract_data_type == "Value"%} | ||
abstract_data_type.value = {% if fields[0].value_type | is_built_in_type == false %} | ||
{{ fields[0].value_type }}::read_from(reader)?; | ||
{% else %} | ||
reader.read_{% if fields[0].isl_type_name == "symbol" %}symbol()?.text().unwrap(){% else %}{{ fields[0].value_type | lower | replace(from="string", to ="str") }}()?{% endif %}{% if fields[0].value_type | lower == "string" %} .to_string() {% endif %}; | ||
{% endif %} | ||
{% elif abstract_data_type is object and abstract_data_type is containing("Structure") %} | ||
reader.step_in()?; | ||
while reader.next()? != StreamItem::Nothing { | ||
if let Some(field_name) = reader.field_name()?.text() { | ||
match field_name { | ||
{% for field in fields -%} | ||
{% if field.value_type | is_built_in_type == false %} | ||
{% if field.value_type is containing("Vec") %} | ||
"{{ field.name }}" => { {{ util_macros::read_as_sequence(field=field) }} } | ||
{% else %} | ||
"{{ field.name }}" => { abstract_data_type.{{ field.name | snake }} = {{ field.value_type }}::read_from(reader)?; } | ||
{% endif %} | ||
{% else %} | ||
"{{ field.name }}" => { abstract_data_type.{{ field.name | snake}} = reader.read_{% if field.isl_type_name == "symbol" %}symbol()?.text().unwrap(){% else %}{{ field.value_type | lower | replace(from="string", to ="str") }}()?{% endif %}{% if field.value_type | lower== "string" %} .to_string() {% endif %}; } | ||
{% endif %} | ||
{% endfor %} | ||
_ => { | ||
{% if abstract_data_type["Structure"] %} | ||
return validation_error( | ||
"Can not read field name:{{ field.name }} for {{ target_kind_name }} as it doesn't exist in the given schema type definition." | ||
); | ||
{% endif %} | ||
} | ||
} | ||
} | ||
} | ||
reader.step_out()?; | ||
{% elif abstract_data_type is object and abstract_data_type is containing("Sequence") %} | ||
if reader.ion_type() != Some(IonType::{{ abstract_data_type["Sequence"].sequence_type }}) { | ||
return validation_error(format!( | ||
"Expected {{ abstract_data_type["Sequence"].sequence_type }}, found {} while reading {{ target_kind_name }}.", reader.ion_type().unwrap() | ||
)); | ||
} | ||
reader.step_in()?; | ||
|
||
abstract_data_type.value = { | ||
let mut values = vec![]; | ||
|
||
while reader.next()? != StreamItem::Nothing { | ||
{% if abstract_data_type["Sequence"].element_type | is_built_in_type == false %} | ||
values.push({{ abstract_data_type["Sequence"].element_type }}::read_from(reader)?); | ||
{% else %} | ||
values.push(reader.read_{% if fields[0].isl_type_name == "symbol" %}symbol()?.text().unwrap(){% else %}{{ abstract_data_type["Sequence"].element_type | lower | replace(from="string", to ="str") }}()?{% endif %}{% if abstract_data_type["Sequence"].element_type | lower== "string" %} .to_string() {% endif %}); | ||
{% endif %} | ||
} | ||
values | ||
}; | ||
reader.step_out()?; | ||
{% else %} | ||
return validation_error("Can not resolve read API template for {{ target_kind_name }}"); | ||
{% endif %} | ||
Ok(abstract_data_type) | ||
} | ||
|
||
pub fn write_to<W: IonWriter>(&self, writer: &mut W) -> SerdeResult<()> { | ||
{% if abstract_data_type == "Value" %} | ||
{% for field in fields %} | ||
{% if field.value_type | is_built_in_type == false %} | ||
self.{{ field.name | snake }}.write_to(writer)?; | ||
{% else %} | ||
writer.write_{% if field.isl_type_name == "symbol" %}symbol{% else %}{{ field.value_type | lower }}{% endif %}(self.value.to_owned())?; | ||
{% endif %} | ||
{% endfor %} | ||
{% elif abstract_data_type is object and abstract_data_type is containing("Structure") %} | ||
writer.step_in(IonType::Struct)?; | ||
{% for field in fields %} | ||
writer.set_field_name("{{ field.name }}"); | ||
{% if field.value_type | is_built_in_type == false %} | ||
{% if field.value_type is containing("Vec") %} | ||
{{ util_macros::write_as_sequence(field=field) }} | ||
{% else %} | ||
self.{{ field.name | snake }}.write_to(writer)?; | ||
{% endif %} | ||
{% else %} | ||
{# TODO: Change the following `to_owned` to only be used when writing i64,f32,f64,bool which require owned value as input #} | ||
writer.write_{% if field.isl_type_name == "symbol" %}symbol{% else %}{{ field.value_type | lower }}{% endif %}(self.{{ field.name | snake }}.to_owned())?; | ||
{% endif %} | ||
{% endfor %} | ||
writer.step_out()?; | ||
{% elif abstract_data_type is object and abstract_data_type is containing("Sequence") %} | ||
writer.step_in(IonType::{{ abstract_data_type["Sequence"].sequence_type }})?; | ||
for value in &self.value { | ||
{% if abstract_data_type["Sequence"].element_type | is_built_in_type == false %} | ||
value.write_to(writer)?; | ||
{% else %} | ||
writer.write_{% if fields[0].isl_type_name == "symbol" %}symbol{% else %}{{ abstract_data_type["Sequence"].element_type | lower }}{% endif %}(value.to_owned())?; | ||
{% endif %} | ||
} | ||
writer.step_out()?; | ||
{% endif %} | ||
Ok(()) | ||
} | ||
} | ||
|
||
{% for inline_type in nested_anonymous_types -%} | ||
{{ self::nested_type(target_kind_name=inline_type.target_kind_name, fields=inline_type.fields, abstract_data_type=inline_type.abstract_data_type, nested_anonymous_types=inline_type.nested_types) }} | ||
{% endfor -%} | ||
{% endmacro %} | ||
{% macro nested_type(model, is_nested) -%} | ||
{% if model.code_gen_type is containing("Structure")%} | ||
{% include "struct.templ" %} | ||
{% endif %} | ||
{% endmacro nested_type -%} |
Oops, something went wrong.