API Reference
Every module and function in the standard library.
Language modules ship with the core prelude; server modules require @prelude(server).
Language Modules
Core
List
List operations.
| Function | Signature | Description |
|---|---|---|
length | fn length(List<T>) -> Int | Number of elements |
head | fn head(List<T>) -> Option<T> | First element |
tail | fn tail(List<T>) -> List<T> | All except first |
get | fn get(List<T>, Int) -> Option<T> | Element at index |
get_or | fn get_or(List<T>, Int, T) -> T | Element at index with default |
set | fn set(List<T>, Int, T) -> List<T> | Replace element at index |
slice | fn slice(List<T>, Int, Int) -> List<T> | Sub-list (start, end exclusive) |
fill | fn fill(Int, T) -> List<T> | Create list filled with value |
reverse | fn reverse(List<T>) -> List<T> | Reverse order |
sort | fn sort(List<T>) -> List<T> | Sort ascending |
concat | fn concat(List<T>, List<T>) -> List<T> | Concatenate two lists |
contains | fn contains(List<T>, T) -> Bool | Check membership |
map | fn map(List<T>, (T) -> U) -> List<U> | Apply function to each |
filter | fn filter(List<T>, (T) -> Bool) -> List<T> | Keep matching elements |
fold | fn fold(List<T>, U, (U, T) -> U) -> U | Reduce to single value |
find | fn find(List<T>, (T) -> Bool) -> Option<T> | First matching element |
String
String operations.
| Function | Signature | Description |
|---|---|---|
length | fn length(String) -> Int | Character count |
contains | fn contains(String, String) -> Bool | Has substring? |
starts_with | fn starts_with(String, String) -> Bool | Starts with prefix? |
ends_with | fn ends_with(String, String) -> Bool | Ends with suffix? |
to_upper | fn to_upper(String) -> String | Uppercase |
to_lower | fn to_lower(String) -> String | Lowercase |
trim | fn trim(String) -> String | Strip whitespace |
reverse | fn reverse(String) -> String | Reverse |
split | fn split(String, String) -> List<String> | Split by delimiter |
join | fn join(List<String>, String) -> String | Join with separator |
slice | fn slice(String, Int, Int) -> String | Substring (start, length) |
replace | fn replace(String, String, String) -> String | Replace all occurrences |
replace_regex | fn replace_regex(String, String, String) -> String | Regex replace |
matches | fn matches(String, String) -> Bool | Regex test |
find_matches | fn find_matches(String, String) -> List<String> | All regex matches |
chars | fn chars(String) -> List<String> | Split into characters |
char_at | fn char_at(String, Int) -> Option<String> | Character at index |
index_of | fn index_of(String, String) -> Int | First occurrence index (-1 if none) |
to_int | fn to_int(String) -> Int | Parse as integer |
from_char_code | fn from_char_code(Int) -> String | String from Unicode code point |
char_code | fn char_code(String) -> Int | Unicode code point of first char |
Int
Integer operations.
| Function | Signature | Description |
|---|---|---|
to_string | fn to_string(Int) -> String | Convert to string |
parse | fn parse(String) -> Int | Parse string as int |
from_float | fn from_float(Float) -> Int | Truncate float to int |
Float
Floating-point operations.
| Function | Signature | Description |
|---|---|---|
from_int | fn from_int(Int) -> Float | Convert int to float |
format | fn format(Float, Int) -> String | Format with decimal places |
Map
Persistent key-value maps.
| Function | Signature | Description |
|---|---|---|
empty | fn empty() -> Map<K, V> | Create empty map |
insert | fn insert(Map<K, V>, K, V) -> Map<K, V> | Insert key-value pair |
set | fn set(Map<K, V>, K, V) -> Map<K, V> | Alias for insert |
get | fn get(Map<K, V>, K) -> Option<V> | Look up key |
remove | fn remove(Map<K, V>, K) -> Map<K, V> | Remove key |
contains | fn contains(Map<K, V>, K) -> Bool | Check if key exists |
has | fn has(Map<K, V>, K) -> Bool | Alias for contains |
keys | fn keys(Map<K, V>) -> List<K> | All keys |
values | fn values(Map<K, V>) -> List<V> | All values |
entries | fn entries(Map<K, V>) -> List<(K, V)> | All key-value pairs |
len | fn len(Map<K, V>) -> Int | Number of entries |
size | fn size(Map<K, V>) -> Int | Alias for len |
from_list | fn from_list(List<(K, V)>) -> Map<K, V> | Create from pairs |
Set
Persistent sets.
| Function | Signature | Description |
|---|---|---|
empty | fn empty() -> Set<T> | Create empty set |
insert | fn insert(Set<T>, T) -> Set<T> | Add element |
remove | fn remove(Set<T>, T) -> Set<T> | Remove element |
contains | fn contains(Set<T>, T) -> Bool | Check membership |
union | fn union(Set<T>, Set<T>) -> Set<T> | Set union |
intersection | fn intersection(Set<T>, Set<T>) -> Set<T> | Set intersection |
len | fn len(Set<T>) -> Int | Number of elements |
from_list | fn from_list(List<T>) -> Set<T> | Create from list |
IO
Console
Terminal I/O.
| Function | Signature | Description |
|---|---|---|
println! | fn println!(String) -> Unit | Print with newline |
print! | fn print!(String) -> Unit | Print without newline |
error! | fn error!(String) -> Unit | Print to stderr |
read_line! | fn read_line!() -> String | Read line from stdin |
Fs
Filesystem operations.
| Function | Signature | Description |
|---|---|---|
read! | fn read!(String) -> String | Read file contents |
write! | fn write!(String, String) -> Unit | Write string to file |
exists! | fn exists!(String) -> Bool | Check if path exists |
read_file! | fn read_file!(String) -> String | Alias for read! |
write_file! | fn write_file!(String, String) -> Unit | Alias for write! |
list_dir! | fn list_dir!(String) -> List<String> | List directory entries |
Env
Environment variables.
| Function | Signature | Description |
|---|---|---|
get! | fn get!(String) -> Option<String> | Read env var |
set! | fn set!(String, String) -> Unit | Set env var |
args! | fn args!() -> List<String> | Command-line arguments |
Async
Async
Run concurrent tasks with parallel execution, racing, timeouts, and delays.
| Function | Signature | Description |
|---|---|---|
parallel! | fn parallel!(List<() -> T>) -> List<T> | Run thunks concurrently, collect all results |
race! | fn race!(List<() -> T>) -> T | Run thunks concurrently, return first result |
scatter_gather! | fn scatter_gather!(List<() -> T>, (List<T>) -> U) -> U | Run thunks concurrently, apply gather function |
delay! | fn delay!(Int, () -> T) -> T | Wait milliseconds, then execute thunk |
interval! | fn interval!(Int, () -> Unit) -> Unit | Repeatedly execute thunk at interval |
timeout! | fn timeout!(Int, () -> T) -> Result<T, String> | Run thunk with timeout, Err on expiry |
Channel
Send and receive messages between concurrent tasks.
| Function | Signature | Description |
|---|---|---|
bounded | fn bounded(Int) -> (Sender<T>, Receiver<T>) | Create bounded channel with capacity |
send! | fn send!(Sender<T>, T) -> Unit | Send value into channel |
recv! | fn recv!(Receiver<T>) -> Option<T> | Receive value, None if closed |
close! | fn close!(Sender<T>) -> Unit | Close channel |
Scope
Structured concurrency.
| Function | Signature | Description |
|---|---|---|
spawn! | fn spawn!(Scope, () -> T) -> Cell<T> | Spawn task in scope |
Data
Json
JSON serialization.
| Function | Signature | Description |
|---|---|---|
parse | fn parse(String) -> T | Parse JSON string |
to_string | fn to_string(T) -> String | Serialize to compact JSON |
to_string_pretty | fn to_string_pretty(T) -> String | Serialize to pretty JSON |
to_camel_case | fn to_camel_case(T) -> T | Convert keys to camelCase |
to_snake_case | fn to_snake_case(T) -> T | Convert keys to snake_case |
Crypto
Cryptographic utilities.
| Function | Signature | Description |
|---|---|---|
sha256 | fn sha256(String) -> String | SHA-256 hash |
hmac_sha256 | fn hmac_sha256(String, String) -> String | HMAC-SHA256 signature |
constant_time_eq | fn constant_time_eq(String, String) -> Bool | Constant-time string comparison |
DateTime
Date and time operations.
| Function | Signature | Description |
|---|---|---|
now! | fn now!() -> Int | Current Unix timestamp |
parse | fn parse(String) -> Result<Int, String> | Parse date string to timestamp |
to_string | fn to_string(Int) -> String | Format timestamp as ISO 8601 |
add | fn add(Int, Int) -> Int | Add seconds to timestamp |
diff | fn diff(Int, Int) -> Int | Difference in seconds |
Math
Mathematical functions.
| Function | Signature | Description |
|---|---|---|
abs | fn abs(Int) -> Int | Absolute value |
min | fn min(Int, Int) -> Int | Smaller of two |
max | fn max(Int, Int) -> Int | Larger of two |
clamp | fn clamp(Int, Int, Int) -> Int | Constrain between min and max |
pow | fn pow(Int, Int) -> Int | Exponentiation |
sqrt | fn sqrt(Float) -> Float | Square root |
sin | fn sin(Float) -> Float | Sine |
cos | fn cos(Float) -> Float | Cosine |
atan2 | fn atan2(Float, Float) -> Float | Two-argument arctangent |
floor | fn floor(Float) -> Float | Round down |
ceil | fn ceil(Float) -> Float | Round up |
Error Handling
Option
Optional values.
| Function | Signature | Description |
|---|---|---|
unwrap | fn unwrap(Option<T>) -> T | Extract value or panic |
unwrap_or | fn unwrap_or(Option<T>, T) -> T | Extract value or default |
is_some | fn is_some(Option<T>) -> Bool | Has value? |
is_none | fn is_none(Option<T>) -> Bool | Is None? |
ok_or | fn ok_or(Option<T>, E) -> Result<T, E> | Convert to Result |
map | fn map(Option<T>, (T) -> U) -> Option<U> | Transform value |
flat_map | fn flat_map(Option<T>, (T) -> Option<U>) -> Option<U> | Chain optional operations |
Result
Error handling.
| Function | Signature | Description |
|---|---|---|
unwrap | fn unwrap(Result<T, E>) -> T | Extract Ok or panic |
unwrap_or | fn unwrap_or(Result<T, E>, T) -> T | Extract Ok or default |
is_ok | fn is_ok(Result<T, E>) -> Bool | Is Ok? |
is_err | fn is_err(Result<T, E>) -> Bool | Is Err? |
ensure | fn ensure(Bool, E) -> Result<Unit, E> | Assert condition |
map | fn map(Result<T, E>, (T) -> U) -> Result<U, E> | Transform Ok value |
and_then | fn and_then(Result<T, E>, (T) -> Result<U, E>) -> Result<U, E> | Chain Result operations |
map_err | fn map_err(Result<T, E>, (E) -> F) -> Result<T, F> | Transform Err value |
context | fn context(Result<T, E>, String) -> Result<T, {context: String, error: E}> | Add error context |
Other
Log
Structured logging (ambient effect).
| Function | Signature | Description |
|---|---|---|
info! | fn info!(String) -> Unit | Log at info level |
warn! | fn warn!(String) -> Unit | Log at warning level |
error! | fn error!(String) -> Unit | Log at error level |
debug! | fn debug!(String) -> Unit | Log at debug level |
Time
Clock operations (ambient effect).
| Function | Signature | Description |
|---|---|---|
now! | fn now!() -> Int | Current Unix timestamp in milliseconds |
sleep! | fn sleep!(Int) -> Unit | Pause for milliseconds |
Random
Random number generation.
| Function | Signature | Description |
|---|---|---|
int! | fn int!(Int, Int) -> Int | Random int in range (inclusive) |
bool! | fn bool!() -> Bool | Random boolean |
uuid! | fn uuid!() -> String | Random UUID v4 |
bytes! | fn bytes!(Int) -> String | Random bytes as hex (max 1024) |
Weak
Weak references.
| Function | Signature | Description |
|---|---|---|
downgrade | fn downgrade(T) -> Weak<T> | Create weak reference |
upgrade | fn upgrade(Weak<T>) -> Option<T> | Try to upgrade to strong reference |
Server Modules
Web
Router
HTTP routing.
| Function | Signature | Description |
|---|---|---|
new | fn new() -> Router | Create empty router |
get | fn get(Router, String, Handler) -> Router | GET route |
post | fn post(Router, String, Handler) -> Router | POST route |
put | fn put(Router, String, Handler) -> Router | PUT route |
delete | fn delete(Router, String, Handler) -> Router | DELETE route |
patch | fn patch(Router, String, Handler) -> Router | PATCH route |
options | fn options(Router, String, Handler) -> Router | OPTIONS route |
head | fn head(Router, String, Handler) -> Router | HEAD route |
any | fn any(Router, String, Handler) -> Router | All methods |
use | fn use(Router, Middleware) -> Router | Add middleware |
group | fn group(Router, String, Router) -> Router | Group under prefix |
resources | fn resources(Router, String, Record) -> Router | RESTful resource routes |
state | fn state(Router, String, T) -> Router | Attach shared state |
docs_json | fn docs_json(Router) -> String | Generate OpenAPI spec |
ws | fn ws(Router, String, Handler) -> Router | WebSocket handler |
routes | fn routes(Router) -> List<Record> | Route table |
Server
HTTP server.
| Function | Signature | Description |
|---|---|---|
listen! | fn listen!(Router, Int) -> Unit | Start server on port |
Request
HTTP request object.
| Function | Signature | Description |
|---|---|---|
header | fn header(Request, String) -> Option<String> | Read header |
method | fn method(Request) -> String | HTTP method |
body_json | fn body_json(Request) -> Result<T, HttpError> | Parse body as JSON |
param | fn param(Request, String) -> Result<String, String> | Path parameter |
param_int | fn param_int(Request, String) -> Result<Int, String> | Path parameter as int |
query | fn query(Request, String) -> Option<String> | Query parameter |
query_int | fn query_int(Request, String) -> Result<Int, String> | Query parameter as int |
decode | fn decode(Request, String) -> Result<T, Response> | Decode and validate body |
multipart | fn multipart(Request) -> List<Record> | Parse multipart form data |
Response
HTTP response construction.
| Function | Signature | Description |
|---|---|---|
ok | fn ok(String) -> Response | 200 with text |
json | fn json(T) -> Response | 200 with JSON |
created | fn created(T) -> Response | 201 |
no_content | fn no_content() -> Response | 204 |
bad_request | fn bad_request(String) -> Response | 400 |
unauthorized | fn unauthorized(String) -> Response | 401 |
forbidden | fn forbidden(String) -> Response | 403 |
not_found | fn not_found(String) -> Response | 404 |
method_not_allowed | fn method_not_allowed(String) -> Response | 405 |
conflict | fn conflict(String) -> Response | 409 |
unprocessable | fn unprocessable(String) -> Response | 422 |
too_many_requests | fn too_many_requests(String) -> Response | 429 |
error | fn error(String) -> Response | 500 |
bad_gateway | fn bad_gateway(String) -> Response | 502 |
service_unavailable | fn service_unavailable(String) -> Response | 503 |
gateway_timeout | fn gateway_timeout(String) -> Response | 504 |
status | fn status(Int, String) -> Response | Custom status |
with_header | fn with_header(Response, String, String) -> Response | Add header |
with_headers | fn with_headers(Response, List<(String, String)>) -> Response | Add multiple headers |
set_cookie | fn set_cookie(Response, String, String) -> Response | Set cookie |
redirect | fn redirect(String) -> Response | 302 redirect |
redirect_permanent | fn redirect_permanent(String) -> Response | 301 redirect |
redirect_temporary | fn redirect_temporary(String) -> Response | 307 redirect |
Http
HTTP client.
| Function | Signature | Description |
|---|---|---|
get! | fn get!(String) -> Response | GET request |
post! | fn post!(String, String) -> Response | POST request with body |
put! | fn put!(String, String) -> Response | PUT request with body |
delete! | fn delete!(String) -> Response | DELETE request |
request! | fn request!(RequestConfig) -> Response | Custom request |
Database
Sqlite
SQLite database.
| Function | Signature | Description |
|---|---|---|
connect! | fn connect!(String) -> Unit | Open database file |
execute! | fn execute!(String, List<String>) -> Int | Execute statement |
query! | fn query!(String, List<String>) -> List<Row> | Run query |
query_one! | fn query_one!(String, List<String>) -> Option<Row> | First row |
query_as! | fn query_as!(Decoder, String, List<String>) -> List<T> | Query with decoder |
query_one_as! | fn query_one_as!(Decoder, String, List<String>) -> Option<T> | First row with decoder |
query_map! | fn query_map!(String, List<String>) -> List<Row> | Query for mapping |
Postgres
PostgreSQL database.
| Function | Signature | Description |
|---|---|---|
connect! | fn connect!(String) -> Unit | Open connection |
execute! | fn execute!(String, List<String>) -> Int | Execute statement |
query! | fn query!(String, List<String>) -> List<Row> | Run query |
query_one! | fn query_one!(String, List<String>) -> Option<Row> | First row |
query_as! | fn query_as!(Decoder, String, List<String>) -> List<T> | Query with decoder |
query_one_as! | fn query_one_as!(Decoder, String, List<String>) -> Option<T> | First row with decoder |
query_map! | fn query_map!(String, List<String>) -> List<Row> | Query for mapping |
Mysql
MySQL database.
| Function | Signature | Description |
|---|---|---|
connect! | fn connect!(String) -> Unit | Open connection |
execute! | fn execute!(String, List<String>) -> Int | Execute statement |
query! | fn query!(String, List<String>) -> List<Row> | Run query |
query_one! | fn query_one!(String, List<String>) -> Option<Row> | First row |
query_as! | fn query_as!(Decoder, String, List<String>) -> List<T> | Query with decoder |
query_one_as! | fn query_one_as!(Decoder, String, List<String>) -> Option<T> | First row with decoder |
query_map! | fn query_map!(String, List<String>) -> List<Row> | Query for mapping |
Row
Database row access.
| Function | Signature | Description |
|---|---|---|
string | fn string(Row, String) -> String | String column |
int | fn int(Row, String) -> Int | Int column |
float | fn float(Row, String) -> Float | Float column |
bool | fn bool(Row, String) -> Bool | Bool column |
optional_string | fn optional_string(Row, String) -> Option<String> | Nullable string |
optional_int | fn optional_int(Row, String) -> Option<Int> | Nullable int |
decode | fn decode(Row, Decoder) -> T | Decode row to record |
Sql
Database migrations.
| Function | Signature | Description |
|---|---|---|
migrate! | fn migrate!(String) -> Result<Int, String> | Run migrations from directory |
Auth
Jwt
JSON Web Tokens.
| Function | Signature | Description |
|---|---|---|
sign | fn sign(Record, String) -> String | Sign claims as JWT (HS256) |
sign_with | fn sign_with(Record, String, { expires_in: Int }) -> String | Sign with options |
verify | fn verify(String, String) -> Result<Record, String> | Verify and decode |
decode | fn decode(String) -> Result<Record, String> | Decode without verifying |
Session
Session management.
| Function | Signature | Description |
|---|---|---|
create! | fn create!(Record) -> String | Create session, return ID |
get! | fn get!(String) -> Option<Record> | Get session data |
set! | fn set!(String, String, T) -> Unit | Update session field |
delete! | fn delete!(String) -> Unit | Remove session |
Middleware
Request middleware utilities.
| Function | Signature | Description |
|---|---|---|
extract_bearer | fn extract_bearer(Request) -> Result<String, HttpError> | Extract Bearer token |
extract_basic | fn extract_basic(Request) -> Result<(String, String), HttpError> | Extract Basic auth |
cors_config | fn cors_config(List<String>) -> Record | CORS configuration |
rate_limit_config | fn rate_limit_config(Int, Int) -> Record | Rate limiting config |
Validation
Validate
Request validation.
| Function | Signature | Description |
|---|---|---|
required | fn required(T, String) -> Result<T, HttpError> | Check not None |
string | fn string(T, String) -> Result<String, HttpError> | Validate string |
int | fn int(T, String) -> Result<Int, HttpError> | Validate int |
boolean | fn boolean(T, String) -> Result<Bool, HttpError> | Validate bool |
min_length | fn min_length(String, Int, String) -> Result<String, HttpError> | Min string length |
max_length | fn max_length(String, Int, String) -> Result<String, HttpError> | Max string length |
min | fn min(Int, Int, String) -> Result<Int, HttpError> | Min number |
max | fn max(Int, Int, String) -> Result<Int, HttpError> | Max number |
one_of | fn one_of(String, List<String>, String) -> Result<String, HttpError> | Allowed values |
HttpError
Typed HTTP errors.
| Function | Signature | Description |
|---|---|---|
bad_request | fn bad_request(String) -> HttpError | 400 |
unauthorized | fn unauthorized(String) -> HttpError | 401 |
forbidden | fn forbidden(String) -> HttpError | 403 |
not_found | fn not_found(String) -> HttpError | 404 |
method_not_allowed | fn method_not_allowed(String) -> HttpError | 405 |
conflict | fn conflict(String) -> HttpError | 409 |
unprocessable | fn unprocessable(String) -> HttpError | 422 |
too_many_requests | fn too_many_requests(String) -> HttpError | 429 |
internal | fn internal(String) -> HttpError | 500 |
bad_gateway | fn bad_gateway(String) -> HttpError | 502 |
service_unavailable | fn service_unavailable(String) -> HttpError | 503 |
gateway_timeout | fn gateway_timeout(String) -> HttpError | 504 |
with_context | fn with_context(HttpError, String) -> HttpError | Add context |
with_code | fn with_code(HttpError, String) -> HttpError | Add error code |
to_response | fn to_response(HttpError) -> Response | Convert to Response |
Realtime
Ws
WebSocket communication.
| Function | Signature | Description |
|---|---|---|
send! | fn send!(String) -> Unit | Send message |
receive! | fn receive!() -> Result<String, String> | Receive message |
close! | fn close!() -> Unit | Close connection |