-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removes generating separate struct for nested sequence/scalar types
* Modified Rust templates to consider `util_macros` when reading or writing a nested sequence type
- Loading branch information
Showing
3 changed files
with
75 additions
and
8 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
47 changes: 47 additions & 0 deletions
47
src/bin/ion/commands/beta/generate/templates/rust/util_macros.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{# following macro defines statements to read a class field as sequence #} | ||
{% macro read_as_sequence(field) %} | ||
{% if field.abstract_data_type["Sequence"].sequence_type == "List" %} | ||
if reader.ion_type() != Some(IonType::List) { | ||
return validation_error(format!( | ||
"Expected list, found {} while reading {{ field.name | snake }}.", reader.ion_type().unwrap() | ||
)); | ||
} | ||
{% elif field.abstract_data_type["Sequence"].sequence_type == "SExp" %} | ||
if reader.ion_type() != Some(IonType::SExp) { | ||
return validation_error(format!( | ||
"Expected sexpression, found {} while reading {{ field.name | snake }}.", reader.ion_type().unwrap() | ||
)); | ||
} | ||
{% endif %} | ||
reader.step_in()?; | ||
|
||
abstract_data_type.{{ field.name | snake }} = { | ||
let mut values = vec![]; | ||
|
||
while reader.next()? != StreamItem::Nothing { | ||
{% if field.abstract_data_type["Sequence"].element_type | is_built_in_type == false %} | ||
values.push({{ field.abstract_data_type["Sequence"].element_type }}::read_from(reader)?); | ||
{% else %} | ||
values.push(reader.read_{% if field.isl_type_name == "symbol" %}symbol()?.text().unwrap(){% else %}{{ field.abstract_data_type["Sequence"].element_type | lower | replace(from="string", to ="str") }}()?{% endif %}{% if field.abstract_data_type["Sequence"].element_type | lower== "string" %} .to_string() {% endif %}); | ||
{% endif %} | ||
} | ||
values | ||
}; | ||
reader.step_out()?; | ||
{% endmacro %} | ||
{# following macro defines statements to write a class field as sequence #} | ||
{% macro write_as_sequence(field) %} | ||
{% if field.abstract_data_type["Sequence"].sequence_type == "List" %} | ||
writer.step_in(IonType::List)?; | ||
{% else %} | ||
writer.step_in(IonType::SExp)?; | ||
{% endif %} | ||
for value in &self.{{ field.name | snake }} { | ||
{% if field.abstract_data_type["Sequence"].element_type | is_built_in_type == false %} | ||
value.write_to(writer)?; | ||
{% else %} | ||
writer.write_{% if field.isl_type_name == "symbol" %}symbol{% else %}{{ field.abstract_data_type["Sequence"].element_type | lower }}{% endif %}(value.to_owned())?; | ||
{% endif %} | ||
} | ||
writer.step_out()?; | ||
{% endmacro %} |