youid/uuid
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.
Types
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(BitArray)
}
Constructors
-
RandomClockSeq
Will be generated randomly.
-
CustomClockSeq(BitArray)
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
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_bit_array(bit_array: BitArray) -> Result(Uuid, Nil)
Attemts to convert a bit array to a UUID. Will fail if the bit array is not 16 bytes long or has an invalid version.
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.
This is only relevant to V1 and V7 UUIDs.
Value is the number of micro seconds since Unix Epoch.
pub fn time_posix_millisec(uuid: Uuid) -> Int
Determine the time a UUID was created with Unix Epoch This is only relevant to V1 and V7 UUIDs Value is the number of milli seconds since Unix Epoch
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: BitArray) -> Result(Uuid, Nil)
Generates a version 3 (name-based, md5 hashed) UUID. Name must be a valid sequence of bytes
pub fn v5(namespace: Uuid, name: BitArray) -> Result(Uuid, Nil)
Generates a version 5 (name-based, sha1 hashed) UUID. name must be a valid sequence of bytes
pub fn v7_from_millisec(timestamp: Int) -> Uuid
Creates a version 7 UUID from a specific UNIX timestamp. Integer should be milliseconds from UNIX epoch.
pub fn v7_string() -> String
Convenience function for quickly creating a timestamp-based version 7 UUID