serde-xml-rs icon indicating copy to clipboard operation
serde-xml-rs copied to clipboard

I am unable to map some parts of vk.xml to serde

Open MaikKlein opened this issue 6 years ago • 9 comments

From the vulkan spec:

    <types comment="Vulkan type definitions">
        <type name="vk_platform" category="include">#include "vk_platform.h"</type>

            <comment>WSI extensions</comment>
        <type category="include">#include "<name>vulkan.h</name>"</type>
        <type category="include">#include &lt;<name>X11/Xlib.h</name>&gt;</type>
        <type category="include">#include &lt;<name>X11/extensions/Xrandr.h</name>&gt;</type>
        <type category="include">#include &lt;<name>android/native_window.h</name>&gt;</type>
        <type category="include">#include &lt;<name>mir_toolkit/client_types.h</name>&gt;</type>
        <type category="include">#include &lt;<name>wayland-client.h</name>&gt;</type>
        <type category="include">#include &lt;<name>windows.h</name>&gt;</type>
        <type category="include">#include &lt;<name>xcb/xcb.h</name>&gt;</type>

        <type requires="X11/Xlib.h" name="Display"/>
        <type requires="X11/Xlib.h" name="VisualID"/>
        <type requires="X11/Xlib.h" name="Window"/>
        <type requires="X11/extensions/Xrandr.h" name="RROutput"/>
        <type requires="android/native_window.h" name="ANativeWindow"/>
        <type requires="mir_toolkit/client_types.h" name="MirConnection"/>
        <type requires="mir_toolkit/client_types.h" name="MirSurface"/>
        <type requires="wayland-client.h" name="wl_display"/>
        <type requires="wayland-client.h" name="wl_surface"/>
        <type requires="windows.h" name="HINSTANCE"/>
        <type requires="windows.h" name="HWND"/>
        <type requires="windows.h" name="HANDLE"/>
        <type requires="windows.h" name="SECURITY_ATTRIBUTES"/>
        <type requires="windows.h" name="DWORD"/>
        <type requires="windows.h" name="LPCWSTR"/>
        <type requires="xcb/xcb.h" name="xcb_connection_t"/>
        <type requires="xcb/xcb.h" name="xcb_visualid_t"/>
        <type requires="xcb/xcb.h" name="xcb_window_t"/>

I have no idea how this would map to serde. I almost believe that this can not be expressed?

I am really confused, any help is greatly appreciated.

MaikKlein avatar Sep 16 '17 14:09 MaikKlein

I guess I can express is sort of like this

#[derive(Debug, Serialize, Deserialize)]
pub struct Types{
    #[serde(rename = "$value")]
    pub types: Vec<Type>,
    pub comment: String

}

#[derive(Debug, Serialize, Deserialize)]
pub enum Type{
    #[serde(rename = "type")]
    Type{
        category: Option<String>,
        name: String,
        requires: Option<String>,
    },
    #[serde(rename = "comment")]
    Comment(String)
}

But then I am missing the content in between <type> ... </type>.

Later I also get nested type.

        <type category="basetype">typedef <type>uint32_t</type> <name>VkSampleMask</name>;</type>
        <type category="basetype">typedef <type>uint32_t</type> <name>VkBool32</name>;</type>
        <type category="basetype">typedef <type>uint32_t</type> <name>VkFlags</name>;</type>
        <type category="basetype">typedef <type>uint64_t</type> <name>VkDeviceSize</name>;</type>

I assume I have to deserialize it manually?

MaikKlein avatar Sep 16 '17 14:09 MaikKlein

Wow, what a coincidence. I'm also trying to write vulkan bindings generated from vk.xml and just ran into this problem.

terrybrash avatar Sep 18 '17 00:09 terrybrash

@MaikKlein Did you try other representations from https://serde.rs/enum-representations.html ? I would think in the worst case untagged should work (but one of tagged probably too).

RReverser avatar Sep 18 '17 18:09 RReverser

In the simplest form, the type of xml that seemingly cannot be deserialized by serde-xml-rs looks like:

<sentence>Lorem <bold>ipsum</bold> dolor sit amet</sentence>

I thought this might work:

#[derive(Deserialize, Debug)]
pub struct Sentence {
    #[derive(rename="$value")]
    elements: Vec<SentenceElement>,
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum SentenceElement {
    Text(String),
    Bold {
        bold: String,
    },
}

But it errors with: Custom("missing field 'elements'")

I also tried various other ways without success.

terrybrash avatar Sep 18 '17 19:09 terrybrash

Also, @MaikKlein, I found that other people had similar issues parsing vk.xml (along with some other gripes). So, somebody created an application to convert vk.xml into something saner: https://github.com/NicolBolas/New-Vulkan-XML-Format

It looks pretty good at first glance, and should be easily parsable by serde-xml-rs.

terrybrash avatar Sep 18 '17 19:09 terrybrash

@RReverser I actually haven't, I think that could be sufficient. But I am not sure if you can still parse things inside tags and between tags. <foo inside=""> between </foo>

@terrybrashaw We probably should join forces if we both want to just parse the vk.xml (or something similar). I need it for ash.

MaikKlein avatar Sep 19 '17 14:09 MaikKlein

@MaikKlein Sure, I'll message you on Reddit so we can coordinate.

terrybrash avatar Sep 19 '17 16:09 terrybrash

Was there any progress on this? I'm trying to parse something very similar, looks like this:

<obj file="byxml.dm:1">inner_text<obj file="byxml.dm:4">other_inner_text</obj></obj>

mloc avatar Oct 23 '17 10:10 mloc

But it errors with: Custom("missing field 'elements'")

that sounds like a bug. We should never see the name elements in any errors if there is a rename attribute. If that issue is fixed, maybe your setup works?

oli-obk avatar Oct 23 '17 10:10 oli-obk