libero/codegen

Values

pub fn emit_typed_decoders(
  discovered: List(walker.DiscoveredType),
) -> String

Emit a JS string with one decoder function per discovered type and a decode_msg_from_server entry point. Does not write to disk - exposed so tests can assert on the output without filesystem I/O.

pub fn extract_dir(path: String) -> String
pub fn module_to_mjs_path(module_path: String) -> String

Convert a Gleam module path like “shared/discount” to its compiled .mjs bundle path “shared/shared/discount.mjs”. The first segment is the package name (Gleam convention) and is repeated because the bundle layout is <package>/<module_path>.mjs.

pub fn write_atoms(
  config config: config.Config,
  discovered discovered: List(walker.DiscoveredType),
) -> Result(Nil, gen_error.GenError)

Generate an Erlang FFI file that pre-registers all constructor atoms discovered by the type graph walker, plus framework atoms used by libero’s wire protocol. Calling ensure/0 from this module creates the atoms in the BEAM atom table so that binary_to_term([safe]) can decode client ETF payloads without rejecting unknown atoms.

pub fn write_config(
  config config: config.Config,
) -> Result(Nil, gen_error.GenError)
pub fn write_decoders_ffi(
  config config: config.Config,
  discovered discovered: List(walker.DiscoveredType),
) -> Result(Nil, gen_error.GenError)

Write the typed decoders FFI file for the consumer.

pub fn write_decoders_gleam(
  config config: config.Config,
) -> Result(Nil, gen_error.GenError)

Write the Gleam wrapper for the typed decoder FFI. Surfaces decode_msg_from_server from the generated JS FFI file to Gleam callers. JS-only - no Erlang target fallback.

pub fn write_dispatch(
  message_modules message_modules: List(scanner.MessageModule),
  server_generated server_generated: String,
  atoms_module atoms_module: String,
  shared_state_module shared_state_module: String,
  app_error_module app_error_module: String,
) -> Result(Nil, gen_error.GenError)

Generate the server dispatch module at server_generated/dispatch.gleam. The dispatch module decodes incoming wire calls and routes them by module name to the appropriate handler.

pub fn write_if_missing(
  path path: String,
  content content: String,
) -> Result(Nil, gen_error.GenError)

Write content to a file, skipping if the file already exists. Used for scaffolded files that users may customize after generation.

pub fn write_main(
  app_name app_name: String,
  port port: Int,
  server_generated server_generated: String,
  shared_state_module shared_state_module: String,
  js_client_names js_client_names: List(String),
) -> Result(Nil, gen_error.GenError)

Generate the server entry point at src/<app_name>.gleam.

pub fn write_push_wrappers(
  message_modules message_modules: List(scanner.MessageModule),
  server_generated server_generated: String,
) -> Result(Nil, List(gen_error.GenError))

Generate per-module push wrapper files under server_generated/. For each message module with has_msg_from_server == True, generates a <module_name>.gleam file with send_to_client and send_to_clients functions that bake in the module string.

pub fn write_send_functions(
  message_modules message_modules: List(scanner.MessageModule),
  client_generated client_generated: String,
) -> Result(Nil, List(gen_error.GenError))

Generate per-module send function files under client_generated/. For each message module with has_msg_from_client == True, generates a <module_name>.gleam file with a send_to_server function that wraps rpc.send.

pub fn write_ssr_flags(
  client_generated client_generated: String,
) -> Result(Nil, gen_error.GenError)

Generate the client-side SSR flags reader (gleam + mjs). Produces a read_flags() function that reads window.LIBERO_FLAGS and returns it as a Dynamic value for ssr.decode_flags().

pub fn write_websocket(
  server_generated server_generated: String,
  shared_state_module shared_state_module: String,
) -> Result(Nil, gen_error.GenError)

Generate the server websocket handler at server_generated/websocket.gleam. Also writes the Erlang FFI for decoding push messages.

Search Document