[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:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c3e4443667ab7d0c55f881901b6683ea
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:

https://kaitai.io

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

2 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.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=45aa56b9f8256e69aea01e6685a1ace2

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

Everything is already working.

2 Likes