use crate::{DeviceId, DriverId, Finger};
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub struct Minutia {
pub x: i32,
pub y: i32,
pub theta: i32,
}
impl Minutia {
#[must_use]
pub const fn from_xyt(x: i32, y: i32, theta: i32) -> Self {
Self { x, y, theta }
}
#[must_use]
pub const fn as_xyt(&self) -> (i32, i32, i32) {
(self.x, self.y, self.theta)
}
}
impl From<(i32, i32, i32)> for Minutia {
fn from((x, y, theta): (i32, i32, i32)) -> Self {
Self { x, y, theta }
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct EnrollDate {
pub year: i32,
pub month: u8,
pub day: u8,
}
impl EnrollDate {
#[must_use]
pub fn new(year: i32, month: u8, day: u8) -> Self {
EnrollDate { year, month, day }
}
pub fn try_new(year: i32, month: u8, day: u8) -> Result<Self, InvalidEnrollDate> {
if !(1..=12).contains(&month) {
return Err(InvalidEnrollDate::Month(month));
}
if !(1..=31).contains(&day) {
return Err(InvalidEnrollDate::Day(day));
}
Ok(EnrollDate { year, month, day })
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum InvalidEnrollDate {
Month(u8),
Day(u8),
}
impl core::fmt::Display for InvalidEnrollDate {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
InvalidEnrollDate::Month(m) => write!(f, "{m} is not a valid month (expected 1..=12)"),
InvalidEnrollDate::Day(d) => write!(f, "{d} is not a valid day (expected 1..=31)"),
}
}
}
impl std::error::Error for InvalidEnrollDate {}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub enum Template {
#[default]
Undefined,
Nbis(Vec<Vec<Minutia>>),
Raw(Vec<u8>),
}
impl Template {
#[must_use]
pub fn is_raw(&self) -> bool {
matches!(self, Template::Raw(_))
}
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct Print {
pub template: Template,
pub finger: Option<Finger>,
pub username: Option<String>,
pub description: Option<String>,
pub driver: Option<DriverId>,
pub device_id: Option<DeviceId>,
pub device_stored: bool,
pub enroll_date: Option<EnrollDate>,
}
impl Print {
#[must_use]
pub fn new_for_enroll(finger: Finger) -> Print {
Print {
finger: Some(finger),
..Print::default()
}
}
#[must_use]
pub fn builder() -> PrintBuilder {
PrintBuilder::default()
}
#[must_use]
pub fn is_compatible_with_driver(&self, driver: &DriverId) -> bool {
match &self.driver {
Some(d) => d == driver,
None => true, }
}
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct PrintBuilder {
template: Template,
finger: Option<Finger>,
username: Option<String>,
description: Option<String>,
driver: Option<DriverId>,
device_id: Option<DeviceId>,
device_stored: bool,
enroll_date: Option<EnrollDate>,
}
impl PrintBuilder {
#[must_use]
pub fn template(mut self, template: Template) -> Self {
self.template = template;
self
}
#[must_use]
pub fn finger(mut self, finger: impl Into<Option<Finger>>) -> Self {
self.finger = finger.into();
self
}
#[must_use]
pub fn username(mut self, username: impl Into<Option<String>>) -> Self {
self.username = username.into();
self
}
#[must_use]
pub fn description(mut self, description: impl Into<Option<String>>) -> Self {
self.description = description.into();
self
}
#[must_use]
pub fn driver(mut self, driver: impl Into<Option<DriverId>>) -> Self {
self.driver = driver.into();
self
}
#[must_use]
pub fn device_id(mut self, device_id: impl Into<Option<DeviceId>>) -> Self {
self.device_id = device_id.into();
self
}
#[must_use]
pub fn device_stored(mut self, device_stored: bool) -> Self {
self.device_stored = device_stored;
self
}
#[must_use]
pub fn enroll_date(mut self, enroll_date: impl Into<Option<EnrollDate>>) -> Self {
self.enroll_date = enroll_date.into();
self
}
#[must_use]
pub fn build(self) -> Print {
Print {
template: self.template,
finger: self.finger,
username: self.username,
description: self.description,
driver: self.driver,
device_id: self.device_id,
device_stored: self.device_stored,
enroll_date: self.enroll_date,
}
}
}
#[cfg(test)]
mod tests {
use super::{EnrollDate, InvalidEnrollDate, Minutia, Print, Template};
use crate::{DriverId, Finger};
#[test]
fn try_new_accepts_in_range_and_rejects_out_of_range() {
assert_eq!(
EnrollDate::try_new(2026, 7, 15),
Ok(EnrollDate::new(2026, 7, 15))
);
for (month, day) in [(1u8, 1u8), (12, 31)] {
assert_eq!(
EnrollDate::try_new(2026, month, day),
Ok(EnrollDate::new(2026, month, day))
);
}
assert_eq!(
EnrollDate::try_new(2026, 0, 1),
Err(InvalidEnrollDate::Month(0))
);
assert_eq!(
EnrollDate::try_new(2026, 13, 1),
Err(InvalidEnrollDate::Month(13))
);
assert_eq!(
EnrollDate::try_new(2026, 1, 0),
Err(InvalidEnrollDate::Day(0))
);
assert_eq!(
EnrollDate::try_new(2026, 1, 32),
Err(InvalidEnrollDate::Day(32))
);
}
#[test]
fn new_for_enroll_sets_the_finger_and_leaves_the_template_undefined() {
for finger in Finger::ALL.iter().chain(&[Finger::Unknown]) {
let print = Print::new_for_enroll(*finger);
assert_eq!(print.finger, Some(*finger));
assert_eq!(print.template, Template::Undefined);
assert!(!print.template.is_raw());
assert_eq!(print.driver, None);
assert_eq!(print.device_id, None);
assert_eq!(print.username, None);
assert_eq!(print.description, None);
assert_eq!(print.enroll_date, None);
assert!(!print.device_stored);
}
}
#[test]
fn an_unbound_print_is_compatible_with_any_driver() {
let print = Print::new_for_enroll(Finger::LeftIndex);
assert_eq!(print.driver, None);
for driver in ["goodixmoc", "synaptics", ""] {
assert!(print.is_compatible_with_driver(&DriverId::new(driver)));
}
}
#[test]
fn a_bound_print_is_compatible_only_with_its_own_driver() {
let print = Print {
driver: Some(DriverId::new("goodixmoc")),
..Print::new_for_enroll(Finger::LeftIndex)
};
assert!(print.is_compatible_with_driver(&DriverId::new("goodixmoc")));
for other in ["synaptics", "GOODIXMOC", "goodixmoc2", ""] {
assert!(
!print.is_compatible_with_driver(&DriverId::new(other)),
"{other:?} must not match goodixmoc"
);
}
}
#[test]
fn is_raw_holds_only_for_the_raw_template() {
assert!(Template::Raw(Vec::new()).is_raw());
assert!(Template::Raw(b"handle".to_vec()).is_raw());
assert!(!Template::Undefined.is_raw());
assert!(!Template::default().is_raw());
assert!(!Template::Nbis(Vec::new()).is_raw());
assert!(!Template::Nbis(vec![vec![Minutia {
x: 1,
y: 2,
theta: 3
}]])
.is_raw());
}
#[test]
fn the_default_template_is_undefined() {
assert_eq!(Template::default(), Template::Undefined);
assert_eq!(Print::default().template, Template::Undefined);
assert_eq!(Print::default().finger, None);
}
}