use alloc::{boxed::Box, format, string::String, vec::Vec};
use crate::{strip_prefix, Error, FromHexPrefixed, ToHexPrefixed};
impl FromHexPrefixed for Vec<u8> {
fn from_hex_prefixed(hex: impl AsRef<str>) -> Result<Self, Error> {
let hex = strip_prefix(hex.as_ref())?;
hex::decode(hex).map_err(Into::into)
}
}
impl FromHexPrefixed for Box<[u8]> {
fn from_hex_prefixed(hex: impl AsRef<str>) -> Result<Self, Error> {
let hex = strip_prefix(hex.as_ref())?;
hex::decode(hex).map(Vec::into_boxed_slice).map_err(Into::into)
}
}
impl<const N: usize> FromHexPrefixed for [u8; N]
where
Self: hex::FromHex,
{
fn from_hex_prefixed(hex: impl AsRef<str>) -> Result<Self, Error> {
let hex = strip_prefix(hex.as_ref())?;
let mut buffer = [0; N];
hex::decode_to_slice(hex, &mut buffer).map_err(|e| match e {
hex::FromHexError::InvalidStringLength | hex::FromHexError::OddLength => Error::InvalidStringLengthSlice {
expected: N * 2,
actual: hex.len(),
},
_ => e.into(),
})?;
Ok(buffer)
}
}
impl<const N: usize> ToHexPrefixed for [u8; N]
where
Self: hex::ToHex,
{
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}
impl<const N: usize> ToHexPrefixed for &[u8; N]
where
[u8; N]: hex::ToHex,
{
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}
macro_rules! impl_to_hex {
($type:ty) => {
impl ToHexPrefixed for $type {
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}
};
}
impl_to_hex!(Box<[u8]>);
impl_to_hex!(&Box<[u8]>);
impl_to_hex!(&[u8]);
impl_to_hex!(Vec<u8>);
impl_to_hex!(&Vec<u8>);