Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Error trait for FixError #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/gtin12/mod.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
///
Expand Down
20 changes: 19 additions & 1 deletion src/gtin13/mod.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
///
Expand Down Expand Up @@ -152,7 +171,6 @@ mod tests {
assert!(fix("❤").is_err());
}


#[test]
fn fix_too_long() {
assert_eq!(fix("00000000000000"), Err(FixError::TooLong));
Expand Down
19 changes: 19 additions & 0 deletions src/gtin14/mod.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
///
Expand Down
19 changes: 19 additions & 0 deletions src/gtin8/mod.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
///
Expand Down