glixir/registry

Type-safe, phantom-typed Registry for Gleam/Elixir interop

This module provides a phantom-typed interface to Elixir Registry. All registries are parameterized by their key_type and message_type, so misuse is a compile error, not a runtime surprise.

Types

pub type KeyEncoder(key_type) =
  fn(key_type) -> String

Opaque phantom-typed registry

pub opaque type Registry(key_type, message_type)

Errors from registry operations

pub type RegistryError {
  StartError(String)
  RegisterError(String)
  LookupError(String)
  UnregisterError(String)
  AlreadyExists
  NotFound
}

Constructors

  • StartError(String)
  • RegisterError(String)
  • LookupError(String)
  • UnregisterError(String)
  • AlreadyExists
  • NotFound

Registry configuration options

pub type RegistryKeys {
  Unique
  Duplicate
}

Constructors

  • Unique
  • Duplicate
pub type RegistryLookupResult(message) {
  RegistryLookupOk(subject: process.Subject(message))
  RegistryLookupNotFound
  RegistryLookupError(reason: dynamic.Dynamic)
}

Constructors

pub type RegistryRegisterResult {
  RegistryRegisterOk
  RegistryRegisterError(reason: dynamic.Dynamic)
}

Constructors

pub type RegistryStartResult {
  RegistryStartOk(pid: process.Pid)
  RegistryStartError(reason: dynamic.Dynamic)
}

Constructors

pub type RegistryUnregisterResult {
  RegistryUnregisterOk
  RegistryUnregisterError(reason: dynamic.Dynamic)
}

Constructors

Values

pub fn atom_key_encoder(key: atom.Atom) -> String

Encoder for Atom keys (most common case)

pub fn int_key_encoder(key: Int) -> String

Encoder for Int keys

pub fn lookup_subject(
  registry_name: atom.Atom,
  key: key_type,
  encode_key: fn(key_type) -> String,
) -> Result(process.Subject(message_type), RegistryError)

Look up a Subject by typed key in the phantom-typed registry

pub fn register_subject(
  registry_name: atom.Atom,
  key: key_type,
  subject: process.Subject(message_type),
  encode_key: fn(key_type) -> String,
) -> Result(Nil, RegistryError)

Register a Subject with a typed key in the phantom-typed registry

pub fn session_key_encoder(session_id: String) -> String

Encoder for session keys (common pattern)

pub fn start_registry(
  name: atom.Atom,
  keys: RegistryKeys,
) -> Result(Registry(key_type, message_type), RegistryError)

Start a phantom-typed registry with bounded key and message types

pub fn start_unique_registry(
  name: atom.Atom,
) -> Result(Registry(key_type, message_type), RegistryError)

Start a unique key registry (most common use case)

pub fn string_key_encoder(key: String) -> String

Encoder for String keys

pub fn unregister_subject(
  registry_name: atom.Atom,
  key: key_type,
  encode_key: fn(key_type) -> String,
) -> Result(Nil, RegistryError)

Unregister a key from the phantom-typed registry

pub fn user_id_encoder(user_id: Int) -> String

Encoder for user ID keys (common pattern)

Search Document