GS1.Parser (gs1_barcode v0.1.2)
View SourceParser for GS1 Data Structures.
Parsing pipeline:
- Prefix matching: Identifies the Symbology Identifier (e.g.,
]d2for DataMatrix). - Tokenization: Splits the raw string into segments using
Tokenizer. Each segment is defined by 2 digit "base AI" (than must be normalized and checked and verified withAIRegistry) and data part. - Normalization: Reconstructs full AIs from tokens (e.g., merging
31+03->3103) and performs compliance checks against theAIRegistry. - Date Structure creation: Returns
GS1.DataStructure.t/0suitable for further validation and processing.
Summary
Functions
Parses a raw GS1 string into a GS1.DataStructure.
Types
@type error_reason() :: :empty | :invalid_input | {:tokenize, String.t(), non_neg_integer()} | {:duplicate_ai, {String.t(), String.t()}} | {:unknown_ai, {String.t(), String.t()}} | {:not_enough_data, {String.t(), String.t()}} | {:ai_part_non_num, {String.t(), String.t()}}
Error reasons returned by parse/1.
Simple errors
:empty- input string is empty.:invalid_input- input is not a binary string.
Tokenization errors
{:tokenize, reason, position}- string structure is invalid or malformed. Thereasonis a description from the tokenizer, andpositionis character index where the invalid sequence begins.
AI processing errors
All AI errors include a tuple {ai, data} containing the AI code
and its associated data segment:
{:unknown_ai, {ai, data}}- AI is not recognized in the registry.{:duplicate_ai, {ai, data}}- same AI appears more than once.{:not_enough_data, {ai, data}}- data segment is too short to reconstruct a 3 or 4 digit AI.{:ai_part_non_num, {ai, data}}- expected numeric digits for the AI suffix during reconstruction, but found non-digit characters.
Functions
@spec parse(String.t()) :: {:ok, GS1.DataStructure.t()} | {:error, error_reason()}
Parses a raw GS1 string into a GS1.DataStructure.
Errors
The following error tuples may be returned:
:empty- input string is empty.:invalid_input- input is not a binary string.{:tokenize, reason, invalid_seq_start}- string structure is invalid or malformed. Aninvalid_seq_startis an index of bad sequence in input string.{:unknown_ai, {ai, data}}- AI is not recognized{:duplicate_ai, {ai, data}}- same AI appears twice{:not_enough_data, {ai, data}}- string ends prematurely for a AI.{:ai_part_non_num, {ai, data}}- expected digits for an AI suffix during reconstruction, but found other characters.
Examples
iex> GS1.Parser.parse("]d20198765432109876")
{:ok,
%GS1.DataStructure{
content: "]d20198765432109876",
type: :gs1_datamatrix,
fnc1_prefix: "]d2",
ais: %{"01" => "98765432109876"}
}}