#gtin #upc #integer #ean

gtin-validate

Validate GTIN barcodes including UPC-A and EAN-13. This module has built-in support for GTIN-8, GTIN-12 (UPC-A), GTIN-13 (EAN-13), and GTIN-14 codes

5 releases (stable)

Uses old Rust 2015

1.3.0 Jan 19, 2020
1.2.0 Dec 1, 2018
1.1.0 Aug 27, 2018
1.0.0 Jun 11, 2016
0.5.0 May 31, 2016

#1319 in Algorithms

Download history 3/week @ 2023-12-04 20/week @ 2024-01-01 15/week @ 2024-02-05 25/week @ 2024-02-19 5/week @ 2024-02-26 14/week @ 2024-03-04 45/week @ 2024-03-11 14/week @ 2024-03-18

89 downloads per month

MIT/Apache

26KB
511 lines

Rust GTIN Validator

Build Status Coverage Status Documentation

Validate and correct GTIN codes, such as UPC-A and EAN-13, in Rust.

Features

Currently supported types:

  • GTIN-8
  • GTIN-12 (UPC-A)
  • GTIN-13 (EAN-13)
  • GTIN-14

Validation features include:

  • Check that the string contains the correct number of digits
  • Compute the check-digit and confirm that it matches

Correction features include:

  • Add zero-padding (some software treats these codes as integers)
  • Strip whitespace from both sides of the code

How to use

For full details, check the documentation.

Briefly, here are the function declarations:

// return true if the code is valid, false otherwise
fn gtin8::check(code: &str) -> bool;
fn gtin12::check(code: &str) -> bool;
fn gtin13::check(code: &str) -> bool;
fn gtin14::check(code: &str) -> bool;

// return a corrected String or Err
fn gtin8::fix(code: &str) -> Result<String, FixError>;
fn gtin12::fix(code: &str) -> Result<String, FixError>;
fn gtin13::fix(code: &str) -> Result<String, FixError>;
fn gtin14::fix(code: &str) -> Result<String, FixError>;

For example, you can validate UPC-A codes:

use gtin_validate::gtin12;

assert_eq!(gtin12::check("000000000000"), true);
assert_eq!(gtin12::check("000000000001"), false);

You can add this line to your Cargo.toml file:

[dependencies]
gtin-validate = "1.3.0"

Contributing

Found a bug? Report an issue through GitHub.

Want to hack on the code? Submit a pull request.

No runtime deps