Skip to content

Easy element construction with body #265

Open
@patrickelectric

Description

@patrickelectric

My objective is to create a XML element that has the following format:

<?xml version="1.0" encoding="UTF-8"?>
<Camera>
   <definition version="42">
      <model>Potato</model>
      <vendor>PotatoFarm</vendor>
   </definition>
</Camera>

But to accomplish that, from what I read from the examples, is necessary to create a structure like this one:

#[derive(Debug, Default, Serialize)]
struct Camera {
    definition: Definition,
}

#[derive(Debug, Default, Serialize)]
struct Definition {
    version: u32,
    model: Model,
    vendor: Vendor,
}

#[derive(Debug, Default, Serialize)]
struct Model {
    #[serde(rename = "$value")]
    body: String,
}

#[derive(Debug, Default, Serialize)]
struct Vendor {
    #[serde(rename = "$value")]
    body: String,
}

Is these struct with body element really necessary or is there a way to accomplish it with a macro or something to a simple struct like:

#[derive(Debug, Default, Serialize)]
struct Camera {
    definition: Definition,
}

#[derive(Debug, Default, Serialize)]
struct Definition {
    #[magic_macro]
    camera_info: CameraInfo,
}

#[derive(Debug, Default, Serialize)]
struct CameraInfo {
    vendor: String,
    model: String,
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions