Skip to main content

Crate slabs_core

Crate slabs_core 

Source
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 as msg_01j....
  • SlabError and SlabResult: explicit error enum returned by every transaction script and background job.
  • AppConfig: runtime configuration loaded from DATABASE_URL and SLABS_* environment variables, plus AppConfig::for_test for isolated tests.
  • AppManifest, RouteMeta, and JobMeta: JSON metadata returned by /_slabs/manifest and slabs manifest.

§HTTP status mapping

SlabError implements axum::response::IntoResponse and returns a JSON body with error and message fields:

VariantHTTP statusJSON error fieldNotes
SlabError::BadRequest400"bad_request"Includes the error message.
SlabError::Unauthorized401"unauthorized"Missing or invalid credentials.
SlabError::Forbidden403"forbidden"Authenticated caller lacks permission.
SlabError::NotFound404"not_found"Resource does not exist.
SlabError::Database (RowNotFound)404"not_found"Message is fixed to "record not found".
SlabError::Conflict409"conflict"Unique key or state conflict.
SlabError::Validation422"validation_error"Also includes a field string.
SlabError::TooManyRequests429"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::Internal500"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 to 500.
  • SLABS_JOB_CONCURRENCY: maximum concurrent background jobs. Defaults to 4.
  • SLABS_COOKIE_SECURE: enables the Secure cookie attribute when "1" or "true" (case-insensitive). Defaults to false.
  • SLABS_WEBHOOKS_LIVE: enables live outbound webhook HTTP requests when "1" or "true". Defaults to false.
  • SLABS_WEBHOOKS_ALLOW_LOCAL: permits webhooks to target loopback addresses when "1" or "true". Defaults to false.

§Invariants

  • id::new produces lowercase ULIDs prefixed with <prefix>_ so SQLite B-tree primary keys stay append-friendly.
  • Returning ? on a sqlx::Error::RowNotFound produces HTTP 404 without manual mapping.
  • Unexpected database and internal errors never leak internal messages or SQL strings in the HTTP response body.
  • AppConfig::for_test pins database_url to "sqlite::memory:", bind_addr to "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... or usr_01j...).
position
Fractional list positioning helpers for O(1) single-row reordering.
slug
URL slug generation helpers.

Structs§

AppConfig
Runtime configuration loaded from SLABS_* and DATABASE_URL environment variables.
AppManifest
Complete JSON manifest of an application’s discovered HTTP routes and background jobs.
JobMeta
Machine-readable metadata describing a registered background job.
RouteMeta
Machine-readable HTTP route metadata emitted by /_slabs/manifest and slabs manifest.

Enums§

SlabError
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§

SlabResult
Result alias using SlabError as the error type.