Standards

The lib* ecosystem

On this page
  1. Rule
  2. Catalogue
  3. Graceful shutdown (libsignal)
  4. Adding a new lib*
  5. Checklist

Rule#

Cross-cutting concerns are solved once in a shared lib* crate and reused by every repo, not re-implemented. Before writing infrastructure code (errors, logging, config, paths, signals, subprocesses), check whether a lib* already covers it. They're consumed as git deps (rust-conventions.md).

Catalogue#

CratePurposeKey API
liberrorSerializable error wrapper preserving the source chainAnyError { $type, context }, From<E: Error>
libidTyped entity identifiers: internal UUIDv7 + public Crockford aliasId<T>, PublicId<T>, PublicEntity (opt. PREFIX); features serde/sqlx/poem-openapi
liblogtracing + OpenTelemetry subscriber stackbuilder().endpoint(..).sample_rate(..).init() -> guard; force_cleanup(guard)
libsignalCross-platform graceful shutdown (Unix/Windows signals)wait_for_signal(), cancel_after_signal(token), *_or(token, fut)
libconfigTOML + env config with mtime trackingload<T>(), store_checked(), config!{} macro
libpathPlatform app dirs (config/logs/cache/data)dirs::config_path(), logs_root(), … (keyed by product name)
libproductReverse-domain product descriptor + macroproduct_name!("dev.thmsn.x"), ProductName { base, ext, version, build }
libbuildinfoBuild metadata (git/OS/package) embedded at build timeemit() (build.rs), load_build_info!(optional)
libcmdAsync subprocess exec with streaming + cancellationrun(cmd, args, token) -> CommandMonitor
libwhichBinary discovery in PATHwhich(names), is_valid_executable()
libringFixed-size ring buffer for bounded historyRingBuffer::new(cap), .push(), .to_vec() (opt. serde)
libciThe fleet's kiln recipes: version scheme, release registry, GHCR images, private npm, audit gate, secret names, service-repo pipelines. Only for a repo's .kiln/ crate, never app code; it depends on kilnversion::Version::derive(ctx), registry::AppRegistry, templates::{RustCi, ImageBuild, WebCi, BrandingCi}; one feature per module
libfsTokio async filesystem with optional tracingPathResult<T> over AnyError
libanalyticsPrivate (ssh://[email protected]/charliethomson/libanalytics.git). Client analytics: the server's telemetry/events handler, plus the client event recorders as a Rust crate (recorder feature), a root Swift package (SSH git URL + semver tags) and a TS package @thmsn/analytics (internal Verdaccio)handler returns per-reason drop counts; recorders take a product-supplied transport + auth state

libpath + libproduct always travel together (paths are keyed by the product name). libbuildinfo backs libproduct's descriptor and liblog's service.build_info.

Graceful shutdown (libsignal)#

The standard choreography — turn a signal into a CancellationToken, then race it:

let token = CancellationToken::new();
libsignal::cancel_after_signal(token.clone());     // spawns a waiter that cancels on SIGTERM/Ctrl-C

loop {
    tokio::select! {
        () = token.cancelled() => break,
        _  = do_work() => {}
    }
}

cancel_after_signal_or(token, fut) adds a second trigger (timeout, health failure). HTTP services pass the same idea to poem's run_with_graceful_shutdown (service-architecture.md).

Adding a new lib*#

  1. Edition 2024, workspace lints, liberror-based serializable errors (error-handling.md).
  2. One clear purpose; feature-gate optional surface; README documents the public API; examples are the integration tests (archetypes/library.md).
  3. Distribute as a git dep (HTTPS public / SSH private).

Checklist#

  • Reached for an existing lib* before writing errors/logging/config/paths/signals/subprocess code.
  • libsignal used for graceful shutdown; liberror for serializable errors; liblog for telemetry.
  • libpath + libproduct used together for app dirs + product identity.
  • A new shared concern becomes a new lib*, not a copy-paste.