use crate::error::RetryReason;
use crate::{DeviceFeature, Error, FingerStatus, Print, Result, ScanType, Temperature};
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct DeviceId(String);
impl DeviceId {
#[must_use]
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl core::fmt::Display for DeviceId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for DeviceId {
fn from(id: &str) -> Self {
Self::new(id)
}
}
impl From<String> for DeviceId {
fn from(id: String) -> Self {
Self::new(id)
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct DriverId(String);
impl DriverId {
#[must_use]
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl core::fmt::Display for DriverId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for DriverId {
fn from(id: &str) -> Self {
Self::new(id)
}
}
impl From<String> for DriverId {
fn from(id: String) -> Self {
Self::new(id)
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct DeviceInfo {
pub id: DeviceId,
pub driver: DriverId,
pub name: String,
pub scan_type: ScanType,
pub features: DeviceFeature,
pub enroll_stages: u32,
}
impl DeviceInfo {
#[must_use]
pub fn new(
id: DeviceId,
driver: DriverId,
name: impl Into<String>,
scan_type: ScanType,
features: DeviceFeature,
enroll_stages: u32,
) -> Self {
DeviceInfo {
id,
driver,
name: name.into(),
scan_type,
features,
enroll_stages,
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct EnrollProgress {
pub completed_stages: u32,
pub total_stages: u32,
pub retry: Option<RetryReason>,
pub finger_status: FingerStatus,
}
impl EnrollProgress {
#[must_use]
pub fn new(completed_stages: u32, total_stages: u32) -> Self {
EnrollProgress {
completed_stages,
total_stages,
retry: None,
finger_status: FingerStatus::NONE,
}
}
#[must_use]
pub fn with_retry(mut self, r: RetryReason) -> Self {
self.retry = Some(r);
self
}
#[must_use]
pub fn with_finger_status(mut self, s: FingerStatus) -> Self {
self.finger_status = s;
self
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct VerifyOutcome {
pub matched: bool,
pub scanned: Option<Print>,
}
impl VerifyOutcome {
#[must_use]
pub fn new(matched: bool, scanned: Option<Print>) -> Self {
VerifyOutcome { matched, scanned }
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct IdentifyOutcome {
pub match_index: Option<usize>,
pub scanned: Option<Print>,
}
impl IdentifyOutcome {
#[must_use]
pub fn new(match_index: Option<usize>, scanned: Option<Print>) -> Self {
IdentifyOutcome {
match_index,
scanned,
}
}
}
#[expect(async_fn_in_trait)]
pub trait Device {
fn info(&self) -> &DeviceInfo;
fn has_feature(&self, feature: DeviceFeature) -> bool {
self.info().features.contains(feature)
}
fn temperature(&self) -> Option<Temperature> {
None
}
async fn open(&mut self) -> Result<()>;
async fn close(&mut self) -> Result<()>;
async fn enroll<F: FnMut(EnrollProgress)>(
&mut self,
print: Print,
on_progress: F,
) -> Result<Print>;
async fn verify_with_status<F: FnMut(FingerStatus)>(
&mut self,
enrolled: &Print,
on_status: F,
) -> Result<VerifyOutcome>;
async fn verify(&mut self, enrolled: &Print) -> Result<VerifyOutcome> {
self.verify_with_status(enrolled, |_| {}).await
}
async fn identify_with_status<F: FnMut(FingerStatus)>(
&mut self,
gallery: &[Print],
on_status: F,
) -> Result<IdentifyOutcome>;
async fn identify(&mut self, gallery: &[Print]) -> Result<IdentifyOutcome> {
self.identify_with_status(gallery, |_| {}).await
}
async fn list_prints(&mut self) -> Result<Vec<Print>> {
Err(Error::NotSupported)
}
async fn delete_print(&mut self, print: &Print) -> Result<()> {
let _ = print;
Err(Error::NotSupported)
}
async fn clear_storage(&mut self) -> Result<()> {
Err(Error::NotSupported)
}
async fn suspend(&mut self) -> Result<()>;
async fn resume(&mut self) -> Result<()>;
}
#[expect(async_fn_in_trait)]
pub trait Backend {
type Device: Device;
async fn enumerate(&self) -> Result<Vec<Self::Device>>;
async fn open(&self, id: &DeviceId) -> Result<Self::Device>;
}
#[cfg(test)]
mod tests {
use super::{Device, DeviceId, DeviceInfo, DriverId, EnrollProgress, IdentifyOutcome};
use crate::feature::FLAGS;
use crate::{DeviceFeature, FingerStatus, Print, Result, ScanType, VerifyOutcome};
struct InfoOnly(DeviceInfo);
impl Device for InfoOnly {
fn info(&self) -> &DeviceInfo {
&self.0
}
async fn open(&mut self) -> Result<()> {
todo!("has_feature never opens the device")
}
async fn close(&mut self) -> Result<()> {
todo!("has_feature never opens the device")
}
async fn enroll<F: FnMut(EnrollProgress)>(&mut self, _: Print, _: F) -> Result<Print> {
todo!("has_feature never scans")
}
async fn verify_with_status<F: FnMut(FingerStatus)>(
&mut self,
_: &Print,
_: F,
) -> Result<VerifyOutcome> {
todo!("has_feature never scans")
}
async fn identify_with_status<F: FnMut(FingerStatus)>(
&mut self,
_: &[Print],
_: F,
) -> Result<IdentifyOutcome> {
todo!("has_feature never scans")
}
async fn suspend(&mut self) -> Result<()> {
todo!("has_feature never suspends")
}
async fn resume(&mut self) -> Result<()> {
todo!("has_feature never suspends")
}
}
fn device_with(features: DeviceFeature) -> InfoOnly {
InfoOnly(DeviceInfo::new(
DeviceId::new("test-0"),
DriverId::new("test"),
"Test Reader",
ScanType::Press,
features,
1,
))
}
#[test]
fn has_feature_agrees_with_the_advertised_set() {
let defined = DeviceFeature::from_bits_truncate(u32::MAX).bits();
for bits in 0..=u32::from(defined) {
let advertised = DeviceFeature::from_bits_truncate(bits);
let device = device_with(advertised);
for flag in
core::iter::once(DeviceFeature::NONE).chain(FLAGS.into_iter().map(|(f, _)| f))
{
assert_eq!(
device.has_feature(flag),
advertised.contains(flag),
"{advertised:?}.has_feature({flag:?})"
);
}
}
}
}