Skip to main content

const_hex/
serde.rs

1//! Hex encoding with [`serde`](serde_core).
2//!
3//! # Examples
4//!
5//! ```
6//! # #[cfg(feature = "alloc")] {
7//! use serde::{Serialize, Deserialize};
8//!
9//! #[derive(Serialize, Deserialize)]
10//! struct Foo {
11//!     #[serde(with = "const_hex")]
12//!     bar: Vec<u8>,
13//! }
14//! # }
15//! ```
16
17use crate::FromHex;
18use core::fmt;
19use core::marker::PhantomData;
20use serde_core::de::{Error, Visitor};
21use serde_core::Deserializer;
22
23/// Serializes `data` as hex string using lowercase characters with a `0x` prefix.
24///
25/// Lowercase characters are used (e.g. `f9b4ca`). The resulting string's length
26/// is always even, each byte in data is always encoded using two hex digits.
27/// Thus, the resulting string contains exactly twice as many bytes as the input
28/// data plus two (for the prefix).
29#[inline]
30pub fn serialize<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
31where
32    S: serde_core::Serializer,
33    T: AsRef<[u8]>,
34{
35    serializer.collect_str(&format_args!("{:#}", crate::display(data)))
36}
37
38/// Serializes `data` as hex string using uppercase characters.
39///
40/// Apart from the characters' casing, this works exactly like [`serialize`].
41#[inline]
42pub fn serialize_upper<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
43where
44    S: serde_core::Serializer,
45    T: AsRef<[u8]>,
46{
47    serializer.collect_str(&format_args!("{:#X}", crate::display(data)))
48}
49
50/// Deserializes a hex string into raw bytes.
51///
52/// Both, upper and lower case characters are valid in the input string and can
53/// even be mixed (e.g. `f9b4ca`, `F9B4CA` and `f9B4Ca` are all valid strings).
54#[inline]
55pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
56where
57    D: Deserializer<'de>,
58    T: FromHex,
59    <T as FromHex>::Error: fmt::Display,
60{
61    struct HexStrVisitor<T>(PhantomData<T>);
62
63    impl<T> Visitor<'_> for HexStrVisitor<T>
64    where
65        T: FromHex,
66        <T as FromHex>::Error: fmt::Display,
67    {
68        type Value = T;
69
70        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71            f.write_str("a hex encoded string")
72        }
73
74        fn visit_bytes<E: Error>(self, data: &[u8]) -> Result<Self::Value, E> {
75            FromHex::from_hex(data).map_err(Error::custom)
76        }
77
78        fn visit_str<E: Error>(self, data: &str) -> Result<Self::Value, E> {
79            FromHex::from_hex(data.as_bytes()).map_err(Error::custom)
80        }
81    }
82
83    deserializer.deserialize_str(HexStrVisitor(PhantomData))
84}
85
86/// Hex encoding with [`serde`](serde_core).
87///
88/// # Examples
89///
90/// ```
91/// # #[cfg(feature = "alloc")] {
92/// use serde::{Serialize, Deserialize};
93///
94/// #[derive(Serialize, Deserialize)]
95/// struct Foo {
96///     #[serde(with = "const_hex::serde::no_prefix")]
97///     bar: Vec<u8>,
98/// }
99/// # }
100/// ```
101pub mod no_prefix {
102    /// Serializes `data` as hex string using lowercase characters.
103    ///
104    /// Lowercase characters are used (e.g. `f9b4ca`). The resulting string's length
105    /// is always even, each byte in data is always encoded using two hex digits.
106    /// Thus, the resulting string contains exactly twice as many bytes as the input
107    /// data.
108    #[inline]
109    pub fn serialize<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
110    where
111        S: serde_core::Serializer,
112        T: AsRef<[u8]>,
113    {
114        serializer.collect_str(&crate::display(data))
115    }
116
117    /// Serializes `data` as hex string using uppercase characters.
118    ///
119    /// Apart from the characters' casing, this works exactly like [`serialize`].
120    #[inline]
121    pub fn serialize_upper<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
122    where
123        S: serde_core::Serializer,
124        T: AsRef<[u8]>,
125    {
126        serializer.collect_str(&format_args!("{:X}", crate::display(data)))
127    }
128
129    pub use super::deserialize;
130}