moonepc

    Strict GS1 EPC URI and RFID tag bit encoding toolkit

    gs1
    epc
    rfid
    tds
    tdt
    encoding
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    9 hours ago
    Downloads
    5

    No dependencies

    #MoonEPC

    中文文档:README.zh-CN.md。 设计说明:docs/design.md;版本记录:CHANGELOG.md。

    MoonEPC 是一个纯 MoonBit 的 GS1 EPC 标识与 RFID tag-memory 编解码库和命令行工具。它把严格校验的 EPC tag URI、pure-identity URI 与 SGTIN-96、SSCC-96、SGLN-96、GRAI-96、GIAI-96、GDTI-96 的 96 位十六进制表示互相转换,适用于读写器适配层、供应链标识服务和协议测试工具。

    当前范围限于 SGTIN-96、SSCC-96、SGLN-96、GRAI-96、数字型 GIAI-96 与 GDTI-96。项目不实现通用 GS1 Application Identifier/条码解析、GS1 Digital Link、EPCIS 事件、RFID 空口帧、CRC、硬件读写器驱动,也不声称覆盖完整 GS1 Tag Data Standard / Tag Data Translation。

    #环境与安装

    • MoonBit CLI:使用支持 moon.mod 模块的近期稳定版。
    • 已验证目标:WebAssembly 与 JavaScript;Native 测试需要本机 C 编译器。
    • 包坐标:Shai1213/moonepc@0.1.0。
    • 通用入口:parse_epc96_tag_uri 按 tag URI 前缀识别编码;parse_epc96_identity_uri 解析身份 URI 并要求调用方提供 tag filter;decode_epc96 按 EPC header 识别 hex 输入。

    在消费者项目的 moon.mod 中加入精确版本依赖:

    import {
    "Shai1213/moonepc@0.1.0",
    }

    在调用包的 moon.pkg 中加入导入:

    import {
    "Shai1213/moonepc",
    }

    然后在消费者项目运行 moon update 和 moon check --target wasm --deny-warn。下方 CLI 命令在本项目源码根目录运行;API 示例放入消费者的 fn main 或测试块后运行。

    在包中导入 Shai1213/moonepc,使用各 scheme 对应的 parse_*_tag_uri 构造校验过的值,再调用 encode_hex;decode_sgtin96、decode_sscc96、decode_sgln96、decode_grai96、decode_giai96 与 decode_gdti96 负责反向解码。所有可预期输入错误通过 Result[..., EpcError] 返回。

    #快速开始

    moon run --target wasm cmd/main -- sgtin-encode urn:epc:tag:sgtin-96:3.0614141.812345.6789 # 3074257bf7194e4000001a85 moon run --target wasm cmd/main -- sgtin-decode 3074257bf7194e4000001a85 # urn:epc:tag:sgtin-96:3.0614141.812345.6789 moon run --target wasm cmd/main -- sscc-encode urn:epc:tag:sscc-96:0.123456789012.12345 moon run --target wasm cmd/main -- sgln-encode urn:epc:tag:sgln-96:3.0614141.00123.456 moon run --target wasm cmd/main -- grai-encode urn:epc:tag:grai-96:5.0614141.81234.123456 # 33b4257bf44f54800001e240 moon run --target wasm cmd/main -- giai-encode urn:epc:tag:giai-96:6.999999.123456789 # 34dbd08fc0000000075bcd15 moon run --target wasm cmd/main -- gdti-encode urn:epc:tag:gdti-96:3.0614141.12345.6789 # 2c74257bf460720000001a85

    命令为 sgtin-encode/decode、sscc-encode/decode、sgln-encode/decode、grai-encode/decode、giai-encode/decode、gdti-encode/decode。URI 必须使用 tag URI 形式;解码器要求恰好 96 位(24 个 hex 字符),接受大小写十六进制输入。

    不知道具体 scheme 时可使用统一 CLI 分派:

    moon run --target wasm cmd/main -- encode urn:epc:tag:gdti-96:3.0614141.12345.6789 # 2c74257bf460720000001a85 moon run --target wasm cmd/main -- decode 2c74257bf460720000001a85 # urn:epc:tag:gdti-96:3.0614141.12345.6789 moon run --target wasm cmd/main -- identity-encode 3 urn:epc:id:gdti:0614141.12345.6789 # 2c74257bf460720000001a85 moon run --target wasm cmd/main -- bits-encode urn:epc:tag:sgtin-96:3.0614141.812345.6789 # 001100000111010000100101011110111111011100011001010011100100000000000000000000000001101010000101

    bits-encode 输出恰好 96 个按网络位序排列的 0/1 字符;bits-decode 接受该表示并根据 header 分派。字节接口为 12 字节网络序,不包含读写器协议头或 CRC。

    #API 示例

    CLI 使用 --help 或 -h 查看命令列表,使用 --version 或 -V 查看当前包版本。

    let epc = @moonepc.parse_sgtin_tag_uri(
    "urn:epc:tag:sgtin-96:3.0614141.812345.6789",
    ).unwrap()
    let encoded = epc.encode_hex().unwrap()
    let decoded = @moonepc.decode_sgtin96(encoded).unwrap()
    assert_eq(decoded.to_tag_uri(), epc.to_tag_uri())

    pure-identity URI 不含物理标签 filter。parse_epc96_identity_uri 与各 scheme 的 parse_*_identity_uri 都要求调用者显式提供 filter,避免把身份标识和标签用途混为一谈。SSCC 提供相同的 identity/tag 转换 API。

    对位级适配可用 encode_bit_string 和 decode_epc96_bit_string 在 96 字符二进制表示与 EPC 值之间转换。固定长度读写器记录可用 encode_epc96_records 与 decode_epc96_records 指定记录步长和 EPC 在记录内的偏移;编码时周边字节使用指定填充值,解码时每条记录独立返回结果。批量 URI 解析 API 保留输入顺序,identity 批量解析要求 URI 与 filter 数组长度相同。

    #开发与验证

    moon info moon fmt moon check --target all --deny-warn moon test --target wasm --deny-warn moon test --target js --deny-warn moon test --target native --deny-warn moon run --target wasm cmd/main -- sgtin-decode 3074257bf7194e4000001a85

    GitHub Actions 在 push、pull request 和手动触发时运行全目标检查、WASM/JS/Native 测试,以及 WASM/Native CLI smoke tests。Native 本地测试与运行需安装 C 编译器。

    #验证与限制

    测试包含已知编码向量、六种 scheme 的分区边界 round-trip、scheme 自动分派、identity/tag URI 转换、SSCC 分区往返,以及非法 filter、分区、hex 长度、字符和未知 header 的拒绝路径。测试覆盖不等同于 GS1 全套兼容性认证;目前没有宣称完成官方测试套件或所有 EPC scheme 的互操作验证。

    当前不支持 SGTIN-198、GRAI-170、字母数字型 GIAI、CPI、GID 等其他 scheme,以及 EPC URI 通用解析。遇到未知 header、错误长度、非法字符或字段越界时返回错误,而不是猜测或修复输入。

    #标准、来源与许可

    位布局以 GS1 EPC Tag Data Standard (TDS) 与 Tag Data Translation (TDT) 的公开定义为行为依据;本项目为独立 MoonBit 实现,不复制上游实现代码或生成数据。格式及官方参考链接见 GS1 TDS 与 GS1 TDT。

    代码按 Apache-2.0 许可发布,详见 LICENSE。本项目使用 AI 辅助编写与审阅代码、测试及文档;协议行为仍须以标准来源和可复现测试验证。

    Epc96

    pub enum Epc96 {
    SGTIN96(Sgtin)
    SSCC96(Sscc)
    SGLN96(Sgln)
    GRAI96(Grai)
    GIAI96(Giai)
    GDTI96(Gdti)
    }

    A recognized 96-bit EPC coding scheme supported by this package.

    Epc96::encode_bit_string

    fn Epc96::encode_bit_string(self : Epc96) -> Result[String, EpcError]

    Encode an EPC value as its 96-bit, most-significant-bit-first representation.

    Epc96::encode_bytes

    fn Epc96::encode_bytes(self : Epc96) -> Result[Array[Byte], EpcError]

    Encode any supported value as 12 bytes in network bit order.

    Epc96::encode_hex

    fn Epc96::encode_hex(self : Epc96) -> Result[String, EpcError]

    Encode any supported 96-bit EPC value as hexadecimal tag memory.

    Epc96::inspect

    fn Epc96::inspect(self : Epc96) -> Epc96Info

    Return the partition and header facts used by a validated value.

    Epc96::to_identity

    fn Epc96::to_identity(self : Epc96) -> Epc96Identity

    Preserve the filter alongside the identity URI for lossless storage.

    Epc96::to_identity_uri

    fn Epc96::to_identity_uri(self : Epc96) -> String

    Render the pure-identity URI, which intentionally omits the tag filter.

    Epc96::to_tag_uri

    fn Epc96::to_tag_uri(self : Epc96) -> String

    Render the canonical EPC tag URI, including its scheme-specific filter.

    Epc96Identity

    pub struct Epc96Identity {
    uri : String
    filter : Int
    }

    A pure identity URI paired with the tag filter needed to encode EPC memory.

    Epc96Identity::filter

    fn Epc96Identity::filter(self : Epc96Identity) -> Int

    Read the filter paired with this pure-identity URI.

    Epc96Identity::to_epc96

    fn Epc96Identity::to_epc96(self : Epc96Identity) -> Result[Epc96, EpcError]

    Restore a typed tag value using the explicitly stored filter.

    Epc96Identity::uri

    fn Epc96Identity::uri(self : Epc96Identity) -> String

    Read the pure-identity URI without converting or normalizing it.

    Epc96Info

    pub struct Epc96Info {
    scheme : String
    header : Int
    filter : Int
    partition : Int
    company_prefix_digits : Int
    bit_length : Int
    }

    Stable diagnostic metadata extracted from a validated EPC-96 value.

    EpcError

    pub enum EpcError {
    InvalidPrefix
    InvalidField(String)
    InvalidFilter(Int)
    InvalidPartition(Int)
    InvalidBitLength(Int)
    InvalidByteOffset(Int)
    InvalidHex
    InvalidBinary
    UnsupportedScheme(String)
    }

    Malformed input, unsupported identifiers, and range errors are distinct.

    Gdti

    pub struct Gdti {
    filter : Int
    company_prefix : String
    document_type : String
    serial : String
    }

    Global Document Type Identifier represented as a GDTI-96 tag URI.

    Gdti::encode_hex

    fn Gdti::encode_hex(self : Gdti) -> Result[String, EpcError]

    Encode a GDTI-96 URI as a 96-bit hexadecimal EPC.

    Gdti::to_identity_uri

    fn Gdti::to_identity_uri(self : Gdti) -> String

    Render the GDTI pure-identity URI, omitting the tag filter.

    Gdti::to_tag_uri

    fn Gdti::to_tag_uri(self : Gdti) -> String

    Render canonical GDTI-96 tag URI syntax.

    Giai

    pub struct Giai {
    filter : Int
    company_prefix : String
    asset_reference : String
    }

    Global Individual Asset Identifier represented as a GIAI-96 tag URI.

    Giai::encode_hex

    fn Giai::encode_hex(self : Giai) -> Result[String, EpcError]

    Encode a GIAI-96 URI as a 96-bit hexadecimal EPC.

    Giai::to_identity_uri

    fn Giai::to_identity_uri(self : Giai) -> String

    Render the GIAI pure-identity URI, omitting the tag filter.

    Giai::to_tag_uri

    fn Giai::to_tag_uri(self : Giai) -> String

    Render canonical GIAI-96 tag URI syntax.

    Grai

    pub struct Grai {
    filter : Int
    company_prefix : String
    asset_type : String
    serial : String
    }

    Global Returnable Asset Identifier represented as a GRAI-96 tag URI.

    Grai::encode_hex

    fn Grai::encode_hex(self : Grai) -> Result[String, EpcError]

    Encode a GRAI-96 URI as a 96-bit hexadecimal EPC.

    Grai::to_identity_uri

    fn Grai::to_identity_uri(self : Grai) -> String

    Render the GRAI pure-identity URI, omitting the tag filter.

    Grai::to_tag_uri

    fn Grai::to_tag_uri(self : Grai) -> String

    Render canonical GRAI-96 tag URI syntax.

    Sgln

    pub struct Sgln {
    filter : Int
    company_prefix : String
    location_reference : String
    extension : String
    }

    Global Location Number represented as an SGLN-96 tag URI.

    Sgln::encode_hex

    fn Sgln::encode_hex(self : Sgln) -> Result[String, EpcError]

    Encode an SGLN-96 URI as a 96-bit hexadecimal EPC.

    Sgln::to_identity_uri

    fn Sgln::to_identity_uri(self : Sgln) -> String

    Render the SGLN pure-identity URI, omitting the tag filter.

    Sgln::to_tag_uri

    fn Sgln::to_tag_uri(self : Sgln) -> String

    Render canonical SGLN-96 tag URI syntax.

    Sgtin

    pub struct Sgtin {
    filter : Int
    company_prefix : String
    item_reference : String
    serial : String
    }

    EPC identifier accepted by the SGTIN-96 codec.

    Sgtin::encode_hex

    fn Sgtin::encode_hex(self : Sgtin) -> Result[String, EpcError]

    Encode an SGTIN-96 URI value into three 32-bit big-endian words as hex.

    Sgtin::to_identity_uri

    fn Sgtin::to_identity_uri(self : Sgtin) -> String

    Render the canonical EPC pure-identity form, which intentionally omits the tag filter because filters describe physical-tag usage rather than identity.

    Sgtin::to_tag_uri

    fn Sgtin::to_tag_uri(self : Sgtin) -> String

    Return canonical tag URI syntax while preserving the supplied decimal IDs.

    Sscc

    pub struct Sscc {
    filter : Int
    company_prefix : String
    serial_reference : String
    }

    Serial Shipping Container Code represented as a tag URI.

    Sscc::encode_hex

    fn Sscc::encode_hex(self : Sscc) -> Result[String, EpcError]

    Encode an SSCC-96 tag URI to 96-bit EPC hexadecimal.

    Sscc::to_identity_uri

    fn Sscc::to_identity_uri(self : Sscc) -> String

    Render the SSCC pure-identity URI, which has no physical-tag filter.

    Sscc::to_tag_uri

    fn Sscc::to_tag_uri(self : Sscc) -> String

    Render a normalized SSCC tag URI.

    decode_epc96

    fn decode_epc96(hex : String) -> Result[Epc96, EpcError]

    Detect and decode one of the supported 96-bit EPC headers.

    decode_epc96_batch

    fn decode_epc96_batch(values : Array[String]) -> Array[Result[Epc96, EpcError]]

    Decode a collection independently, preserving each item's result position.

    decode_epc96_bit_string

    fn decode_epc96_bit_string(bits : String) -> Result[Epc96, EpcError]

    Decode one exact 96-character EPC bit string in network bit order.

    decode_epc96_bytes

    fn decode_epc96_bytes(bytes : Array[Byte]) -> Result[Epc96, EpcError]

    Decode exactly 12 network-order bytes by their EPC-96 header.

    decode_epc96_bytes_at

    fn decode_epc96_bytes_at(buffer : Array[Byte], offset : Int) -> Result[Epc96, EpcError]

    Decode 12 bytes at an offset in a larger reader or transport buffer.

    decode_epc96_records

    fn decode_epc96_records(buffer : Array[Byte], start : Int, count : Int, stride : Int, record_offset : Int) -> Result[Array[Result[Epc96, EpcError]], EpcError]

    Decode fixed-stride reader records containing a 12-byte EPC at record_offset. Each record is validated independently, so one invalid header does not hide later rows.

    decode_gdti96

    fn decode_gdti96(hex : String) -> Result[Gdti, EpcError]

    Decode a 96-bit hexadecimal EPC as GDTI-96.

    decode_giai96

    fn decode_giai96(hex : String) -> Result[Giai, EpcError]

    Decode a 96-bit hexadecimal EPC as GIAI-96.

    decode_grai96

    fn decode_grai96(hex : String) -> Result[Grai, EpcError]

    Decode a 96-bit hexadecimal EPC as GRAI-96.

    decode_sgln96

    fn decode_sgln96(hex : String) -> Result[Sgln, EpcError]

    Decode a 96-bit hexadecimal EPC as SGLN-96.

    decode_sgtin96

    fn decode_sgtin96(hex : String) -> Result[Sgtin, EpcError]

    Decode a 96-bit hex value. Invalid headers and partition codes are rejected.

    decode_sscc96

    fn decode_sscc96(hex : String) -> Result[Sscc, EpcError]

    Decode a 96-bit EPC hex value as SSCC-96.

    encode_epc96_batch

    fn encode_epc96_batch(values : Array[Epc96]) -> Array[Result[String, EpcError]]

    Encode a collection independently, preserving each item's result position.

    encode_epc96_bytes_into

    fn encode_epc96_bytes_into(value : Epc96, buffer : Array[Byte], offset : Int) -> Result[Unit, EpcError]

    Write 12 network-order bytes into an existing reader output buffer.

    encode_epc96_records

    fn encode_epc96_records(values : Array[Epc96], stride : Int, record_offset : Int, fill : Byte) -> Result[Array[Byte], EpcError]

    Encode values into a fixed-stride reader buffer, filling non-EPC bytes uniformly.

    parse_epc96_identity_uri

    fn parse_epc96_identity_uri(uri : String, filter : Int) -> Result[Epc96, EpcError]

    Parse one pure-identity URI using a caller-selected tag filter.

    parse_epc96_identity_uri_batch

    fn parse_epc96_identity_uri_batch(uris : Array[String], filters : Array[Int]) -> Result[Array[Result[Epc96, EpcError]], EpcError]

    Parse identity URIs with a matching per-row filter; lengths must agree.

    parse_epc96_tag_uri

    fn parse_epc96_tag_uri(uri : String) -> Result[Epc96, EpcError]

    Parse one supported EPC tag URI without requiring the caller to select a scheme.

    parse_epc96_tag_uri_batch

    fn parse_epc96_tag_uri_batch(uris : Array[String]) -> Array[Result[Epc96, EpcError]]

    Parse EPC tag URIs independently and retain the result position for each row.

    parse_gdti_identity_uri

    fn parse_gdti_identity_uri(uri : String, filter : Int) -> Result[Gdti, EpcError]

    Parse a GDTI pure-identity URI and supply its omitted tag filter.

    parse_gdti_tag_uri

    fn parse_gdti_tag_uri(uri : String) -> Result[Gdti, EpcError]

    Parse a numeric urn:epc:tag:gdti-96:F.CompanyPrefix.DocumentType.Serial URI.

    parse_giai_identity_uri

    fn parse_giai_identity_uri(uri : String, filter : Int) -> Result[Giai, EpcError]

    Parse a GIAI pure-identity URI and supply its omitted tag filter.

    parse_giai_tag_uri

    fn parse_giai_tag_uri(uri : String) -> Result[Giai, EpcError]

    Parse a numeric urn:epc:tag:giai-96:F.CompanyPrefix.AssetReference URI.

    parse_grai_identity_uri

    fn parse_grai_identity_uri(uri : String, filter : Int) -> Result[Grai, EpcError]

    Parse a GRAI pure-identity URI and supply its omitted tag filter.

    parse_grai_tag_uri

    fn parse_grai_tag_uri(uri : String) -> Result[Grai, EpcError]

    Parse a numeric urn:epc:tag:grai-96:F.CompanyPrefix.AssetType.Serial URI.

    parse_sgln_identity_uri

    fn parse_sgln_identity_uri(uri : String, filter : Int) -> Result[Sgln, EpcError]

    Parse an SGLN pure-identity URI and supply its omitted tag filter.

    parse_sgln_tag_uri

    fn parse_sgln_tag_uri(uri : String) -> Result[Sgln, EpcError]

    Parse urn:epc:tag:sgln-96:F.CompanyPrefix.LocationReference.Extension.

    parse_sgtin_identity_uri

    fn parse_sgtin_identity_uri(uri : String, filter : Int) -> Result[Sgtin, EpcError]

    Parse an SGTIN pure-identity URI with an explicit physical-tag filter.

    parse_sgtin_tag_uri

    fn parse_sgtin_tag_uri(uri : String) -> Result[Sgtin, EpcError]

    Parse strict urn:epc:tag:sgtin-96:F.C.I.S syntax and check all fields.

    parse_sscc_identity_uri

    fn parse_sscc_identity_uri(uri : String, filter : Int) -> Result[Sscc, EpcError]

    Parse an SSCC pure-identity URI and supply the omitted tag filter explicitly.

    parse_sscc_tag_uri

    fn parse_sscc_tag_uri(uri : String) -> Result[Sscc, EpcError]

    Parse urn:epc:tag:sscc-96:F.CompanyPrefix.SerialReference.

    Source Files