pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
NotFound,
NotSupported,
Busy,
ProtoState,
Cancelled,
RetryScan(RetryReason),
DataFull,
DataDuplicate,
DataNotFound,
Transport(String),
Protocol(String),
Other(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RetryReason {
General,
TooShort,
NotCentered,
RemoveAndRetry,
TooFast,
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::NotFound => f.write_str("device not found"),
Error::NotSupported => f.write_str("operation not supported by device"),
Error::Busy => f.write_str("device busy"),
Error::ProtoState => f.write_str("device in wrong state for operation"),
Error::Cancelled => f.write_str("operation cancelled"),
Error::RetryScan(r) => write!(f, "retry scan: {r:?}"),
Error::DataFull => f.write_str("on-device storage full"),
Error::DataDuplicate => f.write_str("finger already enrolled"),
Error::DataNotFound => f.write_str("template not found"),
Error::Transport(m) => write!(f, "transport error: {m}"),
Error::Protocol(m) => write!(f, "protocol error: {m}"),
Error::Other(m) => write!(f, "{m}"),
}
}
}
impl std::error::Error for Error {}
#[cfg(test)]
mod tests {
use super::{Error, RetryReason};
#[test]
fn every_error_variant_renders_its_payload() {
const MARKER: &str = "cafef00d-marker";
let reasons = [
RetryReason::General,
RetryReason::TooShort,
RetryReason::NotCentered,
RetryReason::RemoveAndRetry,
RetryReason::TooFast,
];
let samples = [
Error::NotFound,
Error::NotSupported,
Error::Busy,
Error::ProtoState,
Error::Cancelled,
Error::DataFull,
Error::DataDuplicate,
Error::DataNotFound,
Error::Transport(MARKER.to_string()),
Error::Protocol(MARKER.to_string()),
Error::Other(MARKER.to_string()),
];
let samples: Vec<Error> = samples
.into_iter()
.chain(reasons.map(Error::RetryScan))
.collect();
for error in &samples {
let rendered = error.to_string();
assert!(!rendered.trim().is_empty(), "{error:?} renders nothing");
match error {
Error::NotFound
| Error::NotSupported
| Error::Busy
| Error::ProtoState
| Error::Cancelled
| Error::DataFull
| Error::DataDuplicate
| Error::DataNotFound => {}
Error::RetryScan(reason) => {
let shown = format!("{reason:?}");
assert!(rendered.contains(&shown), "{rendered:?} drops {shown:?}");
}
Error::Transport(message) | Error::Protocol(message) | Error::Other(message) => {
assert!(rendered.contains(message), "{rendered:?} drops {message:?}");
}
}
}
}
#[test]
fn payload_free_variants_render_distinctly() {
let messages = [
Error::NotFound.to_string(),
Error::NotSupported.to_string(),
Error::Busy.to_string(),
Error::ProtoState.to_string(),
Error::Cancelled.to_string(),
Error::DataFull.to_string(),
Error::DataDuplicate.to_string(),
Error::DataNotFound.to_string(),
];
for (i, a) in messages.iter().enumerate() {
for b in &messages[i + 1..] {
assert_ne!(a, b, "two variants share the message {a:?}");
}
}
}
}