Function gtin_validate::gtin8::fix
[−]
[src]
pub fn fix(code: &str) -> Result<String, FixError>
Attempt to fix an invalid GTIN-8 code by stripping whitespace from the left and right sides and zero-padding the code if it is less than 8 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::gtin8; // Add missing zero, fixing length let result1 = gtin8::fix("5766796"); assert!(result1.is_ok()); assert_eq!(result1.unwrap(), "05766796"); // Remove extra whitespace let result2 = gtin8::fix("05766796 "); assert!(result2.is_ok()); assert_eq!(result2.unwrap(), "05766796");
Here is how you catch errors:
match gtin8::fix("14567811") { Ok(fixed) => {println!("Fixed GTIN-14: {}", fixed);} Err(_) => {println!("Could not fix GTIN-14");} }