From 0117557b8eabf12d94b9e91044e5ac847e3657ba Mon Sep 17 00:00:00 2001 From: Guido Knips Date: Fri, 17 Feb 2023 17:43:00 +0100 Subject: [PATCH] Implement Error trait for FixError --- src/gtin12/mod.rs | 19 +++++++++++++++++++ src/gtin13/mod.rs | 20 +++++++++++++++++++- src/gtin14/mod.rs | 19 +++++++++++++++++++ src/gtin8/mod.rs | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/gtin12/mod.rs b/src/gtin12/mod.rs index bc23997..971aba4 100644 --- a/src/gtin12/mod.rs +++ b/src/gtin12/mod.rs @@ -1,5 +1,7 @@ //! Performs validation and correction of GTIN-12 and UPC-A codes. +use std::error::Error; +use std::fmt; use utils; /// Errors that make GTIN-12 correction impossible. @@ -13,6 +15,23 @@ pub enum FixError { CheckDigitIncorrect, } +impl Error for FixError {} + +impl fmt::Display for FixError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + FixError::NonAsciiString => { + write!(f, "the provided string contains non-ASCII characters") + } + FixError::TooLong => write!(f, "the provided code was too long too be valid"), + FixError::CheckDigitIncorrect => write!( + f, + "the calculated check-digit did not match the code's check-digit" + ), + } + } +} + /// Check that a UPC-A code is valid by confirming that it is made of /// exactly 12 digits and that the check-digit is correct. /// diff --git a/src/gtin13/mod.rs b/src/gtin13/mod.rs index a07ac8a..1866b6e 100644 --- a/src/gtin13/mod.rs +++ b/src/gtin13/mod.rs @@ -1,5 +1,7 @@ //! Performs validation and correction of GTIN-13 and EAN-13 codes. +use std::error::Error; +use std::fmt; use utils; /// Errors that make GTIN-13 correction impossible. @@ -13,6 +15,23 @@ pub enum FixError { CheckDigitIncorrect, } +impl Error for FixError {} + +impl fmt::Display for FixError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + FixError::NonAsciiString => { + write!(f, "the provided string contains non-ASCII characters") + } + FixError::TooLong => write!(f, "the provided code was too long too be valid"), + FixError::CheckDigitIncorrect => write!( + f, + "the calculated check-digit did not match the code's check-digit" + ), + } + } +} + /// Check that a GTIN-13 code is valid by checking the length (should be /// exactly 13 digits) and that the check-digit is correct. /// @@ -152,7 +171,6 @@ mod tests { assert!(fix("❤").is_err()); } - #[test] fn fix_too_long() { assert_eq!(fix("00000000000000"), Err(FixError::TooLong)); diff --git a/src/gtin14/mod.rs b/src/gtin14/mod.rs index 8408742..b2da1bb 100644 --- a/src/gtin14/mod.rs +++ b/src/gtin14/mod.rs @@ -1,5 +1,7 @@ //! Performs validation and correction of GTIN-14 codes. +use std::error::Error; +use std::fmt; use utils; /// Errors that make GTIN-14 correction impossible. @@ -13,6 +15,23 @@ pub enum FixError { CheckDigitIncorrect, } +impl Error for FixError {} + +impl fmt::Display for FixError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + FixError::NonAsciiString => { + write!(f, "the provided string contains non-ASCII characters") + } + FixError::TooLong => write!(f, "the provided code was too long too be valid"), + FixError::CheckDigitIncorrect => write!( + f, + "the calculated check-digit did not match the code's check-digit" + ), + } + } +} + /// Check that a GTIN-14 code is valid by confirming that it is exactly /// 14 digits in length and that the check-digit is correct. /// diff --git a/src/gtin8/mod.rs b/src/gtin8/mod.rs index 8befd31..159405f 100644 --- a/src/gtin8/mod.rs +++ b/src/gtin8/mod.rs @@ -1,5 +1,7 @@ //! Performs validation and correction of GTIN-8 codes. +use std::error::Error; +use std::fmt; use utils; /// Errors that make GTIN-8 correction impossible. @@ -13,6 +15,23 @@ pub enum FixError { CheckDigitIncorrect, } +impl Error for FixError {} + +impl fmt::Display for FixError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + FixError::NonAsciiString => { + write!(f, "the provided string contains non-ASCII characters") + } + FixError::TooLong => write!(f, "the provided code was too long too be valid"), + FixError::CheckDigitIncorrect => write!( + f, + "the calculated check-digit did not match the code's check-digit" + ), + } + } +} + /// Check that a GTIN-8 code is valid by confirming that it is exactly /// 8 digits in length and that the check-digit is correct. ///