dream_uuid
UUIDs for Gleam: all of RFC 9562, on Erlang and on JavaScript.
Each 128-bit value is a valid Uuid, so the nil UUID, the max UUID and
each version from 0 to 15 can go in and come out again. The module makes
v1, v3, v4, v5, v6, v7 and v8 UUIDs, reads and writes the canonical text
form and the URN form, and reads the fields of each version.
No value above 2^53 is ever a JavaScript number. Thus a field that has more than 53 bits, for example the 60-bit timestamp of v1, goes in as a bit array, and the time of a UUID comes out as seconds and nanoseconds.
Types
The fields that a builder can refuse.
pub type Field {
GregorianTimestamp
UnixMilliseconds
ClockSequence
Node
RandA
RandB
}
Constructors
-
GregorianTimestampThe 60-bit count of 100 ns steps from 1582-10-15, of v1 and v6.
-
UnixMillisecondsThe 48-bit Unix time in milliseconds of v7.
-
ClockSequenceThe 14-bit clock sequence of v1 and v6.
-
NodeThe 48-bit node of v1 and v6.
-
RandAThe 12 bits after the version of v7.
-
RandBThe 62 bits after the variant of v7.
Why text is not a UUID.
pub type ParseError {
WrongLength(length: Int)
InvalidCharacter(position: Int, character: String)
MissingUrnPrefix
}
Constructors
-
WrongLength(length: Int)The text does not have the necessary count of characters: 36 for
from_string, 45 forfrom_urn.lengthis the count that the text has. -
InvalidCharacter(position: Int, character: String)The character at
position(from 0, in the text that the caller gave) is not permitted there. -
MissingUrnPrefixfrom_urngot text that does not start withurn:uuid:, in any case.
A bit array has the wrong size. bits is the size that the caller gave.
For a name that is not whole bytes, expected_bits is the next count
of whole bytes, in bits.
pub type SizeError {
SizeError(expected_bits: Int, bits: Int)
}
Constructors
-
SizeError(expected_bits: Int, bits: Int)
A UUID: 128 bits. Each 128-bit value is a valid Uuid.
The value holds the 128 bits as four words of 32 bits, from the left. Each word is below 2^32, so it is exact on Erlang and on JavaScript.
pub opaque type Uuid
The variant field (RFC 9562 section 4.1, table 1).
pub type Variant {
Ncs
Rfc
Microsoft
Future
}
Constructors
-
Ncs0xxx: NCS backward compatibility. The nil UUID is in this range. -
Rfc10xx: the variant of RFC 9562. -
Microsoft110x: Microsoft backward compatibility. -
Future111x: reserved. The max UUID is in this range.
The version field: bits 48 to 51. Unknown holds the nibble for 0 and
for 9 to 15. The nil UUID gives Unknown(0), and the max UUID gives
Unknown(15). version never gives Unknown for a nibble from 1 to 8.
pub type Version {
V1
V2
V3
V4
V5
V6
V7
V8
Unknown(nibble: Int)
}
Constructors
-
V1Gregorian time, clock sequence and node.
-
V2DCE Security. RFC 9562 does not define its layout.
-
V3The MD5 hash of a namespace and a name.
-
V4Random.
-
V5The SHA-1 hash of a namespace and a name.
-
V6The fields of v1 in an order that sorts by time.
-
V7Unix time in milliseconds, then random bits.
-
V8Custom: 122 bits that the application defines.
-
Unknown(nibble: Int)A nibble that RFC 9562 does not give a version: 0 or 9 to 15.
Values
pub fn clock_sequence(uuid: Uuid) -> option.Option(Int)
The 14-bit clock sequence of a v1 or v6 UUID of the RFC variant.
None for each other UUID.
pub fn compare(a: Uuid, b: Uuid) -> order.Order
Order two UUIDs by their bytes, as unsigned values, from the left.
PostgreSQL orders a uuid column the same way.
pub fn from_bit_array(bytes: BitArray) -> Result(Uuid, SizeError)
Make a Uuid from 16 bytes. Each 16-byte value is accepted. Each other
size gives a SizeError.
pub fn from_string(text: String) -> Result(Uuid, ParseError)
Read the canonical text form: 8-4-4-4-12 hexadecimal digits, in uppercase, lowercase or mixed case. Each other form is refused, also braces, the URN form and 32 digits with no hyphens.
pub fn from_urn(text: String) -> Result(Uuid, ParseError)
Read the URN form of RFC 9562 section 4: urn:uuid:, then the
canonical form. The prefix is not case-sensitive (RFC 8141). A position
in an error counts from the start of the URN.
pub const namespace_dns: Uuid
The DNS namespace: 6ba7b810-9dad-11d1-80b4-00c04fd430c8.
pub const namespace_oid: Uuid
The OID namespace: 6ba7b812-9dad-11d1-80b4-00c04fd430c8.
pub const namespace_url: Uuid
The URL namespace: 6ba7b811-9dad-11d1-80b4-00c04fd430c8.
pub const namespace_x500: Uuid
The X.500 namespace: 6ba7b814-9dad-11d1-80b4-00c04fd430c8.
pub fn node(uuid: Uuid) -> option.Option(Int)
The 48-bit node of a v1 or v6 UUID of the RFC variant. None for each
other UUID.
pub fn to_bit_array(uuid: Uuid) -> BitArray
The 16 bytes of a UUID, in network byte order.
pub fn to_string(uuid: Uuid) -> String
The canonical text form, in lowercase:
f81d4fae-7dec-11d0-a765-00a0c91e6bf6.
pub fn to_urn(uuid: Uuid) -> String
The URN form, in lowercase:
urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6.
pub fn unix_milliseconds(uuid: Uuid) -> option.Option(Int)
The time in a v1, v6 or v7 UUID of the RFC variant, in milliseconds
from the Unix epoch, rounded down. A v1 or v6 time before 1970 gives a
negative number. None for each other UUID.
pub fn unix_seconds_and_nanoseconds(
uuid: Uuid,
) -> option.Option(#(Int, Int))
The time in a v1, v6 or v7 UUID of the RFC variant, as whole seconds
from the Unix epoch and the nanoseconds after them (0 to 999,999,999).
v1 and v6 have steps of 100 ns, and v7 has steps of 1 ms. The pair is
the input of timestamp.from_unix_seconds_and_nanoseconds of
gleam_time. None for each other UUID.
pub fn v1() -> Uuid
A new v1 UUID: the Gregorian time, a clock sequence and a node.
The clock sequence and the node are random. They are made one time when
the module loads, and the node has the multicast bit set (RFC 9562
section 6.10). The package never reads a MAC address. The time comes
from the counter that v6 also uses, so each v1 and v6 on one BEAM
node, or in one JavaScript realm, has a later time than the one before.
pub fn v1_from(
timestamp timestamp: BitArray,
clock_sequence clock_sequence: Int,
node node: Int,
) -> Result(Uuid, FieldError)
A v1 UUID from its three fields. timestamp is the count of 100 ns
steps from 1582-10-15, as 60 bits, for example
<<0x1EC9414:28, 0xC232AB00:32>>. A value above 2^53 is not exact in a
JavaScript number, so write it as parts of 32 bits or less.
clock_sequence has 14 bits, and node has 48 bits.
pub fn v3(namespace: Uuid, name: String) -> Uuid
A v3 UUID: the MD5 hash of the namespace and the UTF-8 bytes of the name.
pub fn v3_bytes(
namespace: Uuid,
name: BitArray,
) -> Result(Uuid, SizeError)
A v3 UUID of a name that is bytes. A name that is not whole bytes gives
a SizeError.
pub fn v4_from(bytes: BitArray) -> Result(Uuid, SizeError)
A v4 UUID from 16 bytes of the caller. The version and variant bits are set, and the other 122 bits stay.
pub fn v5(namespace: Uuid, name: String) -> Uuid
A v5 UUID: the SHA-1 hash of the namespace and the UTF-8 bytes of the name.
pub fn v5_bytes(
namespace: Uuid,
name: BitArray,
) -> Result(Uuid, SizeError)
A v5 UUID of a name that is bytes. A name that is not whole bytes gives
a SizeError.
pub fn v6() -> Uuid
A new v6 UUID: the fields of v1 in an order that sorts by time.
Each v6 UUID gets a new random clock sequence and a new random node
with the multicast bit set (RFC 9562 section 5.6). The order comes from
the time: each v1 and v6 on one BEAM node, or in one JavaScript
realm, has a later time than the one before.
pub fn v6_from(
timestamp timestamp: BitArray,
clock_sequence clock_sequence: Int,
node node: Int,
) -> Result(Uuid, FieldError)
A v6 UUID from its three fields. The arguments are those of v1_from.
pub fn v7() -> Uuid
A new v7 UUID. Each call on one BEAM node, or in one JavaScript realm, returns a UUID that sorts after each UUID that an earlier call returned there.
The layout is method 3 of RFC 9562 section 6.2: 48 bits of Unix milliseconds, a 12-bit fraction of the millisecond, and 62 random bits. A counter keeps the 60 time bits ahead of the last UUID. Above 4,096 UUIDs in one millisecond, or after the clock goes back, the time bits go ahead of the clock by one step (1/4096 ms) for each UUID.
pub fn v7_at(unix_milliseconds: Int) -> Result(Uuid, FieldError)
A v7 UUID for a given time, with 74 random bits. Use it to make IDs for old records. It gives no order between two calls with the same time. The time must be from 0 to 2^48 - 1.
pub fn v7_from(
unix_milliseconds unix_milliseconds: Int,
rand_a rand_a: Int,
rand_b rand_b: BitArray,
) -> Result(Uuid, FieldError)
A v7 UUID from its three fields. unix_milliseconds has 48 bits and
rand_a has 12 bits. rand_b is 62 bits, for example
<<0x18C4DC0C:30, 0x0C07398F:32>>.