[Serde-rs] How to deserialize YAML to HashMap<String, Option<String>>?

Hello!
I have a YAML:

description: Events frame
frame_header:
  PREAMBLE
  FRAME_TYPE: FRAME_TYPE_EVENTS
  DEV_TYPE: DEV_TYPE_DI
  ADDRESS

and struct

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct MyStruct
{
    description: String,
    frame_header: HashMap<String, Option<String>>
}

How to convert yaml to MyStruct?
Please help me.

Rust Playground - Click Me!

Edit:

How to do without null?

My Target:

MyStruct { description: "Events frame", frame_header: {"PREAMBLE": None, "FRAME_TYPE": Some("FRAME_TYPE_EVENTS"), "DEV_TYPE": Some("DEV_TYPE_DI"), "ADDRESS": None} }

I'm trying to do a similar thing to:

This is not a valid YAML document. The colon is still necessary even if the value is missing. Replace PREAMBLE with PREAMBLE: etc, which is identical with PREAMBLE: null.

https://yaml.org/spec/1.2/spec.html

3 Likes

Thanks for reply.

I have a better idea. I will use the reference.
I need to describe a communication protocol using YAML. Its my goal.

The code you've commented out compiles and runs well. What's the question?

Everything is already working.

2 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.