gl_gtin/gs1_prefix

GS1 prefix lookup module for GTIN codes.

Provides country/region lookup based on GS1 prefix codes.

The lookup parses the first three digits of a format-normalized 13-digit basis as an integer and matches it against GS1’s real 3-digit allocation ranges (ported from the ex_gtin lookup_gs1_prefix/1 table).

Input length leniency

lookup is intentionally lenient about total input length. It does not enforce a strict GTIN length (8/12/13/14); instead it guards only that the normalized basis has at least three characters (>= 3). Shorter input yields Error(NoGs1PrefixFound), and extra trailing characters are ignored. A stricter length check is deliberately out of scope to avoid a second breaking change to the error surface.

GTIN-8 limitation

A GTIN-8 is used as-is for the prefix basis, so it reads its own leading digits against the GTIN-13 range table. This module does NOT implement true GS1-8 semantics; the result for a GTIN-8 reflects the GTIN-13 table lookup rather than a dedicated GS1-8 allocation.

Types

Error type for prefix lookup operations

pub type PrefixError {
  NoGs1PrefixFound
}

Constructors

  • NoGs1PrefixFound

Values

pub fn lookup(code: String) -> Result(String, PrefixError)

Look up the country of origin from a GTIN code’s GS1 prefix.

Computes the format-normalized 13-digit basis, then parses its first three digits as an integer and matches GS1’s real 3-digit allocation ranges.

Arguments

  • code - GTIN code string

Returns

Ok(country) if a prefix is found, Error(NoGs1PrefixFound) otherwise.

Examples

lookup("6291041500213")
// -> Ok("GS1 Emirates")

lookup("8711111111116")
// -> Ok("GS1 Netherlands")

lookup("999999999999")
// -> Error(NoGs1PrefixFound)
✨ Search Document