mirror of
https://github.com/k0kubun/xremap
synced 2024-11-02 03:40:25 +00:00
Merge pull request #49 from k0kubun/derive-deserialize
Switch to #[derive(Deserialize)]
This commit is contained in:
commit
f0782c7899
@ -1,10 +1,9 @@
|
||||
use crate::config::key::Key;
|
||||
use serde::de::{IntoDeserializer, MapAccess, Visitor};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use std::fmt::Formatter;
|
||||
use serde::Deserialize;
|
||||
|
||||
// Values in `modmap.remap`
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum KeyAction {
|
||||
Key(Key),
|
||||
MultiPurposeKey(MultiPurposeKey),
|
||||
@ -23,38 +22,3 @@ impl MultiPurposeKey {
|
||||
1000
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for KeyAction {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct ActionVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for ActionVisitor {
|
||||
type Value = KeyAction;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
|
||||
formatter.write_str("string or map")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
let key = Deserialize::deserialize(value.into_deserializer())?;
|
||||
Ok(KeyAction::Key(key))
|
||||
}
|
||||
|
||||
fn visit_map<M>(self, map: M) -> Result<Self::Value, M::Error>
|
||||
where
|
||||
M: MapAccess<'de>,
|
||||
{
|
||||
let multi_purpose_key = Deserialize::deserialize(serde::de::value::MapAccessDeserializer::new(map))?;
|
||||
Ok(KeyAction::MultiPurposeKey(multi_purpose_key))
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_any(ActionVisitor)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user