Defines the behaviour for ShortUUID-compatible modules.
Modules created with ShortUUID.Builder implement this behaviour and meet
the contract below. Hand-written implementations should meet it too, so that
code using them (for example an Ecto type) can rely on it.
Contract
encode/1accepts a UUID string in any of the formats listed in the README (with or without hyphens, in curly braces or as aurn:uuid:URN), in any letter case. It returns{:ok, short_id}, or{:error, message}for anything else.decode/1returns{:ok, uuid}with a lowercase, hyphenated UUID, and accepts exactly the strings thatencode/1produces: every UUID has one short ID, and decoding it and encoding the result gives the same short ID. Any other input returns{:error, message}.encode/1anddecode/1never raise. Any term, including non-strings and invalid UTF-8, returns{:error, message}instead.encode!/1anddecode!/1return the value directly and raise anArgumentErrorwhere the non-bang version returns an error.- The optional
__shortuuid__/0describes the encoder. Modules created withShortUUID.Builderdefine it, withcanonical: true.
Required Callbacks
encode/1- Encodes a standard UUID into a shorter stringencode!/1- Encodes a UUID, raising an exception on invalid inputdecode/1- Decodes a shortened UUID string back into standard UUID formatdecode!/1- Decodes a shortened UUID, raising an exception on invalid input
Summary
Types
Encoder description returned by __shortuuid__/0.
Callbacks
Describes the encoder. Optional; modules created with ShortUUID.Builder
define it.
Decodes a shortened UUID string back into standard UUID format.
Decodes a shortened UUID string back into standard UUID format. Raises an ArgumentError if the input is invalid.
Encodes a UUID string into a shorter string representation.
Encodes a UUID string into a shorter string representation. Raises an ArgumentError if the input is invalid.
Types
@type info() :: %{ alphabet: String.t(), base: pos_integer(), encoded_length: pos_integer(), canonical: boolean() }
Encoder description returned by __shortuuid__/0.
Callbacks
@callback __shortuuid__() :: info()
Describes the encoder. Optional; modules created with ShortUUID.Builder
define it.
alphabet- the alphabet as a single stringbase- the number of characters in the alphabetencoded_length- the number of alphabet characters in every short IDcanonical-trueif every UUID has exactly one short ID anddecode/1accepts only those IDs, so encoding a decoded ID always gives back the same ID. Code that re-encodes decoded IDs to reject other spellings can skip that step when this istrue.
To check for it, load the module first:
Code.ensure_loaded?(module) and function_exported?(module, :__shortuuid__, 0)
Decodes a shortened UUID string back into standard UUID format.
Parameters
string- A shortened UUID string
Returns
{:ok, uuid}- Successfully decoded UUID{:error, message}- Error with descriptive message
Decodes a shortened UUID string back into standard UUID format. Raises an ArgumentError if the input is invalid.
Parameters
string- A shortened UUID string
Returns
uuid- Successfully decoded UUID
Raises
ArgumentError- If the input is invalid
Encodes a UUID string into a shorter string representation.
Parameters
uuid- A UUID string in one of the supported formats
Returns
{:ok, encoded}- Successfully encoded string{:error, message}- Error with descriptive message
Encodes a UUID string into a shorter string representation. Raises an ArgumentError if the input is invalid.
Parameters
uuid- A UUID string in one of the supported formats
Returns
encoded- Successfully encoded string
Raises
ArgumentError- If the input is invalid