I have a .toml config file containing some key bindings expressed as chars, I need to deserialize that file to a struct where each field is a crossterm::event::KeyCode. I'm using toml crate to parse the string. What I thought is that maybe there is a way to deserialize the string parsing keys as char and then mapping them to KeyCode.
config.toml:
key_0 = 'x'
key_bindings.rs:
use crossterm::event::KeyCode;
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct KeyBindings {
pub key_0: KeyCode,
}
How can I deserialize the config.toml file to the KeyBindings struct?
To my knowledge, there's neither a crate to do this parsing from a string to a crossterm key, nor even a normalized format.
You'll also have to deal with specifics of both Crossterm and your application. It especially depends on how you want to use the key. For example, if you want to parameterize a mapping from an event to an action, you may want to transform the key a little so that it matches what Crossterm may produce.
You might use a function like this one:
source: keys.rs in broot