whisky_common/utils/
parser.rs

1use crate::errors::WError;
2
3pub fn bytes_to_hex(bytes: &[u8]) -> String {
4    hex::encode(bytes)
5}
6
7pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, WError> {
8    hex::decode(hex).map_err(|err| WError::new("hex_to_bytes", err.to_string().as_str()))
9}
10
11pub fn string_to_hex(s: &str) -> String {
12    hex::encode(s)
13}
14
15pub fn hex_to_string(hex: &str) -> Result<String, WError> {
16    let bytes = hex::decode(hex).map_err(|err| {
17        WError::new(
18            "hex_to_string",
19            &format!("Invalid hex string found: {}", err),
20        )
21    })?;
22    Ok(std::str::from_utf8(&bytes)
23        .map_err(|err| {
24            WError::new(
25                "hex_to_string",
26                &format!("Invalid bytes for utf-8 found: {}", err),
27            )
28        })?
29        .to_string())
30}