use std::fmt;
use crate::error::{FormatError, Result};
pub type PublicKey = [u8; 32];
pub type Signature = [u8; 64];
macro_rules! id_type {
($(#[$doc:meta])* $name:ident, $len:literal) => {
$(#[$doc])*
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct $name(pub [u8; $len]);
impl $name {
pub const LEN: usize = $len;
pub fn to_hex(&self) -> String {
to_hex(&self.0)
}
pub fn from_hex(s: &str) -> Result<Self> {
let mut out = [0u8; $len];
from_hex(s, &mut out)?;
Ok(Self(out))
}
}
impl From<[u8; $len]> for $name {
fn from(bytes: [u8; $len]) -> Self {
Self(bytes)
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.to_hex())
}
}
impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({})", stringify!($name), self.to_hex())
}
}
};
}
id_type!(
LookupId,
32
);
id_type!(
SegmentId,
32
);
id_type!(
PackId,
16
);
id_type!(
CacheId,
16
);
id_type!(
TenantId,
16
);
id_type!(
RequestId,
16
);
const HEX: &[u8; 16] = b"0123456789abcdef";
fn to_hex(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
out.push(char::from(HEX[usize::from(b >> 4)]));
out.push(char::from(HEX[usize::from(b & 15)]));
}
out
}
fn nibble(c: u8) -> Result<u8> {
match c {
b'0'..=b'9' => Ok(c - b'0'),
b'a'..=b'f' => Ok(c - b'a' + 10),
_ => Err(FormatError::Invalid("hex digit")),
}
}
fn from_hex(s: &str, out: &mut [u8]) -> Result<()> {
let s = s.as_bytes();
if s.len() != out.len() * 2 {
return Err(FormatError::Invalid("hex length"));
}
for (i, byte) in out.iter_mut().enumerate() {
*byte = nibble(s[2 * i])? << 4 | nibble(s[2 * i + 1])?;
}
Ok(())
}