Resurrect deserializer for Actions

pull/51/head
Takashi Kokubun 2 years ago
parent 00e4a07f43
commit 12906a45df
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD

@ -1,6 +1,4 @@
modmap:
- remap:
Space:
held: Shift_L
alone: Space
alone_timeout_millis: 500
keymap:
- name: Global
remap:
RO: Shift-RO

@ -1,7 +1,7 @@
use crate::config::action::Action;
use crate::config::application::Application;
use crate::config::key_press::KeyPress;
use serde::de::{MapAccess, Visitor};
use serde::de::{value, IntoDeserializer, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer};
use std::collections::HashMap;
use std::fmt;
@ -23,12 +23,6 @@ where
{
struct RemapVisitor;
#[derive(Deserialize)]
enum Actions {
Action(Action),
Actions(Vec<Action>),
}
impl<'de> Visitor<'de> for RemapVisitor {
type Value = HashMap<KeyPress, Vec<Action>>;
@ -56,3 +50,51 @@ where
deserializer.deserialize_any(RemapVisitor)
}
enum Actions {
Action(Action),
Actions(Vec<Action>),
}
impl<'de> Deserialize<'de> for Actions {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ActionsVisitor;
impl<'de> Visitor<'de> for ActionsVisitor {
type Value = Actions;
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
formatter.write_str("strings or maps")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let key_press = Deserialize::deserialize(value.into_deserializer())?;
Ok(Actions::Action(Action::KeyPress(key_press)))
}
fn visit_seq<S>(self, seq: S) -> Result<Self::Value, S::Error>
where
S: SeqAccess<'de>,
{
let actions = Deserialize::deserialize(value::SeqAccessDeserializer::new(seq))?;
Ok(Actions::Actions(actions))
}
fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let action = Deserialize::deserialize(value::MapAccessDeserializer::new(map))?;
Ok(Actions::Action(action))
}
}
deserializer.deserialize_any(ActionsVisitor)
}
}

Loading…
Cancel
Save