use crate::identifier::Identifier;
const DERIVATION_MARKER: u8 = b'\'';
#[derive(Clone, Copy)]
pub struct EncodedEpin {
buf: [u8; 4],
len: u8,
}
impl EncodedEpin {
pub(crate) fn from_identifier(id: Identifier) -> Self {
let mut buf = [0u8; 4];
let mut len: u8 = 0;
let pin = id.pin().encode();
for &byte in pin.as_str().as_bytes() {
buf[usize::from(len)] = byte;
len += 1;
}
if id.is_derived() {
buf[usize::from(len)] = DERIVATION_MARKER;
len += 1;
}
Self { buf, len }
}
#[must_use]
pub fn as_str(&self) -> &str {
let bytes = &self.buf[..usize::from(self.len)];
debug_assert!(
bytes.is_ascii(),
"EncodedEpin must contain only ASCII bytes"
);
core::str::from_utf8(bytes).unwrap_or("")
}
}
impl core::ops::Deref for EncodedEpin {
type Target = str;
fn deref(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for EncodedEpin {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl core::fmt::Display for EncodedEpin {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
impl core::fmt::Debug for EncodedEpin {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "EncodedEpin({:?})", self.as_str())
}
}
impl PartialEq<str> for EncodedEpin {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<&str> for EncodedEpin {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<EncodedEpin> for str {
fn eq(&self, other: &EncodedEpin) -> bool {
self == other.as_str()
}
}
impl PartialEq<EncodedEpin> for &str {
fn eq(&self, other: &EncodedEpin) -> bool {
*self == other.as_str()
}
}