use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ViErrorKind {
InvalidHeader,
InvalidPayload,
InvalidDisclosure,
Expired,
NotYetValid,
SignatureInvalid,
KeyMismatch,
KeyUnsupported,
SdHashMismatch,
CrossReferenceMismatch,
ReferenceBindingMismatch,
AmountOutOfRange,
BudgetExceeded,
CurrencyMismatch,
MerchantNotAllowed,
PayeeNotAllowed,
LineItemViolation,
RecurrenceViolation,
UnknownConstraintType,
ModeMismatch,
UnknownMandateType,
IncompleteMandatePair,
IssuanceInputInvalid,
}
#[derive(Debug, Clone)]
pub struct ViError {
pub kind: ViErrorKind,
pub message: String,
}
impl ViError {
pub fn new(kind: ViErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
}
}
}
impl fmt::Display for ViError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "VI/{:?}: {}", self.kind, self.message)
}
}
impl std::error::Error for ViError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_display_includes_kind_and_message() {
let err = ViError::new(ViErrorKind::AmountOutOfRange, "50000 > 40000 USD");
let s = format!("{err}");
assert!(s.contains("AmountOutOfRange"));
assert!(s.contains("50000 > 40000 USD"));
}
#[test]
fn error_kind_equality() {
assert_eq!(ViErrorKind::Expired, ViErrorKind::Expired);
assert_ne!(ViErrorKind::Expired, ViErrorKind::SignatureInvalid);
}
}