Encodes and decodes Guild Wars 2 chat codes.
Chat codes are the strings Guild Wars 2 places in chat when you link
game data, such as items or wardrobe templates. They are wrapped in the
familiar [&...] format and contain Base64-encoded binary payloads.
This module provides the public entry point for converting between those chat code strings and typed Elixir structs.
Supported chat codes
The library currently supports:
GW2.ChatCode.Itemfor item links.GW2.ChatCode.WardrobeTemplatefor wardrobe template links.
Guild Wars 2 has other chat code types, such as coins, maps, skills, traits, recipes, outfits, build templates, achievements, and travel templates. Those types are not implemented yet. Decoding an unsupported chat code type returns an error tuple.
Encoding
Build one of the supported structs and pass it to encode/1:
iex> GW2.ChatCode.encode(%GW2.ChatCode.Item{id: 43766, quantity: 9})
{:ok, "[&Agn2qgAA]"}Decoding
Pass a chat code string to decode/1 to get a typed struct back:
iex> GW2.ChatCode.decode("[&Agn2qgAA]")
{:ok, %GW2.ChatCode.Item{id: 43766, quantity: 9, skin_id: nil, upgrade_ids: []}}Unknown, malformed, or unsupported chat codes return an error tuple instead of raising.
Common error reasons are:
:invalid_codefor invalid wrappers, Base64, unsupported chat code types, or malformed payloads.:quantity_out_of_rangewhen encoding an item quantity outside0..255.:invalid_idwhen encoding an id that cannot fit in the chat code format.:invalid_dyeswhen encoding a wardrobe skin with an invalid dye list.
Summary
Types
A supported decoded chat code struct.
Functions
Decodes a chat code string into a structured value.
Encodes a supported chat code struct into a Guild Wars 2 chat code string.
Types
@type parsed_struct() :: GW2.ChatCode.Item.t() | GW2.ChatCode.WardrobeTemplate.t()
A supported decoded chat code struct.
Functions
@spec decode(binary()) :: {:ok, parsed_struct()} | {:error, atom()}
Decodes a chat code string into a structured value.
Returns {:ok, struct} for supported chat code types. Invalid Base64,
missing wrappers, unknown chat code types, and malformed payloads return
an error tuple.
Examples
iex> GW2.ChatCode.decode("[&AgGqtgAA]")
{:ok, %GW2.ChatCode.Item{id: 46762, quantity: 1, skin_id: nil, upgrade_ids: []}}
iex> GW2.ChatCode.decode("not a chat code")
{:error, :invalid_code}
@spec encode(GW2.ChatCode.Item.t()) :: {:ok, binary()} | {:error, atom()}
@spec encode(GW2.ChatCode.WardrobeTemplate.t()) :: {:ok, binary()} | {:error, atom()}
@spec encode(parsed_struct()) :: {:ok, binary()} | {:error, atom()}
Encodes a supported chat code struct into a Guild Wars 2 chat code string.
Returns {:ok, code} when the struct can be encoded, or {:error, reason}
when one of its fields cannot be represented in a chat code payload.
Examples
iex> GW2.ChatCode.encode(%GW2.ChatCode.Item{id: 46762})
{:ok, "[&AgGqtgAA]"}
iex> GW2.ChatCode.encode(%GW2.ChatCode.Item{id: 1, quantity: 256})
{:error, :quantity_out_of_range}