howdy/uuid

Disclaimer: This is a very slightly modified version of gleam_uuid. The reason this exists is because gleam_uuid seems to have been abandonded, and the howdy webserver needed a uuid parser. If the auther of gleam_uuid picks up the project then this library will be depreciated in favour of that project.

Spec conformant UUID v1, v3, v4, and v5 generation.

Spec conformant UUID decoding for all versions and variants.

Spec: https://www.ietf.org/rfc/rfc4122.txt

Wikipedia: https://en.wikipedia.org/wiki/uuid

Unless you have a specific reason otherwise, you probably either want the random v4 or the time-based v1 version.

Quick Usage:

import howdy/uuid

// Generation
> uuid.v4_string()
"f7e321c7-4a4b-4287-a8b8-1ae35b5538ce"

// Decoding
> "f7e321c7-4a4b-4287-a8b8-1ae35b5538ce"
    |> uuid.from_string()
    |> result.map(uuid.version)
Ok(uuid.V4)

Types

pub type Encryption {
  Hwaddr
  Md5
  Sha
}

Constructors

  • Hwaddr
  • Md5
  • Sha

Supported string formats

pub type Format {
  String
  Hex
  Urn
}

Constructors

  • String

    Standad hex string with dashes

  • Hex

    Hex string with no dashes

  • Urn

    Standard hex string with dashes prepended with “urn:uuid:”

Opaque type for holding onto a UUID. Opaque so you know that if you have a UUID it is valid.

pub opaque type UUID

How to generate the clock sequence for a V1 UUID

pub type V1ClockSeq {
  RandomClockSeq
  CustomClockSeq(BitString)
}

Constructors

  • RandomClockSeq

    Will be generated randomly.

  • CustomClockSeq(BitString)

    Will be the provided bit string, must be exactly 14 bits.

How to generate the node for a V1 UUID.

pub type V1Node {
  DefaultNode
  RandomNode
  CustomNode(String)
}

Constructors

  • DefaultNode

    Will first attempt to use the network cards MAC address, then fall back to random

  • RandomNode

    Will be random

  • CustomNode(String)

    Will be the provided sting, must be 12 characters long and valid hex

Possible UUID variants. This library only produces Rfc4122 variant UUIDs but can decode all variants

pub type Variant {
  ReservedFuture
  ReservedMicrosoft
  ReservedNcs
  Rfc4122
}

Constructors

  • ReservedFuture
  • ReservedMicrosoft
  • ReservedNcs
  • Rfc4122

Possible UUID versions. This library only creates V1, V3, V4 and V5 UUIDs but can decode all versions

pub type Version {
  V1
  V2
  V3
  V4
  V5
  VUnknown
}

Constructors

  • V1
  • V2
  • V3
  • V4
  • V5
  • VUnknown

Functions

pub fn clock_sequence(uuid: UUID) -> Int

Determine the clock sequence of a UUID This is only relevant to a V1 UUID

pub fn dns_uuid() -> UUID

dns namespace UUID provided by the spec, only useful for v3 and v5

pub fn format(uuid: UUID, format: Format) -> String

Convert a UUID to one of the supported string formats

pub fn from_string(in: String) -> Result(UUID, Nil)

Attempt to decode a UUID from a string. Supports strings formatted in the same ways this library will output them. Hex with dashes, hex without dashes and hex with or without dashes prepended with “urn:uuid:”

pub fn node(uuid: UUID) -> String

Determine the node of a UUID This is only relevant to a V1 UUID

pub fn oid_uuid() -> UUID

oid namespace UUID provided by the spec, only useful for v3 and v5

pub fn time(uuid: UUID) -> Int

Determine the time a UUID was created with Gregorian Epoch This is only relevant to a V1 UUID UUID’s use 15 Oct 1582 as Epoch and time is measured in 100ns intervals. This value is useful for comparing V1 UUIDs but not so much for telling what time a UUID was created. See time_posix_microsec and clock_sequence

pub fn time_posix_microsec(uuid: UUID) -> Int

Determine the time a UUID was created with Unix Epoch This is only relevant to a V1 UUID Value is the number of micro seconds since Unix Epoch

pub fn to_string(uuid: UUID) -> String

Convert a UUID to a standard string

pub fn url_uuid() -> UUID

url namespace UUID provided by the spec, only useful for v3 and v5

pub fn v1() -> UUID

Create a V1 (time-based) UUID with default node and random clock sequence.

pub fn v1_custom(node: V1Node, clock_seq: V1ClockSeq) -> Result(
  UUID,
  Nil,
)

Create a V1 (time-based) UUID with custom node and clock sequence.

pub fn v1_string() -> String

Convenience for quickly creating a time-based UUID String with default settings.

pub fn v3(namespace: UUID, name: BitString) -> Result(UUID, Nil)

Generates a version 3 (name-based, md5 hashed) UUID. Name must be a valid sequence of bytes

pub fn v4() -> UUID

Generates a version 4 (random) UUID.

pub fn v4_string() -> String

Convenience for quickly creating a random UUID String

pub fn v5(namespace: UUID, name: BitString) -> Result(UUID, Nil)

Generates a version 5 (name-based, sha1 hashed) UUID. name must be a valid sequence of bytes

pub fn variant(uuid: UUID) -> Variant

Determine the Variant of a UUID

pub fn version(uuid: UUID) -> Version

Determine the Version of a UUID

pub fn x500_uuid() -> UUID

x500 namespace UUID provided by the spec, only useful for v3 and v5