gl_gtin/upc

UPC-E ⇄ UPC-A conversion module (F1).

Implements the GS1 zero-suppression rules that expand a compressed 8-digit UPC-E code to its full 12-digit UPC-A form (and, in a later task, compress a UPC-A back to UPC-E). These functions work directly in the public GtinError type because their failure conditions are exactly the public error variants and they compose the shared check-digit engine.

Values

pub fn upca_to_upce(
  code: String,
) -> Result(String, gtin_types.GtinError)

Compress a full 12-digit UPC-A code to its 8-digit UPC-E form when possible.

A UPC-A code is 12 digits: a number-system digit (NS), a 10-digit manufacturer-plus-item block, and a UPC-A check digit. Compression to UPC-E is only possible when NS is 0 or 1 and the manufacturer-plus-item block matches one of the GS1 zero-suppression patterns (the exact inverse of the expansion table used by upce_to_upca). When it matches, the six UPC-E body digits are reconstructed and the resulting 8-digit UPC-E string is returned. The UPC-E check digit is the same as the UPC-A check digit (the code is validated first, so its final digit is already the correct check digit), which keeps the upca_to_upce/upce_to_upca round-trip exact.

Defects are evaluated in the order character validity, then digit count, then check-digit correctness, then compressibility, and only the first matching error is returned with no partial UPC-E produced.

Arguments

  • code - The UPC-A string to compress (leading/trailing whitespace is trimmed)

Returns

Ok(upce) as an 8-digit string if the code is a compressible UPC-A, Error(InvalidCharacters) if any character is non-numeric, Error(InvalidLength(got: n)) if the trimmed digit count is not 12, Error(InvalidCheckDigit) if the check digit does not match, Error(InvalidFormat) if the number-system digit is not 0 or 1 or the code is not zero-suppressible.

Examples

upca_to_upce("042100005264")
// -> Ok("04252614")

upca_to_upce("012345678905")
// -> Error(InvalidFormat)
pub fn upce_to_upca(
  code: String,
) -> Result(String, gtin_types.GtinError)

Expand a compressed 8-digit UPC-E code to its full 12-digit UPC-A form.

A UPC-E code is 8 digits: a number-system digit (NS, which must be 0 or 1), six body digits X1 X2 X3 X4 X5 X6, and a UPC-E check digit. The 6th body digit X6 selects the zero-suppression expansion that reconstructs the 10-digit manufacturer-plus-item block of the UPC-A. The NS digit is prepended, the check digit is recomputed via the GS1 Modulo 10 algorithm, and the resulting 12-digit UPC-A string is returned.

Defects are evaluated in the order length, then character validity, then number-system validity, and only the first matching error is returned with no partial UPC-A produced.

Arguments

  • code - The UPC-E string to expand (leading/trailing whitespace is trimmed)

Returns

Ok(upca) as a 12-digit string if the code is a valid UPC-E, Error(InvalidLength(got: n)) if the trimmed length is not 8, Error(InvalidCharacters) if any character is non-numeric, Error(InvalidFormat) if the number-system digit is not 0 or 1.

Examples

upce_to_upca("04252614")
// -> Ok("042100005264")

upce_to_upca("12345")
// -> Error(InvalidLength(got: 5))

upce_to_upca("24252614")
// -> Error(InvalidFormat)
✨ Search Document