[][src]Function gtin_validate::gtin14::fix

pub fn fix(code: &str) -> Result<String, FixError>

Attempt to fix an invalid GTIN-14 code by stripping whitespace from the left and right sides and zero-padding the code if it is less than 14 digits in length.

These corrections fix many common errors introduced by manual data entry and software that treats GTINs as integers rather than strings, thus truncating the leading zeros.

Examples

use gtin_validate::gtin14;

// Add missing zero, fixing length
let result1 = gtin14::fix("04527819983417");
assert!(result1.is_ok());
assert_eq!(result1.unwrap(), "04527819983417");

// Remove extra whitespace
let result2 = gtin14::fix("04527819983417 ");
assert!(result2.is_ok());
assert_eq!(result2.unwrap(), "04527819983417");

Here is how you catch errors:

match gtin14::fix("14507829283411") {
  Ok(fixed) => {println!("Fixed GTIN-14: {}", fixed);}
  Err(_) => {println!("Could not fix GTIN-14");}
}