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
#[macro_use]
extern crate wedpr_l_macros;
use wedpr_l_utils::{error::WedprError, traits::Coder};
#[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 bytes = hex.decode(&str).unwrap();
let recovered_str = hex.encode(&bytes);
assert_eq!(str, recovered_str);
}
}