cratestack/lib.rs
1//! CrateStack server facade — Postgres (sqlx) + Axum.
2//!
3//! This crate is the server-side slice of the framework. It re-exports the
4//! shared schema / parser / policy / SQL surface plus the sqlx (Postgres)
5//! runtime, Axum HTTP bindings, and the generated Rust client runtime.
6//!
7//! It deliberately does **not** depend on `cratestack-rusqlite`. That keeps
8//! `libsqlite3-sys` out of the dep graph, so consumers can use the official
9//! `sqlx` umbrella crate (which optionally declares `sqlx-sqlite` and trips
10//! Cargo's `links = "sqlite3"` collision rule) without needing a local
11//! `sqlx-shim` workaround.
12//!
13//! For embedded / mobile / wasm targets, depend on `cratestack-sqlite`
14//! instead. The two crates are strictly disjoint by design.
15//!
16//! Schema macros emit `::cratestack::*` paths, so consumers rename this
17//! crate via Cargo's `package =` field:
18//!
19//! ```toml
20//! [dependencies]
21//! cratestack = { package = "cratestack-pg", version = "0.4" }
22//! ```
23
24// Both `cratestack_core` and `cratestack_axum` expose `codec` and
25// `transport` modules, and the facade re-exports both crates with a glob.
26// The overlap is intentional — consumers reach those via the originating
27// crate's path, not the facade root — so silence the ambiguity warning
28// rather than dropping either glob.
29#![allow(ambiguous_glob_reexports)]
30
31pub use chrono;
32pub use cratestack_client_rust as client_rust;
33pub use cratestack_core::*;
34pub use cratestack_macros::{
35 include_client_schema, include_embedded_schema, include_server_schema,
36};
37pub use cratestack_parser::{SchemaError, parse_schema, parse_schema_file, parse_schema_named};
38pub use cratestack_policy::{
39 PolicyExpr, PolicyLiteral, ProcedureArgs, ProcedurePolicy, ProcedurePolicyExpr,
40 ProcedurePolicyLiteral, ProcedurePredicate, ReadPolicy, ReadPredicate, RelationQuantifier,
41 authorize_procedure,
42};
43
44// SQL primitives shared by every backend — re-exported directly from
45// `cratestack-sql` so consumers don't transit through `cratestack-sqlx`.
46pub use cratestack_sql::{
47 CoalesceExpr, CoalesceFilter, ConflictTarget, CreateDefault, CreateDefaultType,
48 CreateModelInput, FieldRef, Filter, FilterExpr, FilterOp, IntoColumnName, IntoSqlValue,
49 JsonFilter, JsonTextPath, ModelColumn, ModelDescriptor, ModelPrimaryKey, NullOrder,
50 OrderClause, Projection, ReadSource, RelationFilter, RelationInclude, SortDirection,
51 SpatialFilter, SpatialPoint, SqlColumnValue, SqlValue, UpdateModelInput, UpsertModelInput,
52 ViewDescriptor, WriteSource, coalesce, point,
53};
54
55pub use regex;
56pub use serde;
57pub use serde_json;
58pub use tracing;
59pub use uuid;
60
61// `Json<T>` resolves to `sqlx::types::Json<T>` on the server so
62// `sqlx::FromRow` decodes Postgres `jsonb` columns into it directly.
63pub use cratestack_sqlx::sqlx::types::Json;
64
65// -----------------------------------------------------------------------------
66// Server surface — sqlx, axum, audit/idempotency/migrations/isolation.
67// -----------------------------------------------------------------------------
68
69pub use cratestack_axum::axum;
70pub use cratestack_axum::*;
71
72// Disambiguate the `rpc` module path. Both `cratestack_core` (wire shapes)
73// and `cratestack_axum` (binding helpers) expose an `rpc` module, so the
74// two `pub use ..::*` globs collide on the name and `::cratestack::rpc::*`
75// resolves non-deterministically. Macro-emitted code in `transport rpc`
76// schemas references symbols like `encode_rpc_error`,
77// `convert_handler_error_response`, `response_to_frame`, and
78// `RPC_BINDING_CAPABILITIES` — all of which live in `cratestack-axum::rpc`.
79// An explicit `pub use` re-export takes precedence over the globs, pinning
80// `::cratestack::rpc` to the axum module (which itself re-exports the wire
81// types from `cratestack-core::rpc`).
82pub use cratestack_axum::rpc;
83
84pub use cratestack_sqlx::AUDIT_TABLE_DDL;
85pub use cratestack_sqlx::sqlx;
86pub use cratestack_sqlx::{
87 Aggregate, AggregateColumn, AggregateCount, CreateRecord, DeleteMany, DeleteRecord, FindMany,
88 FindManyWith, FindUnique, FromPartialPgRow, ModelDelegate, ProjectedFindMany,
89 ProjectedFindUnique, ScopedAggregate, ScopedAggregateColumn, ScopedAggregateCount,
90 ScopedCreateRecord, ScopedDeleteMany, ScopedDeleteRecord, ScopedFindMany, ScopedFindManyWith,
91 ScopedFindUnique, ScopedModelDelegate, ScopedProjectedFindMany, ScopedProjectedFindUnique,
92 ScopedUpdateMany, ScopedUpdateManySet, ScopedUpdateRecord, ScopedUpdateRecordSet,
93 SqlxIdempotencyStore, UpdateMany, UpdateManySet, UpdateRecord, UpdateRecordSet, ViewDelegate,
94 ViewDelegateNoUnique, create_record_with_executor, update_record_with_executor,
95};
96pub use cratestack_sqlx::{
97 MIGRATIONS_TABLE_DDL, Migration, MigrationState, MigrationStatus, apply_pending,
98 ensure_migrations_table, status,
99};
100pub use cratestack_sqlx::{
101 cool_error_from_sqlx, run_in_isolated_tx, run_in_isolated_tx_with_retries,
102};
103
104/// Crypto provider selection — banks running on FIPS-validated hardware
105/// enable the `crypto-aws-lc-rs` feature. The function below surfaces an
106/// error early when the feature is missing so the wrong build can't slip
107/// into a regulated production cluster.
108///
109/// Operational steps for a real FIPS deployment (out of scope for the
110/// framework itself):
111///
112/// 1. Build with `--features crypto-aws-lc-rs`.
113/// 2. Use an `aws-lc-rs`/`rustls` build configured against the vendor's
114/// FIPS-validated `libcrypto`.
115/// 3. Call [`install_fips_crypto_provider`] from your service's `main`
116/// *before* any TLS-using code runs.
117/// 4. Pin the binary's `cargo audit` report and the validated module's
118/// certificate id in your release process.
119pub fn install_fips_crypto_provider() -> Result<(), cratestack_core::CoolError> {
120 #[cfg(feature = "crypto-aws-lc-rs")]
121 {
122 Ok(())
123 }
124 #[cfg(not(feature = "crypto-aws-lc-rs"))]
125 {
126 Err(cratestack_core::CoolError::Internal(
127 "cratestack was not compiled with `crypto-aws-lc-rs` feature; \
128 FIPS-validated crypto provider is unavailable"
129 .to_owned(),
130 ))
131 }
132}
133
134#[doc(hidden)]
135pub mod __private {
136 pub use cratestack_sqlx::SqlxRuntime;
137
138 /// Re-exports for the macro-emitted RPC dispatcher. Not part of the
139 /// public API surface — schema authors should never reference these
140 /// directly. Public helpers live at `cratestack::rpc::*`.
141 pub use cratestack_axum::rpc::{decode_rpc_body, encode_rpc_value, response_to_frame};
142}