[][src]Function gtin_validate::gtin12::fix

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

Attempt to fix invalid UPC codes by stripping whitespace from the left and right sides and zero-padding the UPC if it is less than 12 digits in length.

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

Examples

use gtin_validate::gtin12;

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

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

It is also possible to detect errors:

use gtin_validate::gtin12;
let result = gtin12::fix("123412341234123"); // UPC too long
assert!(result.is_err());