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
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.

//! Hex encoding and decoding functions.

#[macro_use]
extern crate wedpr_l_macros;

use wedpr_l_utils::{error::WedprError, traits::Coder};

/// Implements Hex as a Coder instance.
#[derive(Default, Debug, Clone)]
pub struct WedprHex {}

impl Coder for WedprHex {
    fn encode<T: ?Sized + AsRef<[u8]>>(&self, input: &T) -> String {
        hex::encode(input)
    }

    fn decode(&self, input: &str) -> Result<Vec<u8>, WedprError> {
        match hex::decode(input) {
            Ok(v) => return Ok(v),
            Err(_) => {
                wedpr_println!("Hex decoding failed, input was: {}", input);
                return Err(WedprError::DecodeError);
            },
        };
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hex() {
        let hex = WedprHex::default();
        let str = "5c74d17c6a";
        // let str_bytes = "5c74d17c6a".as_bytes();
        // let str_re = String::from_utf8(str_bytes.to_vec());
        let bytes = hex.decode(&str).unwrap();
        // println!("bytes = {:?}", )
        let recovered_str = hex.encode(&bytes);
        assert_eq!(str, recovered_str);
    }
}