1use serde::de::{self, Visitor};
4use std::fmt;
5
6pub fn encode<T: AsRef<[u8]>>(data: T) -> String {
8 let hex = hex::encode(data);
9
10 let mut s = "0x".to_string();
11 s.push_str(hex.as_str());
12 s
13}
14
15pub fn decode(s: &str) -> Result<Vec<u8>, String> {
17 if let Some(stripped) = s.strip_prefix("0x") {
18 hex::decode(stripped).map_err(|e| format!("invalid hex: {:?}", e))
19 } else {
20 Err("hex must have 0x prefix".to_string())
21 }
22}
23
24pub struct PrefixedHexVisitor;
25
26impl<'de> Visitor<'de> for PrefixedHexVisitor {
27 type Value = Vec<u8>;
28
29 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
30 formatter.write_str("a hex string with 0x prefix")
31 }
32
33 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
34 where
35 E: de::Error,
36 {
37 decode(value).map_err(de::Error::custom)
38 }
39}
40
41pub struct HexVisitor;
42
43impl<'de> Visitor<'de> for HexVisitor {
44 type Value = Vec<u8>;
45
46 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
47 formatter.write_str("a hex string (irrelevant of prefix)")
48 }
49
50 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
51 where
52 E: de::Error,
53 {
54 hex::decode(value.trim_start_matches("0x"))
55 .map_err(|e| de::Error::custom(format!("invalid hex ({:?})", e)))
56 }
57}
58
59#[cfg(test)]
60mod test {
61 use super::*;
62
63 #[test]
64 fn encoding() {
65 let bytes = vec![0, 255];
66 let hex = encode(bytes);
67 assert_eq!(hex.as_str(), "0x00ff");
68
69 let bytes = vec![];
70 let hex = encode(bytes);
71 assert_eq!(hex.as_str(), "0x");
72
73 let bytes = vec![1, 2, 3];
74 let hex = encode(bytes);
75 assert_eq!(hex.as_str(), "0x010203");
76 }
77}