Skip to main content

tycho_common/
serde_primitives.rs

1use std::{collections::HashMap, fmt, hash::Hash, marker::PhantomData};
2
3use hex::FromHexError;
4use serde::{de, Deserialize, Deserializer};
5
6use crate::Bytes;
7
8/// Decodes a hex string with an optional `0x` prefix. An odd number of digits has an implied
9/// leading zero.
10fn decode_hex_with_prefix(val: &str) -> Result<Vec<u8>, FromHexError> {
11    let digits = val
12        .strip_prefix("0x")
13        .unwrap_or(val)
14        .as_bytes();
15    let mut out = vec![0u8; digits.len().div_ceil(2)];
16    if digits.len().is_multiple_of(2) {
17        hex::decode_to_slice(digits, &mut out)?;
18    } else if let (Some((first, rest)), Some((first_out, rest_out))) =
19        (digits.split_first(), out.split_first_mut())
20    {
21        hex::decode_to_slice([b'0', *first], std::slice::from_mut(first_out))?;
22        hex::decode_to_slice(rest, rest_out)?;
23    }
24    Ok(out)
25}
26
27/// Bytes decoded from a hex string. The string is decoded where the deserializer holds it, without
28/// copying it into a `String` first.
29struct HexValue(Vec<u8>);
30
31impl<'de> Deserialize<'de> for HexValue {
32    fn deserialize<D>(d: D) -> Result<Self, D::Error>
33    where
34        D: Deserializer<'de>,
35    {
36        struct HexVisitor;
37
38        impl de::Visitor<'_> for HexVisitor {
39            type Value = HexValue;
40
41            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
42                f.write_str("a hex string")
43            }
44
45            fn visit_str<E: de::Error>(self, value: &str) -> Result<HexValue, E> {
46                decode_hex_with_prefix(value)
47                    .map(HexValue)
48                    .map_err(|e| E::custom(e.to_string()))
49            }
50        }
51
52        d.deserialize_str(HexVisitor)
53    }
54}
55
56impl From<HexValue> for Bytes {
57    fn from(value: HexValue) -> Self {
58        Bytes::from(value.0)
59    }
60}
61
62/// Deserializes a map straight into its final types: each entry is read as `(KR, VR)` and
63/// converted, with no intermediate map.
64fn deserialize_map<'de, D, KR, VR, K, V>(d: D) -> Result<HashMap<K, V>, D::Error>
65where
66    D: Deserializer<'de>,
67    KR: Deserialize<'de> + Into<K>,
68    VR: Deserialize<'de> + Into<V>,
69    K: Eq + Hash,
70{
71    struct MapVisitor<KR, VR, K, V>(PhantomData<(KR, VR, K, V)>);
72
73    impl<'de, KR, VR, K, V> de::Visitor<'de> for MapVisitor<KR, VR, K, V>
74    where
75        KR: Deserialize<'de> + Into<K>,
76        VR: Deserialize<'de> + Into<V>,
77        K: Eq + Hash,
78    {
79        type Value = HashMap<K, V>;
80
81        fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
82            f.write_str("a map")
83        }
84
85        fn visit_map<A: de::MapAccess<'de>>(self, mut access: A) -> Result<Self::Value, A::Error> {
86            let mut map = HashMap::with_capacity(access.size_hint().unwrap_or(0));
87            while let Some((key, value)) = access.next_entry::<KR, VR>()? {
88                map.insert(key.into(), value.into());
89            }
90            Ok(map)
91        }
92    }
93
94    d.deserialize_map(MapVisitor::<KR, VR, K, V>(PhantomData))
95}
96
97/// A buffer for writing bytes as `0x`-prefixed lowercase hex.
98///
99/// Reusing one buffer for many values allocates only when a value is longer than every value
100/// before it.
101#[derive(Default)]
102struct HexBuffer(Vec<u8>);
103
104impl HexBuffer {
105    /// Returns `bytes` as `0x` followed by two lowercase hex digits per byte. Empty input gives
106    /// `"0x"`. The string borrows the buffer and is valid until the next call.
107    ///
108    /// # Errors
109    ///
110    /// Returns a serializer error if the hex string length overflows `usize`.
111    fn encode<E: serde::ser::Error>(&mut self, bytes: &[u8]) -> Result<&str, E> {
112        let hex_len = bytes
113            .len()
114            .checked_mul(2)
115            .and_then(|digits| digits.checked_add(2))
116            .ok_or_else(|| {
117                E::custom(format!("cannot hex encode {} bytes: length overflows", bytes.len()))
118            })?;
119        self.0.clear();
120        self.0.extend_from_slice(b"0x");
121        self.0.resize(hex_len, 0);
122        let digits = self
123            .0
124            .get_mut(2..)
125            .ok_or_else(|| E::custom("hex buffer is shorter than its prefix"))?;
126        hex::encode_to_slice(bytes, digits).map_err(E::custom)?;
127        std::str::from_utf8(&self.0).map_err(E::custom)
128    }
129}
130
131/// serde functions for handling bytes as hex strings, such as [bytes::Bytes]
132pub mod hex_bytes {
133    use serde::{Deserialize, Deserializer, Serializer};
134
135    use super::{HexBuffer, HexValue};
136
137    /// Serialize a byte vec as a hex string with 0x prefix
138    pub fn serialize<S, T>(x: T, s: S) -> Result<S::Ok, S::Error>
139    where
140        S: Serializer,
141        T: AsRef<[u8]>,
142    {
143        s.serialize_str(HexBuffer::default().encode(x.as_ref())?)
144    }
145
146    /// Deserialize a hex string into a byte vec
147    /// Accepts a hex string with optional 0x prefix
148    pub fn deserialize<'de, T, D>(d: D) -> Result<T, D::Error>
149    where
150        D: Deserializer<'de>,
151        T: From<Vec<u8>>,
152    {
153        HexValue::deserialize(d).map(|value| value.0.into())
154    }
155}
156
157/// serde functions for handling Option of bytes
158pub mod hex_bytes_option {
159    use serde::{Deserialize, Deserializer, Serializer};
160
161    use super::{HexBuffer, HexValue};
162
163    /// Serialize a byte vec as a Some hex string with 0x prefix
164    pub fn serialize<S, T>(x: &Option<T>, s: S) -> Result<S::Ok, S::Error>
165    where
166        S: Serializer,
167        T: AsRef<[u8]>,
168    {
169        if let Some(x) = x {
170            s.serialize_str(HexBuffer::default().encode(x.as_ref())?)
171        } else {
172            s.serialize_none()
173        }
174    }
175
176    /// Deserialize a hex string into a byte vec or None
177    /// Accepts a hex string with optional 0x prefix
178    pub fn deserialize<'de, T, D>(d: D) -> Result<Option<T>, D::Error>
179    where
180        D: Deserializer<'de>,
181        T: From<Vec<u8>>,
182    {
183        let value: Option<HexValue> = Option::deserialize(d)?;
184        Ok(value.map(|value| value.0.into()))
185    }
186}
187
188/// serde functions for handling HashMap with a bytes key
189pub mod hex_hashmap_key {
190    use std::collections::HashMap;
191
192    use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
193
194    use super::{deserialize_map, HexBuffer, HexValue};
195    use crate::Bytes;
196
197    pub fn serialize<S, V>(x: &HashMap<Bytes, V>, s: S) -> Result<S::Ok, S::Error>
198    where
199        S: Serializer,
200        V: Serialize,
201    {
202        let mut key_hex = HexBuffer::default();
203        let mut map = s.serialize_map(Some(x.len()))?;
204        for (k, v) in x.iter() {
205            map.serialize_entry(key_hex.encode(k)?, v)?;
206        }
207        map.end()
208    }
209
210    pub fn deserialize<'de, V, D>(d: D) -> Result<HashMap<Bytes, V>, D::Error>
211    where
212        D: Deserializer<'de>,
213        V: Deserialize<'de>,
214    {
215        deserialize_map::<D, HexValue, V, Bytes, V>(d)
216    }
217}
218
219/// serde functions for handling Vec of Bytes as hex strings
220pub mod hex_bytes_vec {
221    use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serializer};
222
223    use super::{HexBuffer, HexValue};
224
225    /// Serialize Vec<Vec<u8>> as a list of hex strings with 0x prefix
226    pub fn serialize<S>(list: &[Vec<u8>], s: S) -> Result<S::Ok, S::Error>
227    where
228        S: Serializer,
229    {
230        let mut hex = HexBuffer::default();
231        let mut seq = s.serialize_seq(Some(list.len()))?;
232        for x in list {
233            seq.serialize_element(hex.encode(x)?)?;
234        }
235        seq.end()
236    }
237
238    /// Deserialize a list of hex strings into Vec<Vec<u8>>
239    pub fn deserialize<'de, D>(d: D) -> Result<Vec<Vec<u8>>, D::Error>
240    where
241        D: Deserializer<'de>,
242    {
243        let values = Vec::<HexValue>::deserialize(d)?;
244        Ok(values
245            .into_iter()
246            .map(|value| value.0)
247            .collect())
248    }
249}
250
251/// The length of an iterator when it is known, as a map serializer wants it.
252fn exact_len<I: Iterator>(iter: &I) -> Option<usize> {
253    match iter.size_hint() {
254        (low, Some(high)) if low == high => Some(low),
255        _ => None,
256    }
257}
258
259/// serde functions for handling HashMap with bytes value
260pub mod hex_hashmap_value {
261    use std::collections::HashMap;
262
263    use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
264
265    use super::{deserialize_map, HexBuffer, HexValue};
266    use crate::Bytes;
267
268    pub fn serialize<S, K>(x: &HashMap<K, Bytes>, s: S) -> Result<S::Ok, S::Error>
269    where
270        S: Serializer,
271        K: Serialize,
272    {
273        serialize_iter(x, s)
274    }
275
276    /// Writes the pairs as one map. Needs no `HashMap`, so a caller can write borrowed pairs.
277    /// With an iterator whose length is not exact, the map length is `None`, which formats that
278    /// need a length reject.
279    pub fn serialize_iter<'a, S, K, I>(pairs: I, s: S) -> Result<S::Ok, S::Error>
280    where
281        S: Serializer,
282        K: Serialize + 'a,
283        I: IntoIterator<Item = (&'a K, &'a Bytes)>,
284    {
285        let pairs = pairs.into_iter();
286        let mut value_hex = HexBuffer::default();
287        let mut map = s.serialize_map(super::exact_len(&pairs))?;
288        for (k, v) in pairs {
289            map.serialize_entry(k, value_hex.encode(v)?)?;
290        }
291        map.end()
292    }
293
294    pub fn deserialize<'de, K, D>(d: D) -> Result<HashMap<K, Bytes>, D::Error>
295    where
296        D: Deserializer<'de>,
297        K: Deserialize<'de> + Eq + std::hash::Hash, // HashMap key trait bounds
298    {
299        deserialize_map::<D, K, HexValue, K, Bytes>(d)
300    }
301}
302
303/// serde functions for handling HashMap with a bytes key and value
304pub mod hex_hashmap_key_value {
305    use std::collections::HashMap;
306
307    use serde::{ser::SerializeMap, Deserializer, Serializer};
308
309    use super::{deserialize_map, HexBuffer, HexValue};
310    use crate::Bytes;
311
312    pub fn serialize<S>(x: &HashMap<Bytes, Bytes>, s: S) -> Result<S::Ok, S::Error>
313    where
314        S: Serializer,
315    {
316        serialize_iter(x, s)
317    }
318
319    /// Writes the pairs as one map. Needs no `HashMap`, so a caller can write borrowed pairs.
320    /// With an iterator whose length is not exact, the map length is `None`, which formats that
321    /// need a length reject.
322    pub fn serialize_iter<'a, S, I>(pairs: I, s: S) -> Result<S::Ok, S::Error>
323    where
324        S: Serializer,
325        I: IntoIterator<Item = (&'a Bytes, &'a Bytes)>,
326    {
327        let pairs = pairs.into_iter();
328        let (mut key_hex, mut value_hex) = (HexBuffer::default(), HexBuffer::default());
329        let mut map = s.serialize_map(super::exact_len(&pairs))?;
330        for (k, v) in pairs {
331            map.serialize_entry(key_hex.encode(k)?, value_hex.encode(v)?)?;
332        }
333        map.end()
334    }
335
336    pub fn deserialize<'de, D>(d: D) -> Result<HashMap<Bytes, Bytes>, D::Error>
337    where
338        D: Deserializer<'de>,
339    {
340        deserialize_map::<D, HexValue, HexValue, Bytes, Bytes>(d)
341    }
342}
343
344#[cfg(test)]
345mod tests {
346    use std::collections::HashMap;
347
348    use serde::{Deserialize, Serialize};
349
350    use super::*;
351    use crate::Bytes;
352
353    #[derive(Debug, Serialize, Deserialize)]
354    struct TestStruct {
355        #[serde(with = "hex_bytes")]
356        bytes: Vec<u8>,
357
358        #[serde(with = "hex_bytes_option")]
359        bytes_option: Option<Vec<u8>>,
360
361        #[serde(with = "hex_bytes_vec")]
362        bytes_vec: Vec<Vec<u8>>,
363    }
364
365    #[derive(Serialize)]
366    struct IterMaps {
367        #[serde(serialize_with = "key_value_via_iter")]
368        key_value: HashMap<Bytes, Bytes>,
369        #[serde(serialize_with = "value_via_iter")]
370        value: HashMap<String, Bytes>,
371    }
372
373    #[derive(Serialize)]
374    struct DirectMaps {
375        #[serde(with = "hex_hashmap_key_value")]
376        key_value: HashMap<Bytes, Bytes>,
377        #[serde(with = "hex_hashmap_value")]
378        value: HashMap<String, Bytes>,
379    }
380
381    fn key_value_via_iter<S: serde::Serializer>(
382        x: &HashMap<Bytes, Bytes>,
383        s: S,
384    ) -> Result<S::Ok, S::Error> {
385        hex_hashmap_key_value::serialize_iter(x.iter().filter(|_| true), s)
386    }
387
388    fn value_via_iter<S: serde::Serializer>(
389        x: &HashMap<String, Bytes>,
390        s: S,
391    ) -> Result<S::Ok, S::Error> {
392        hex_hashmap_value::serialize_iter(x.iter().filter(|_| true), s)
393    }
394
395    #[test]
396    fn hex_hashmap_serialize_iter_matches_serialize() {
397        let key_value = HashMap::from([(Bytes::from(vec![1, 2]), Bytes::from(vec![0xAB]))]);
398        let value = HashMap::from([("a".to_string(), Bytes::from(vec![0xFF, 0x00]))]);
399
400        let direct = serde_json::to_string(&DirectMaps {
401            key_value: key_value.clone(),
402            value: value.clone(),
403        })
404        .unwrap();
405        let via_iter = serde_json::to_string(&IterMaps { key_value, value }).unwrap();
406
407        assert_eq!(via_iter, direct);
408        assert_eq!(direct, r#"{"key_value":{"0x0102":"0xab"},"value":{"a":"0xff00"}}"#);
409    }
410
411    #[test]
412    fn hex_bytes_serialize_deserialize() {
413        let test_struct = TestStruct {
414            bytes: vec![0u8; 10],
415            bytes_option: Some(vec![0u8; 10]),
416            bytes_vec: vec![vec![0, 1, 2, 3], vec![0xFF, 0xAB]],
417        };
418
419        // Serialize to JSON
420        let serialized = serde_json::to_string(&test_struct).unwrap();
421        assert_eq!(
422            serialized,
423            "{\"bytes\":\"0x00000000000000000000\",\"bytes_option\":\"0x00000000000000000000\",\"bytes_vec\":[\"0x00010203\",\"0xffab\"]}"
424        );
425
426        // Deserialize from JSON
427        let deserialized: TestStruct = serde_json::from_str(&serialized).unwrap();
428        assert_eq!(deserialized.bytes, vec![0u8; 10]);
429        assert_eq!(deserialized.bytes_option, Some(vec![0u8; 10]));
430        assert_eq!(deserialized.bytes_vec, vec![vec![0, 1, 2, 3], vec![0xFF, 0xAB]]);
431    }
432
433    #[test]
434    fn hex_bytes_option_none() {
435        let test_struct =
436            TestStruct { bytes: vec![0u8; 10], bytes_option: None, bytes_vec: vec![] };
437
438        // Serialize to JSON
439        let serialized = serde_json::to_string(&test_struct).unwrap();
440        assert_eq!(
441            serialized,
442            "{\"bytes\":\"0x00000000000000000000\",\"bytes_option\":null,\"bytes_vec\":[]}"
443        );
444
445        // Deserialize from JSON
446        let deserialized: TestStruct = serde_json::from_str(&serialized).unwrap();
447        assert_eq!(deserialized.bytes, vec![0u8; 10]);
448        assert_eq!(deserialized.bytes_option, None);
449    }
450
451    /// Serializers that format every value with `format!`: the expected output for the ones above.
452    mod reference {
453        use std::collections::HashMap;
454
455        use serde::{ser::SerializeMap, Serialize, Serializer};
456
457        use crate::Bytes;
458
459        pub fn hex(x: &[u8]) -> String {
460            format!("0x{}", hex::encode(x))
461        }
462
463        pub fn hex_bytes<S: Serializer>(x: &[u8], s: S) -> Result<S::Ok, S::Error> {
464            s.serialize_str(&hex(x))
465        }
466
467        pub fn hex_bytes_option<S: Serializer>(
468            x: &Option<Vec<u8>>,
469            s: S,
470        ) -> Result<S::Ok, S::Error> {
471            match x {
472                Some(x) => s.serialize_str(&hex(x)),
473                None => s.serialize_none(),
474            }
475        }
476
477        pub fn hex_bytes_vec<S: Serializer>(list: &[Vec<u8>], s: S) -> Result<S::Ok, S::Error> {
478            list.iter()
479                .map(|x| hex(x))
480                .collect::<Vec<_>>()
481                .serialize(s)
482        }
483
484        pub fn hex_hashmap_key<S: Serializer>(
485            x: &HashMap<Bytes, Bytes>,
486            s: S,
487        ) -> Result<S::Ok, S::Error> {
488            let mut map = s.serialize_map(Some(x.len()))?;
489            for (k, v) in x.iter() {
490                map.serialize_entry(&format!("{k:#x}"), v)?;
491            }
492            map.end()
493        }
494
495        pub fn hex_hashmap_value<S: Serializer>(
496            x: &HashMap<Bytes, Bytes>,
497            s: S,
498        ) -> Result<S::Ok, S::Error> {
499            let mut map = s.serialize_map(Some(x.len()))?;
500            for (k, v) in x.iter() {
501                map.serialize_entry(k, &format!("{v:#x}"))?;
502            }
503            map.end()
504        }
505
506        pub fn hex_hashmap_key_value<S: Serializer>(
507            x: &HashMap<Bytes, Bytes>,
508            s: S,
509        ) -> Result<S::Ok, S::Error> {
510            let mut map = s.serialize_map(Some(x.len()))?;
511            for (k, v) in x.iter() {
512                map.serialize_entry(&format!("{k:#x}"), &format!("{v:#x}"))?;
513            }
514            map.end()
515        }
516
517        /// Decodes by copying the digits and prepending a zero to an odd count.
518        pub fn decode(val: &str) -> Result<Vec<u8>, hex::FromHexError> {
519            let mut digits: String = val
520                .strip_prefix("0x")
521                .unwrap_or(val)
522                .into();
523            if !digits.len().is_multiple_of(2) {
524                digits.insert(0, '0');
525            }
526            hex::decode(&digits)
527        }
528    }
529
530    #[derive(Serialize)]
531    struct CurrentBytes<'a> {
532        #[serde(with = "hex_bytes")]
533        bytes: &'a Vec<u8>,
534        #[serde(with = "hex_bytes_option")]
535        bytes_option: &'a Option<Vec<u8>>,
536        #[serde(with = "hex_bytes_vec")]
537        bytes_vec: &'a Vec<Vec<u8>>,
538    }
539
540    #[derive(Serialize)]
541    struct ReferenceBytes<'a> {
542        #[serde(serialize_with = "reference::hex_bytes")]
543        bytes: &'a Vec<u8>,
544        #[serde(serialize_with = "reference::hex_bytes_option")]
545        bytes_option: &'a Option<Vec<u8>>,
546        #[serde(serialize_with = "reference::hex_bytes_vec")]
547        bytes_vec: &'a Vec<Vec<u8>>,
548    }
549
550    #[derive(Serialize)]
551    struct CurrentMaps<'a> {
552        #[serde(with = "hex_hashmap_key")]
553        key: &'a HashMap<Bytes, Bytes>,
554        #[serde(with = "hex_hashmap_value")]
555        value: &'a HashMap<Bytes, Bytes>,
556        #[serde(with = "hex_hashmap_key_value")]
557        key_value: &'a HashMap<Bytes, Bytes>,
558    }
559
560    #[derive(Serialize)]
561    struct ReferenceMaps<'a> {
562        #[serde(serialize_with = "reference::hex_hashmap_key")]
563        key: &'a HashMap<Bytes, Bytes>,
564        #[serde(serialize_with = "reference::hex_hashmap_value")]
565        value: &'a HashMap<Bytes, Bytes>,
566        #[serde(serialize_with = "reference::hex_hashmap_key_value")]
567        key_value: &'a HashMap<Bytes, Bytes>,
568    }
569
570    /// Empty, single bytes, leading zeros, a full word, and values longer than any slot: the
571    /// largest contract code, odd lengths and 128 KiB.
572    fn hex_cases() -> Vec<Vec<u8>> {
573        let long = |len: usize| {
574            (0..len)
575                .map(|i| (i * 31 % 256) as u8)
576                .collect()
577        };
578        vec![
579            vec![],
580            vec![0],
581            vec![0x0a],
582            vec![0xff],
583            vec![0, 0, 1],
584            vec![0xff; 32],
585            (0..32).collect(),
586            long(33),
587            long(24_576),
588            long(24_577),
589            long((1 << 17) + 3),
590        ]
591    }
592
593    #[test]
594    fn hex_bytes_serializers_write_the_same_json_as_formatting_each_value() {
595        let cases = hex_cases();
596        for value in &cases {
597            let option = Some(value.clone());
598            let current = CurrentBytes { bytes: value, bytes_option: &option, bytes_vec: &cases };
599            let reference =
600                ReferenceBytes { bytes: value, bytes_option: &option, bytes_vec: &cases };
601
602            assert!(
603                serde_json::to_vec(&current).unwrap() == serde_json::to_vec(&reference).unwrap(),
604                "output differs for a {}-byte value",
605                value.len()
606            );
607        }
608    }
609
610    #[test]
611    fn hex_hashmap_serializers_write_the_same_json_as_formatting_each_entry() {
612        let cases = hex_cases();
613        // Each key is a case followed by its index and a value prefix, so that keys are distinct
614        // and short and long keys sit next to short and long values.
615        let mut map = HashMap::new();
616        for (i, key) in cases.iter().enumerate() {
617            for value in &cases {
618                let mut key = key.clone();
619                key.push(i as u8);
620                key.extend_from_slice(&value[..value.len().min(40)]);
621                map.insert(Bytes::from(key), Bytes::from(value.clone()));
622            }
623        }
624
625        let current = CurrentMaps { key: &map, value: &map, key_value: &map };
626        let reference = ReferenceMaps { key: &map, value: &map, key_value: &map };
627
628        assert!(serde_json::to_vec(&current).unwrap() == serde_json::to_vec(&reference).unwrap());
629    }
630
631    #[test]
632    fn hex_serializers_round_trip_long_values() {
633        let test_struct = TestStruct {
634            bytes: hex_cases().pop().unwrap(),
635            bytes_option: Some(vec![]),
636            bytes_vec: hex_cases(),
637        };
638
639        let serialized = serde_json::to_string(&test_struct).unwrap();
640        let deserialized: TestStruct = serde_json::from_str(&serialized).unwrap();
641
642        assert_eq!(deserialized.bytes, test_struct.bytes);
643        assert_eq!(deserialized.bytes_option, Some(vec![]));
644        assert_eq!(deserialized.bytes_vec, test_struct.bytes_vec);
645    }
646
647    /// Every case as lowercase and uppercase digits, with and without the prefix, and with an odd
648    /// number of digits where the first one is a zero.
649    fn hex_strings() -> Vec<String> {
650        let mut strings = vec![];
651        for value in hex_cases() {
652            let digits = hex::encode(&value);
653            for digits in [digits.clone(), digits.to_uppercase()] {
654                strings.push(format!("0x{digits}"));
655                strings.push(digits.clone());
656                if let Some(odd) = digits.strip_prefix('0') {
657                    strings.push(format!("0x{odd}"));
658                    strings.push(odd.to_string());
659                }
660            }
661        }
662        strings
663    }
664
665    #[test]
666    fn decode_hex_with_prefix_reads_the_same_bytes_as_the_reference_decoder() {
667        for string in hex_strings() {
668            assert_eq!(
669                decode_hex_with_prefix(&string).unwrap(),
670                reference::decode(&string).unwrap(),
671                "differs for {}",
672                &string[..string.len().min(20)]
673            );
674        }
675    }
676
677    #[test]
678    fn decode_hex_with_prefix_rejects_what_the_reference_decoder_rejects() {
679        for string in ["0xzz", "0x1g", "0xg", "g", "0x0x", "é", "0xé", "0x0é", "é0", "0x é", "0x-1"]
680        {
681            assert!(reference::decode(string).is_err(), "reference accepts {string}");
682            assert!(decode_hex_with_prefix(string).is_err(), "accepts {string}");
683        }
684    }
685
686    #[derive(Debug, PartialEq, Deserialize)]
687    struct DecodedMaps {
688        #[serde(with = "hex_hashmap_key")]
689        key: HashMap<Bytes, u64>,
690        #[serde(with = "hex_hashmap_value")]
691        value: HashMap<String, Bytes>,
692        #[serde(with = "hex_hashmap_key_value")]
693        key_value: HashMap<Bytes, Bytes>,
694    }
695
696    #[test]
697    fn hex_deserializers_read_borrowed_escaped_and_owned_strings() {
698        // "\u0030x0a" is "0x0a" with an escaped first character: the deserializer can't lend it
699        // from the input.
700        let json = r#"{
701            "key": {"0x0a": 1, "\u0030x0b": 2},
702            "value": {"a": "0x0c", "b": "\u0030x0d"},
703            "key_value": {"0x01": "0x0002", "3": "0xff", "\u0030x04": "0x"}
704        }"#;
705        let expected = DecodedMaps {
706            key: HashMap::from([(Bytes::from(vec![0x0a]), 1), (Bytes::from(vec![0x0b]), 2)]),
707            value: HashMap::from([
708                ("a".to_string(), Bytes::from(vec![0x0c])),
709                ("b".to_string(), Bytes::from(vec![0x0d])),
710            ]),
711            key_value: HashMap::from([
712                (Bytes::from(vec![0x01]), Bytes::from(vec![0x00, 0x02])),
713                (Bytes::from(vec![0x03]), Bytes::from(vec![0xff])),
714                (Bytes::from(vec![0x04]), Bytes::from(vec![])),
715            ]),
716        };
717
718        let from_str: DecodedMaps = serde_json::from_str(json).unwrap();
719        let from_reader: DecodedMaps = serde_json::from_reader(json.as_bytes()).unwrap();
720        let value: serde_json::Value = serde_json::from_str(json).unwrap();
721        let from_value: DecodedMaps = serde_json::from_value(value).unwrap();
722
723        assert_eq!(from_str, expected);
724        assert_eq!(from_reader, expected);
725        assert_eq!(from_value, expected);
726    }
727
728    #[test]
729    fn hex_deserializers_reject_invalid_hex_and_non_strings() {
730        let invalid = [
731            r#"{"key": {"0xzz": 1}, "value": {}, "key_value": {}}"#,
732            r#"{"key": {}, "value": {"a": "0x1g"}, "key_value": {}}"#,
733            r#"{"key": {}, "value": {}, "key_value": {"0x01": "é"}}"#,
734            r#"{"key": {}, "value": {"a": 1}, "key_value": {}}"#,
735        ];
736        for json in invalid {
737            assert!(serde_json::from_str::<DecodedMaps>(json).is_err(), "accepts {json}");
738        }
739        assert!(serde_json::from_str::<TestStruct>(
740            r#"{"bytes": 1, "bytes_option": null, "bytes_vec": []}"#
741        )
742        .is_err());
743    }
744}