Expand description
Core types for the Slabs web framework.
This crate owns prefixed ULID generation, the unified HTTP error type, environment configuration, and JSON route/job manifests.
§Owned types
id::new: generates lowercase, time-sortable prefixed ULIDs such asmsg_01j....SlabErrorandSlabResult: explicit error enum returned by every transaction script and background job.Validate: multi-field procedural validation builder returningSlabError::ValidationErrors.AppConfig: runtime configuration loaded fromDATABASE_URLandSLABS_*environment variables, plusAppConfig::for_testfor isolated tests.AppManifest,RouteMeta, andJobMeta: JSON metadata returned by/_slabs/manifestandslabs manifest, plusOpenAPI3.1.0 export viaAppManifest::to_openapi_3_1.
§HTTP status mapping
SlabError implements axum::response::IntoResponse and returns a JSON body with error and message fields:
| Variant | HTTP status | JSON error field | Notes |
|---|---|---|---|
SlabError::BadRequest | 400 | "bad_request" | Includes the error message. |
SlabError::Unauthorized | 401 | "unauthorized" | Missing or invalid credentials. |
SlabError::Forbidden | 403 | "forbidden" | Authenticated caller lacks permission. |
SlabError::NotFound | 404 | "not_found" | Resource does not exist. |
SlabError::Database (RowNotFound) | 404 | "not_found" | Message is fixed to "record not found". |
SlabError::Conflict | 409 | "conflict" | Unique key or state conflict. |
SlabError::Validation | 422 | "validation_error" | Also includes a field string. |
SlabError::ValidationErrors | 422 | "validation_error" | Includes a fields map of field names to error message lists. |
SlabError::TooManyRequests | 429 | "too_many_requests" | Sets Retry-After header and retry_after_secs. |
SlabError::Database (other) | 500 | "database_error" | Logs via tracing::error! and hides raw SQL details. |
SlabError::Internal | 500 | "internal_error" | Logs via tracing::error! and returns "internal server error". |
§Environment variables
AppConfig::from_env reads these variables and falls back to local development defaults derived from app_name:
SLABS_BIND: bind address. Defaults to"127.0.0.1:3000".DATABASE_URL: database connection string. Defaults to"sqlite://{app_name}.db?mode=rwc".SLABS_SECRET_KEY: signing key for cookies, tokens, and HMAC digests. Defaults to a development-only string.SLABS_STORAGE_DIR: local blob directory. Defaults to"./storage/{app_name}".SLABS_ASSETS_DIR: static assets directory. Defaults to"./assets"when present, otherwise"./examples/{app_name}/assets".SLABS_JOB_POLL_MS: background worker poll interval in milliseconds. Defaults to500.SLABS_JOB_CONCURRENCY: maximum concurrent background jobs. Defaults to4.SLABS_COOKIE_SECURE: enables theSecurecookie attribute when"1"or"true"(case-insensitive). Defaults tofalse.SLABS_WEBHOOKS_LIVE: enables live outbound webhook HTTP requests when"1"or"true". Defaults tofalse.SLABS_WEBHOOKS_ALLOW_LOCAL: permits webhooks to target loopback addresses when"1"or"true". Defaults tofalse.
§Invariants
id::newproduces lowercase ULIDs prefixed with<prefix>_soSQLiteB-tree primary keys stay append-friendly.- Returning
?on asqlx::Error::RowNotFoundproduces HTTP 404 without manual mapping. - Unexpected database and internal errors never leak internal messages or SQL strings in the HTTP response body.
AppConfig::for_testpinsdatabase_urlto"sqlite::memory:",bind_addrto"127.0.0.1:0", and disables live webhooks.
§Usage
use slabs_core::{id, AppConfig, SlabError, SlabResult};
fn validate_title(title: &str) -> SlabResult<String> {
if title.trim().is_empty() {
return Err(SlabError::validation("title", "cannot be blank"));
}
Ok(id::new("room"))
}
let config = AppConfig::from_env("campfire");
assert_eq!(config.app_name, "campfire");
assert!(validate_title("Lobby").unwrap().starts_with("room_"));
assert_eq!(validate_title(" ").unwrap_err().status_code().as_u16(), 422);Modules§
- id
- Prefix-tagged, time-sortable ULID generator (for example
msg_01j...orusr_01j...). - position
- Fractional list positioning helpers for O(1) single-row reordering.
- slug
- URL slug generation helpers.
Structs§
- AppConfig
- Runtime configuration loaded from
SLABS_*andDATABASE_URLenvironment variables. - AppManifest
- Complete JSON manifest of an application’s discovered HTTP routes and background jobs.
- JobMeta
- Machine-readable metadata describing a registered background job.
- Route
Meta - Machine-readable HTTP route metadata emitted by
/_slabs/manifestandslabs manifest. - Validate
- Multi-field procedural validation builder for transaction scripts.
Enums§
- Slab
Error - Unified HTTP error type returned by every transaction script and background job.
Functions§
- fingerprint
- Compute a lowercase SHA-256 hex digest over NUL-separated string parts.
Type Aliases§
- Slab
Result - Result alias using
SlabErroras the error type.