Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always Box Structs and Enums to allow for recursive types in XSDs #165

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions xsd-parser/src/generator/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait EnumGenerator {
indent = gen.base().indent()
);

format!(
let str = format!(
"{comment}{macros}\n\
pub enum {name} {{\n\
{cases}\n\
Expand All @@ -36,7 +36,14 @@ pub trait EnumGenerator {
default = default_case,
subtypes = self.subtypes(entity, gen),
validation = self.validation(entity, gen),
)
);

let mut str2 = format!("impl YaSerialize for Box<{name}> {{\n");
str2 += format!("\tfn serialize<W: Write>(&self, writer: &mut Serializer<W>) -> Result<(), String> {{\n\t\tself.as_ref().serialize(writer)\n}}\n").as_str();
str2 += format!("\tfn serialize_attributes(&self, attributes: Vec<OwnedAttribute>, namespace: Namespace) -> Result<(Vec<OwnedAttribute>, Namespace), String> {{\n\t\tself.as_ref().serialize_attributes(attributes, namespace)\n}}\n}}\n\n").as_str();
let mut str3 = format!("impl YaDeserialize for Box<{name}> {{\n\tfn deserialize<R: Read>(reader: &mut Deserializer<R>) -> Result<Self, String> {{\n");
str3 += format!("\t\tOk(Box::new({name}::deserialize(reader)?))\n}}\n}}\n\n").as_str();
str + str2.as_str() + str3.as_str()
}

fn cases(&self, entity: &Enum, gen: &Generator) -> String {
Expand Down
11 changes: 9 additions & 2 deletions xsd-parser/src/generator/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ use crate::{

pub trait StructGenerator {
fn generate(&self, entity: &Struct, gen: &Generator) -> String {
format!(
let str = format!(
"{comment}{macros}pub struct {name} {{{fields}}}\n\n{validation}\n{subtypes}\n",
comment = self.format_comment(entity, gen),
macros = self.macros(entity, gen),
name = self.get_type_name(entity, gen),
fields = self.fields(entity, gen),
subtypes = self.subtypes(entity, gen),
validation = self.validation(entity, gen),
)
);
let name = self.get_type_name(entity, gen);
let mut str2 = format!("impl YaSerialize for Box<{name}> {{\n");
str2 += format!("\tfn serialize<W: Write>(&self, writer: &mut Serializer<W>) -> Result<(), String> {{\n\t\tself.as_ref().serialize(writer)\n}}\n").as_str();
str2 += format!("\tfn serialize_attributes(&self, attributes: Vec<OwnedAttribute>, namespace: Namespace) -> Result<(Vec<OwnedAttribute>, Namespace), String> {{\n\t\tself.as_ref().serialize_attributes(attributes, namespace)\n}}\n}}\n\n").as_str();
let mut str3 = format!("impl YaDeserialize for Box<{name}> {{\n\tfn deserialize<R: Read>(reader: &mut Deserializer<R>) -> Result<Self, String> {{\n");
str3 += format!("\t\tOk(Box::new({name}::deserialize(reader)?))\n}}\n}}\n\n").as_str();
str + str2.as_str() + str3.as_str()
}

fn fields(&self, entity: &Struct, gen: &Generator) -> String {
Expand Down
12 changes: 9 additions & 3 deletions xsd-parser/src/generator/struct_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ pub trait StructFieldGenerator {
}

fn get_type_name(&self, entity: &StructField, gen: &Generator) -> String {
gen.base()
let type_name = gen.base().format_type_name(entity.type_name.as_str(), gen).to_string();
let mut type_name: String = gen.base()
.modify_type(
gen.base().format_type_name(entity.type_name.as_str(), gen).as_ref(),
&type_name,
&entity.type_modifiers,
)
.into()
.into();
if type_name != "f64" && !type_name.contains("String") && !type_name.starts_with("Vec<") {
type_name = format!("Box<{type_name}>");
}
type_name = type_name.replace("Box<Option<", "Option<Box<");
type_name
}

fn get_name(&self, entity: &StructField, gen: &Generator) -> String {
Expand Down