Merge pull request #49 from k0kubun/derive-deserialize

Switch to #[derive(Deserialize)]
This commit is contained in:
Takashi Kokubun 2021-12-29 01:09:36 -08:00 committed by GitHub
commit f0782c7899
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}
}