cratestack-core
Core types, traits, and error handling shared across the CrateStack workspace.
Overview
cratestack-core provides the foundational types that the rest of the workspace depends on:
- Error handling:
CoolErrorwith HTTP status mapping and operator vs. public message split - Auth context:
CoolContext,PrincipalContext,CoolAuthIdentity,AuthProvider - Schema AST:
Schema,Model,Field,Procedure,MixinDecl,TypeDecl,EnumDecl - Audit:
AuditEvent,AuditOperation,AuditActor,AuditSink,NoopAuditSink,MulticastAuditSink - Signed envelope:
HmacEnvelope(HS256),KeyProvider,StaticKeyProvider,NonceStore,InMemoryNonceStore - Codec/envelope traits:
CoolCodec,CoolEnvelope,NoEnvelope - Event bus:
CoolEventBus,ModelEvent<T>,ModelEventKind,CoolEventEnvelope - Transaction isolation:
TransactionIsolation - Decimal scalar:
Decimal(compile-time backend) - Validators:
validate_length,validate_range_i64,validate_range_decimal,validate_email,validate_uri,validate_iso4217
Installation
[]
= "0.2.2"
A Decimal backend feature must be selected. decimal-rust-decimal is the default; decimal-bigdecimal is reserved and not yet implemented (selecting it today is a compile error).
Error Handling
CoolError returns a safe public message to clients while keeping operator-only detail for tracing.
use CoolError;
use StatusCode;
let err = BadRequest;
assert_eq!;
assert_eq!;
// 5xx variants return a fixed canned public message; the inner string flows to `detail` only.
let err = Database;
assert_eq!;
assert_eq!;
Variants: BadRequest, NotAcceptable, Unauthorized, UnsupportedMediaType, Forbidden, NotFound, Conflict, Validation, PreconditionFailed, Codec, Database, DatabaseTyped, Internal. The codec/database/internal variants are 5xx-mapped. CoolError is #[non_exhaustive], so downstream matches must include a wildcard arm.
DatabaseTyped carries a DbErrorInfo { detail, sqlstate, constraint } and is produced by cratestack_sqlx::cool_error_from_sqlx at sqlx call sites. Use err.db_sqlstate() and err.db_constraint() to inspect the typed fields instead of substring-matching the stringified detail.
Auth Context
CoolContext carries the authenticated principal and arbitrary host-provided extensions.
use ;
let ctx = anonymous;
let ctx = from_principal?;
assert_eq!;
assert_eq!;
AuthProvider
Host applications implement AuthProvider to resolve auth from HTTP requests:
use ;
;
AuthProvider is also implemented blanket for any Fn(&HeaderMap) -> Result<CoolContext, E> closure.
Audit Events
use ;
use Utc;
let event = AuditEvent ;
AuditSink is an async trait. The bundled implementations are NoopAuditSink and MulticastAuditSink. The in-database table written by cratestack-sqlx is treated as the canonical record; sinks are best-effort projections.
Decimal Backend
[]
= { = "0.2.2", = ["decimal-rust-decimal"] }
use Decimal;
let amount: Decimal = "123.45".parse?;
Transaction Isolation
use TransactionIsolation;
let isolation = parse?;
assert_eq!;
Accepts read_committed / read committed, repeatable_read / repeatable read, and serializable.
Signed Envelope (HMAC-SHA-256)
HmacEnvelope<K: KeyProvider> implements CoolEnvelope for HS256-signed messages. Production multi-replica deployments back the NonceStore with Redis so replay rejection holds cluster-wide.
See Also
License
MIT