std.uuid

std.uuid generates, parses, and validates UUIDs. All functions are pure
compute (no I/O), so they stay available in guarded engines.

Reference

uuid4()                  # random RFC 4122 version-4 UUID string
uuid7()                  # time-ordered RFC 9562 version-7 UUID string
uuid3(namespace, name)   # deterministic MD5 name-based UUID
uuid5(namespace, name)   # deterministic SHA-1 name-based UUID
is_uuid(s)               # true when s is a well-formed UUID string
                           # (8-4-4-4-12 hex, either case, any version incl. nil)
nil_uuid()               # the nil UUID (all zeros)
version(uuid)            # version nibble (throws INVALID_ARGUMENT unless well-formed)
to_bytes(uuid)           # 16 raw bytes (throws INVALID_ARGUMENT unless well-formed)
from_bytes(bytes)        # format exactly 16 bytes as a UUID string

namespace is "dns", "url", "oid", "x500", or a UUID string.

Examples

print(std.uuid.uuid4())  # e.g. 550e8400-e29b-41d4-a716-446655440000

# time-ordered ids sort by creation time — the default for DB keys
var ids = []
for (i : std.seq.range(3)) {
  ids:insert(std.uuid.uuid7())
}
print(ids)

# deterministic ids: same inputs always give the same UUID
var user_id = std.uuid.uuid5("dns", "alice@example.com")

# validate and inspect
print(std.uuid.is_uuid(user_id))       # true
print(std.uuid.is_uuid("nope"))        # false
print(std.uuid.version(user_id))       # 5

# compact storage: 16 bytes instead of 36 chars
var raw = std.uuid.to_bytes(user_id)
print(len(raw))                        # 16
print(std.uuid.from_bytes(raw) == user_id)  # true

# use as a unique filename or key
var tmp = "/tmp/" + std.uuid.uuid4() + ".json"
std.file.write(tmp, std.json.stringify({id: std.uuid.uuid4()}))

See also

  • std.file — temp file helpers (temp) as an alternative for unique paths