keyfork/crates/util/smex/src/lib.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

use std::fmt::Write;
#[derive(Debug)]
pub enum DecodeError {
InvalidCharacter(u8),
InvalidCharacterCount(usize),
}
impl std::fmt::Display for DecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidCharacter(c) => {
write!(f, "Invalid character: {c} not in [0123456789ABCDEF]")
}
Self::InvalidCharacterCount(n) => {
write!(f, "Invalid character count: {n} % 2 != 0")
}
}
}
}
impl std::error::Error for DecodeError {}
pub fn encode(input: &[u8]) -> String {
let mut s = String::new();
for byte in input {
write!(s, "{byte:02x}").unwrap();
}
s
}
fn val(c: u8) -> Result<u8, DecodeError> {
match c {
b'A'..=b'F' => Ok(c - b'A' + 10),
b'a'..=b'f' => Ok(c - b'a' + 10),
b'0'..=b'9' => Ok(c - b'0'),
_ => Err(DecodeError::InvalidCharacter(c)),
}
}
pub fn decode(input: &str) -> Result<Vec<u8>, DecodeError> {
let len = input.len();
if len % 2 != 0 {
return Err(DecodeError::InvalidCharacterCount(len));
}
input
.as_bytes()
.chunks_exact(2)
.map(|pair| Ok(val(pair[0])? << 4 | val(pair[1])?))
.collect()
}