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::Property(k1), Key::Property(k2)) => k1 == k2,
90            (Key::Property(k1), Key::Borrowed(k2)) => k1.to_cow() == *k2,
91            (Key::Property(k1), Key::Owned(k2)) => k1.to_cow() == k2.as_str(),
92            (Key::Owned(k1), Key::Owned(k2)) => k1 == k2,
93            (Key::Owned(k1), Key::Borrowed(k2)) => k1 == k2,
94            (Key::Owned(k1), Key::Property(k2)) => k1.as_str() == k2.to_cow(),
95            (Key::Borrowed(k1), Key::Borrowed(k2)) => k1 == k2,
96            (Key::Borrowed(k1), Key::Owned(k2)) => k1 == k2,
97            (Key::Borrowed(k1), Key::Property(k2)) => *k1 == k2.to_cow(),
98        }
99    }
100}
101
102impl<P: Property> Eq for Key<'_, P> {}
103
104impl<P: Property> PartialEq<&str> for Key<'_, P> {
105    fn eq(&self, other: &&str) -> bool {
106        self.to_string() == *other
107    }
108}
109
110impl<'x, P: Property> From<&'x str> for Key<'x, P> {
111    fn from(s: &'x str) -> Self {
112        match P::try_parse(None, s) {
113            Some(word) => Key::Property(word),
114            None => Key::Borrowed(s),
115        }
116    }
117}
118
119impl<'x, P: Property> From<Key<'x, P>> for Cow<'x, str> {
120    fn from(s: Key<'x, P>) -> Self {
121        match s {
122            Key::Borrowed(s) => Cow::Borrowed(s),
123            Key::Owned(s) => Cow::Owned(s),
124            Key::Property(word) => word.to_cow(),
125        }
126    }
127}
128
129impl<'x, P: Property> From<Cow<'x, str>> for Key<'x, P> {
130    fn from(s: Cow<'x, str>) -> Self {
131        match s {
132            Cow::Borrowed(s) => Key::Borrowed(s),
133            Cow::Owned(s) => Key::Owned(s),
134        }
135    }
136}
137
138impl<P: Property> Key<'_, P> {
139    pub fn to_string(&self) -> Cow<'_, str> {
140        match self {
141            Key::Borrowed(s) => Cow::Borrowed(s),
142            Key::Owned(s) => Cow::Borrowed(s.as_str()),
143            Key::Property(word) => word.to_cow(),
144        }
145    }
146
147    pub fn into_string(self) -> String {
148        match self {
149            Key::Borrowed(s) => s.to_owned(),
150            Key::Owned(s) => s,
151            Key::Property(word) => word.to_cow().into_owned(),
152        }
153    }
154
155    pub fn try_into_property(self) -> Option<P> {
156        match self {
157            Key::Property(word) => Some(word),
158            _ => None,
159        }
160    }
161
162    pub fn into_owned(self) -> Key<'static, P> {
163        match self {
164            Key::Borrowed(s) => Key::Owned(s.to_owned()),
165            Key::Owned(s) => Key::Owned(s),
166            Key::Property(word) => Key::Property(word),
167        }
168    }
169
170    pub fn to_owned(&self) -> Key<'static, P> {
171        match self {
172            Key::Borrowed(s) => Key::Owned(s.to_string()),
173            Key::Owned(s) => Key::Owned(s.clone()),
174            Key::Property(word) => Key::Property(word.clone()),
175        }
176    }
177
178    pub fn as_property(&self) -> Option<&P> {
179        match self {
180            Key::Property(word) => Some(word),
181            _ => None,
182        }
183    }
184
185    pub fn as_string_key(&self) -> Option<&str> {
186        match self {
187            Key::Borrowed(s) => Some(s),
188            Key::Owned(s) => Some(s.as_str()),
189            Key::Property(_) => None,
190        }
191    }
192}
193
194impl<'x, P: Property> From<P> for Key<'x, P> {
195    fn from(word: P) -> Self {
196        Key::Property(word)
197    }
198}