Deparsing dependig on version

Hello

I'm trying to write a library for deparsing GDTF Files in a struct. The basic operation is to deparse an xml file. I'm currently using quick_xml for that and it works fine.

Now GDTF standard evolves and in the xml is a version field (version of the gdtf-spec), so the xml will vary a little bit depending on this field. The rusty thing to do here would be to create an enum with all supported versions of GDTF. Since gdtf is quite complex, i would like to avoid to copy the whole structs for every version. I'd rather just change the nodes that have different specifications due to the version and wrap them with an enum if needed.

What would be the best way to do this?

Example:

<GDTF DataVersion="1.0">
    <SomeNode1>
        <SomeNode2>
             <SomeNodeA>Here is the definition for 1.0</SomeNodeA>
        </SomeNode2>
    </SomeNode1>
</GDTF>


<GDTF DataVersion="1.1">
    <SomeNode1>
        <SomeNode2>
             <SomeNodeB>Here is the definition for 1.1. The node name is different for example</SomeNodeB>
        </SomeNode2>
    </SomeNode1>
</GDTF>

You could use something like this to change the contents of types based on a "version" type parameter, with a single enum switch at the top level for selecting the version.

enum Gdtf {
    Version1_0(GdtfRoot<Version1_0>),
    Version1_1(GdtfRoot<Version1_1>),
}

struct GdtfRoot<V: GdtfVersion> {
    pub inner: V::GdtfInner,
}

struct SomeNodeA {
    pub content: String,
}

struct SomeNodeB {
    pub content: String,
}

trait GdtfVersion {
    type GdtfInner;
}

enum Version1_0 {}

impl GdtfVersion for Version1_0 {
    type GdtfInner = SomeNodeA;
}

enum Version1_1 {}

impl GdtfVersion for Version1_1 {
    type GdtfInner = SomeNodeB;
}

Depending on how you're parsing the XML will change how you deal with the different names. Which reminds me...

I have never heard this term before and have no idea what it means. From context, I think you mean "parsing". "de-parsing" sounds like it would involve taking a structure and turning it into text or bytes, which would normally be "serialization" or "formatting" or "printing". "parsing" is taking text or bytes and turning it into a structure.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.