use crate::protocol::wr;
use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct Finger {
pub uid: u16,
pub fid: u8,
pub valid: u8,
pub template: Vec<u8>,
}
impl Finger {
pub fn new(uid: u16, fid: u8, valid: u8, template: Vec<u8>) -> Self {
Finger {
uid,
fid,
valid,
template,
}
}
pub fn size(&self) -> usize {
self.template.len()
}
pub fn repack(&self) -> Vec<u8> {
wr(|w| {
w.u16((self.size() + 6) as u16)
.u16(self.uid)
.u8(self.fid)
.u8(self.valid)
.bytes(&self.template);
})
}
pub(crate) fn repack_only(&self) -> Vec<u8> {
wr(|w| {
w.u16(self.size() as u16).bytes(&self.template);
})
}
fn mark(&self) -> String {
let head = &self.template[..self.template.len().min(8)];
let tail = if self.template.len() >= 8 {
&self.template[self.template.len() - 8..]
} else {
&[][..]
};
format!("{}...{}", hex(head), hex(tail))
}
}
impl fmt::Debug for Finger {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"<Finger> [uid:{:>3}, fid:{}, size:{:>4} v:{} t:{}]",
self.uid,
self.fid,
self.size(),
self.valid,
self.mark()
)
}
}
impl fmt::Display for Finger {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
fn hex(bytes: &[u8]) -> String {
let mut s = String::with_capacity(bytes.len() * 2);
for b in bytes {
s.push_str(&format!("{b:02x}"));
}
s
}