#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#[cfg(feature = "alloc")]
extern crate alloc;
pub mod buf_encoder;
pub mod display;
mod error;
mod iter;
pub mod parse;
pub use display::DisplayHex;
pub use iter::{BytesToHexIter, HexToBytesIter};
pub use parse::{FromHex, HexToArrayError, HexToBytesError};
pub mod exts {
pub use super::display::DisplayHex;
pub use super::parse::FromHex;
}
pub(crate) mod prelude {
#[cfg(feature = "alloc")]
pub(crate) use alloc::string::String;
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Case {
Lower,
Upper,
}
impl Default for Case {
fn default() -> Self { Case::Lower }
}
impl Case {
#[inline]
#[rustfmt::skip]
pub(crate) fn table(self) -> &'static [u8; 16] {
static LOWER: [u8; 16] = [b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b', b'c', b'd', b'e', b'f'];
static UPPER: [u8; 16] = [b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E', b'F'];
match self {
Case::Lower => &LOWER,
Case::Upper => &UPPER,
}
}
}
#[inline]
pub(crate) fn byte_to_hex(byte: u8, table: &[u8; 16]) -> [u8; 2] {
[table[usize::from(byte.wrapping_shr(4))], table[usize::from(byte & 0x0F)]]
}
#[macro_export]
macro_rules! test_hex_unwrap (($hex:expr) => (<Vec<u8> as $crate::FromHex>::from_hex($hex).unwrap()));
#[cfg(test)]
mod tests {
use crate::test_hex_unwrap as hex;
#[test]
fn parse_hex_into_vector() {
let got = hex!("deadbeef");
let want = vec![0xde, 0xad, 0xbe, 0xef];
assert_eq!(got, want)
}
}