gl_gtin/parse

Structured GTIN parsing module (F4).

Decomposes a validated GTIN string into a structured GtinInfo record exposing the detected format, the trimmed digits, the packaging-level indicator (for GTIN-14), the GS1 prefix, the GS1 prefix region, and the check digit. It composes the shared subsystems — validation for the length/character/check-digit gate, gs1_prefix for the region lookup, and internal/utils for digit parsing — and works directly in the public GtinError type.

GS1 company-prefix limitation

The length of the GS1 company prefix within a GTIN is assigned per licensee and is NOT derivable offline from the digits alone (it requires a GEPIR / GS1 registry lookup). This module therefore does not attempt to split the code into company prefix and item reference. Only the GS1 prefix region (derived from the leading three digits of the format-normalized 13-digit basis) is exposed, via gs1_region. That region is derived solely from the gs1_prefix subsystem; this module does not read or reimplement the prefix allocation table.

Types

Structured decomposition of a validated GTIN code.

Every field is populated only after the input passes validation, so a GtinInfo value always describes a well-formed GTIN.

  • format - the GTIN format inferred from the trimmed digit count.
  • digits - the trimmed input string.
  • indicator - Ok(n) with the leading indicator digit for a GTIN-14, Error(Nil) for every other format.
  • gs1_prefix - the leading three characters of the format-normalized 13-digit basis (the true GS1 prefix).
  • gs1_region - the GS1 prefix region from the prefix subsystem, Ok(name) on a hit or Error(NoGs1PrefixFound) when the prefix has no allocation.
  • check_digit - the integer value of the trimmed code’s final digit.
pub type GtinInfo {
  GtinInfo(
    format: gtin_types.GtinFormat,
    digits: String,
    indicator: Result(Int, Nil),
    gs1_prefix: String,
    gs1_region: Result(String, gtin_types.GtinError),
    check_digit: Int,
  )
}

Constructors

Values

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

Decompose a validated GTIN into a structured GtinInfo record.

The input is trimmed and validated first (length, then characters, then check digit). An invalid code returns the corresponding GtinError (InvalidLength, InvalidCharacters, or InvalidCheckDigit) and never a partially populated GtinInfo. On success every field of the record is populated from the trimmed input.

The GS1 company-prefix length is not derivable offline; only the GS1 prefix region is exposed, and it is derived solely from the gs1_prefix subsystem.

Arguments

  • code - The GTIN string to parse (leading/trailing whitespace is trimmed)

Returns

Ok(GtinInfo) with every field populated if the code is a valid GTIN, Error(InvalidLength(got: n)) if the trimmed digit count is not 8/12/13/14, Error(InvalidCharacters) if any character is non-numeric, Error(InvalidCheckDigit) if the check digit does not match.

Examples

parse("6291041500213")
// -> Ok(GtinInfo(
//   format: Gtin13,
//   digits: "6291041500213",
//   indicator: Error(Nil),
//   gs1_prefix: "629",
//   gs1_region: Ok("GS1 Emirates"),
//   check_digit: 3,
// ))

parse("invalid")
// -> Error(InvalidCharacters)
✨ Search Document