jmap_tools/json/
key.rs

1/*
2 * SPDX-FileCopyrightText: 2021 Pascal Seitz <pascal.seitz@gmail.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use crate::json::value::Property;
8use serde::de::{self, DeserializeSeed, Visitor};
9use serde::{Serialize, Serializer};
10use std::borrow::Cow;
11use std::fmt;
12
13#[derive(Debug, Clone, PartialOrd, Ord, Hash)]
14pub enum Key<'x, P: Property> {
15    Property(P),
16    Borrowed(&'x str),
17    Owned(String),
18}
19
20pub(crate) struct DeserializationContext<'x, P: Property> {
21    pub parent_key: Option<&'x Key<'x, P>>,
22}
23
24impl<'de, 'x, P: Property> DeserializeSeed<'de> for DeserializationContext<'x, P> {
25    type Value = Key<'de, P>;
26
27    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
28    where
29        D: serde::Deserializer<'de>,
30    {
31        deserializer.deserialize_any(KeyVisitor { context: &self })
32    }
33}
34
35struct KeyVisitor<'x, P: Property> {
36    context: &'x DeserializationContext<'x, P>,
37}
38
39impl<'de, 'x, P: Property> Visitor<'de> for KeyVisitor<'x, P> {
40    type Value = Key<'de, P>;
41
42    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
43        formatter.write_str("a string")
44    }
45
46    fn visit_borrowed_str<ERR>(self, value: &'de str) -> Result<Self::Value, ERR>
47    where
48        ERR: de::Error,
49    {
50        match P::try_parse(self.context.parent_key, value) {
51            Some(word) => Ok(Key::Property(word)),
52            None => Ok(Key::Borrowed(value)),
53        }
54    }
55
56    fn visit_str<ERR>(self, value: &str) -> Result<Self::Value, ERR>
57    where
58        ERR: de::Error,
59    {
60        match P::try_parse(self.context.parent_key, value) {
61            Some(word) => Ok(Key::Property(word)),
62            None => Ok(Key::Owned(value.to_owned())),
63        }
64    }
65
66    fn visit_string<ERR>(self, value: String) -> Result<Self::Value, ERR>
67    where
68        ERR: de::Error,
69    {
70        match P::try_parse(self.context.parent_key, &value) {
71            Some(word) => Ok(Key::Property(word)),
72            None => Ok(Key::Owned(value)),
73        }
74    }
75}
76
77impl<P: Property> Serialize for Key<'_, P> {
78    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
79    where
80        S: Serializer,
81    {
82        serializer.serialize_str(self.to_string().as_ref())
83    }
84}
85
86impl<P: Property> PartialEq for Key<'_, P> {
87    fn eq(&self, other: &Self) -> bool {
88        match (self, other) {
89            (Key::Borrowed(s1), Key::Borrowed(s2)) => s1 == s2,
90            (Key::Owned(s1), Key::Owned(s2)) => s1 == s2,
91            (Key::Property(w1), Key::Property(w2)) => w1 == w2,
92            _ => self.to_string() == other.to_string(),
93        }
94    }
95}
96
97impl<P: Property> Eq for Key<'_, P> {}
98
99impl<P: Property> PartialEq<&str> for Key<'_, P> {
100    fn eq(&self, other: &&str) -> bool {
101        self.to_string() == *other
102    }
103}
104
105impl<'x, P: Property> From<&'x str> for Key<'x, P> {
106    fn from(s: &'x str) -> Self {
107        match P::try_parse(None, s) {
108            Some(word) => Key::Property(word),
109            None => Key::Borrowed(s),
110        }
111    }
112}
113
114impl<'x, P: Property> From<Key<'x, P>> for Cow<'x, str> {
115    fn from(s: Key<'x, P>) -> Self {
116        match s {
117            Key::Borrowed(s) => Cow::Borrowed(s),
118            Key::Owned(s) => Cow::Owned(s),
119            Key::Property(word) => word.to_cow(),
120        }
121    }
122}
123
124impl<'x, P: Property> From<Cow<'x, str>> for Key<'x, P> {
125    fn from(s: Cow<'x, str>) -> Self {
126        match s {
127            Cow::Borrowed(s) => Key::Borrowed(s),
128            Cow::Owned(s) => Key::Owned(s),
129        }
130    }
131}
132
133impl<P: Property> Key<'_, P> {
134    pub fn to_string(&self) -> Cow<'_, str> {
135        match self {
136            Key::Borrowed(s) => Cow::Borrowed(s),
137            Key::Owned(s) => Cow::Borrowed(s.as_str()),
138            Key::Property(word) => word.to_cow(),
139        }
140    }
141
142    pub fn into_string(self) -> String {
143        match self {
144            Key::Borrowed(s) => s.to_owned(),
145            Key::Owned(s) => s,
146            Key::Property(word) => word.to_cow().into_owned(),
147        }
148    }
149
150    pub fn try_into_property(self) -> Option<P> {
151        match self {
152            Key::Property(word) => Some(word),
153            _ => None,
154        }
155    }
156
157    pub fn into_owned(self) -> Key<'static, P> {
158        match self {
159            Key::Borrowed(s) => Key::Owned(s.to_owned()),
160            Key::Owned(s) => Key::Owned(s),
161            Key::Property(word) => Key::Property(word),
162        }
163    }
164}
165
166impl<'x, P: Property> From<P> for Key<'x, P> {
167    fn from(word: P) -> Self {
168        Key::Property(word)
169    }
170}