1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Hex serialization traits

use serde::{Deserialize, Deserializer, Serializer};
use std::str;

pub trait ToHex {
    fn to_hex(&self) -> String;
}

pub fn serialize<S, T>(this: T, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
    T: ToHex,
{
    let hex = this.to_hex();
    serializer.serialize_str(&hex)
}

pub trait FromHex: Sized {
    type Error: std::fmt::Display;
    fn from_hex(hex_str: &str) -> Result<Self, Self::Error>;
}

pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
    T: FromHex,
{
    use serde::de::Error;
    let buff: &[u8] = Deserialize::deserialize(deserializer)?;
    T::from_hex(str::from_utf8(buff).unwrap()).map_err(D::Error::custom)
}

/// Little endian format of full slice content
/// (so string lengths are always even).
impl ToHex for &[u8] {
    fn to_hex(&self) -> String {
        // an empty vector is allowed
        if self.is_empty() {
            return "".to_string();
        }

        let mut hex = vec![0u8; self.len() * 2];
        faster_hex::hex_encode(self, hex.as_mut_slice())
            .expect("The output is exactly twice the size of the input");
        let result = unsafe { str::from_utf8_unchecked(&hex) };
        result.to_string()
    }
}

/// Little endian format of full content
/// (so string lengths are always even).
impl ToHex for Vec<u8> {
    fn to_hex(&self) -> String {
        (&**self).to_hex()
    }
}

/// Little endian format of full content
/// (so string lengths must be even).
impl FromHex for Vec<u8> {
    type Error = faster_hex::Error;
    fn from_hex(hex_str: &str) -> Result<Self, Self::Error> {
        // an empty string is allowed
        if hex_str.is_empty() {
            return Ok(vec![]);
        }

        let mut bytes = vec![0u8; hex_str.len() / 2];
        faster_hex::hex_decode(hex_str.as_bytes(), bytes.as_mut_slice())?;
        Ok(bytes)
    }
}