json_rpc_types/
utils.rs

1use serde::{Deserialize, Deserializer};
2use serde::de::{Error, Visitor};
3
4use core::fmt;
5
6pub enum Key {
7    JsonRpc,
8    Result,
9    Error,
10    Id,
11}
12
13struct KeyVisitor;
14
15impl<'a> Visitor<'a> for KeyVisitor {
16    type Value = Key;
17
18    #[inline]
19    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
20        formatter.write_str("Key must be a string and one of the following values: ['jsonrpc', 'result', 'error', 'id']")
21    }
22
23    #[inline]
24    fn visit_str<E: Error>(self, text: &str) -> Result<Self::Value, E> {
25        if text.eq_ignore_ascii_case("jsonrpc") {
26            Ok(Key::JsonRpc)
27        } else if text.eq_ignore_ascii_case("result") {
28            Ok(Key::Result)
29        } else if text.eq_ignore_ascii_case("error") {
30            Ok(Key::Error)
31        } else if text.eq_ignore_ascii_case("id") {
32            Ok(Key::Id)
33        } else {
34            Err(Error::invalid_value(serde::de::Unexpected::Str(text), &self))
35        }
36    }
37}
38
39
40impl<'a> Deserialize<'a> for Key {
41    #[inline]
42    fn deserialize<D: Deserializer<'a>>(des: D) -> Result<Self, D::Error> {
43        des.deserialize_str(KeyVisitor)
44    }
45}