1use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub enum ValidationError {
9 ZeroQuantity,
11 ZeroPrice,
13}
14
15impl fmt::Display for ValidationError {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 match self {
18 ValidationError::ZeroQuantity => write!(f, "quantity must be greater than zero"),
19 ValidationError::ZeroPrice => write!(f, "price must be greater than zero"),
20 }
21 }
22}
23
24impl std::error::Error for ValidationError {}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn display() {
32 assert_eq!(
33 format!("{}", ValidationError::ZeroQuantity),
34 "quantity must be greater than zero"
35 );
36 assert_eq!(
37 format!("{}", ValidationError::ZeroPrice),
38 "price must be greater than zero"
39 );
40 }
41
42 #[test]
43 fn is_error() {
44 let err: Box<dyn std::error::Error> = Box::new(ValidationError::ZeroQuantity);
45 assert!(err.to_string().contains("quantity"));
46 }
47}