persistant/lib.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! PersistAnt: a thin persistence layer over Apache OpenDAL, for blobs and records by key.
3//!
4#![doc = include_str!("../README.md")]
5
6pub mod blocking;
7mod error;
8mod store;
9
10pub use error::{Error, Missing, Result};
11pub use store::{Config, Entry, Faults, Need, Needs, Page, Record, Replace, Store};
12
13// The other kinds of persistence, behind features that are off by default. Additive: nothing here
14// changes `Store`, `Config`, `Need` or any other item above. Names avoid `core` and `opendal`
15// because those are already names in scope here.
16
17/// The capability vocabulary, the matrix file and its gate lookup (`persistant-core`).
18#[cfg(feature = "core")]
19pub use persistant_core as capability;
20
21/// The blob trait, its reference backend and its contract tests (`persistant-blob`).
22#[cfg(feature = "blob")]
23pub use persistant_blob as blob;
24
25/// The key-value trait, `KvOnBlob`, `KvOnRelational`, leases and their contract tests
26/// (`persistant-kv`).
27#[cfg(feature = "kv")]
28pub use persistant_kv as kv;
29
30/// The small relational trait, values and typed errors (`persistant-relational`).
31#[cfg(feature = "relational")]
32pub use persistant_relational as relational;
33
34/// The OpenDAL blob driver and `ProcessLock` (`persistant-opendal`).
35#[cfg(feature = "opendal-driver")]
36pub use persistant_opendal as opendal_driver;
37
38/// The PostgreSQL driver over sqlx (`persistant-sqlx`). Needs Rust 1.94.
39#[cfg(feature = "sqlx-driver")]
40pub use persistant_sqlx as sqlx_driver;
41
42/// The crate version, as declared in `Cargo.toml`.
43pub const VERSION: &str = env!("CARGO_PKG_VERSION");
44
45/// Compile-time SQL (feature `sql-macros`): `sql!` checks a literal statement and translates it for
46/// every target named, at build time. The syntax, the placeholders and every target's translation are
47/// build errors, not run-time ones; no database is consulted and no schema is read yet (the
48/// catalog-based checks are a later stage, `docs/COMPILE-TIME-SQL.md`).
49///
50/// `schema` is required: `schema = none` for database-free checks only, or a path (relative to your
51/// `Cargo.toml`) to a `.json` snapshot written by `persistant-migrate snapshot`, or a `.sql` DDL file.
52/// With none stated the crate looks for `persistant-schema.json` and fails to build without it: a
53/// missing schema is never a quiet downgrade. With one, UniSQL checks the names and the `INSERT`
54/// arity, and PersistAnt checks that an `INSERT` does not leave out a `NOT NULL` column that has no
55/// default and that an `ON CONFLICT (..)` names a unique key. It errors only when certain: what the
56/// schema does not state is unknown and passes.
57///
58/// ```
59/// use persistant::{Sql, sql};
60/// const FIND: Sql = sql!(
61/// source = postgres, targets = [postgres, sqlite], schema = none,
62/// "SELECT id FROM users WHERE id = $1"
63/// );
64/// assert_eq!(FIND.params, 1);
65/// ```
66///
67/// A statement that cannot be translated for a named target does not build. The twin above is the same
68/// call with the fault (`md5` has no SQLite form) removed:
69///
70/// ```
71/// use persistant::{Sql, sql};
72/// const OK: Sql = sql!(source = postgres, targets = [postgres], schema = none, "SELECT md5($1)");
73/// ```
74///
75/// ```compile_fail
76/// use persistant::{Sql, sql};
77/// const BAD: Sql = sql!(source = postgres, targets = [postgres, sqlite], schema = none, "SELECT md5($1)");
78/// ```
79///
80/// Against a schema (`tests/doctest-schema.sql`: `users` has `id` and `email`, both `NOT NULL` with no
81/// default), an `INSERT` that leaves `email` out does not build; the twin that supplies it does:
82///
83/// ```
84/// use persistant::{Sql, sql};
85/// const OK: Sql = sql!(
86/// source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
87/// "INSERT INTO users (id, email) VALUES ($1, $2)"
88/// );
89/// ```
90///
91/// ```compile_fail
92/// use persistant::{Sql, sql};
93/// const BAD: Sql = sql!(
94/// source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
95/// "INSERT INTO users (id) VALUES ($1)"
96/// );
97/// ```
98#[cfg(feature = "sql-macros")]
99pub use persistant_macros::sql;
100
101/// What `sql!` expands to, and the extension traits that run it (feature `sql-macros`).
102#[cfg(feature = "sql-macros")]
103pub use persistant_relational::{
104 RelationalRowsExt, RelationalSqlExt, RelationalTxRowsExt, RelationalTxSqlExt, Sql, TypedSql,
105 embed,
106};
107
108/// Typed rows (feature `sql-macros`): `#[derive(FromRow)]` reads a struct from a result row by
109/// position, and `sql!(as T, ..)` checks a `SELECT` against it **at build time**, using the schema: the
110/// number of columns, each column's name (the alias, if any), its class (a numeric column into an
111/// integer or float field, text into `String`, a boolean into `bool`) and its nullability (a column
112/// that can be NULL needs an `Option` field). A mismatch is a build error naming the column. Run the
113/// result with `query_as`.
114///
115/// ```
116/// use persistant::{FromRow, TypedSql, sql};
117/// #[derive(FromRow)]
118/// struct User { id: i64, email: String, note: Option<String> }
119/// const FIND: TypedSql<User> = sql!(
120/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
121/// "SELECT id, email, note FROM users WHERE id = $1"
122/// );
123/// ```
124///
125/// An expression column is named and typed: `count(*) AS "n: i64"` (or `Option<i64>`). The twins of
126/// the failing cases below are the example above:
127///
128/// ```compile_fail
129/// use persistant::{FromRow, TypedSql, sql};
130/// #[derive(FromRow)]
131/// struct User { id: i64, email: i64, note: Option<String> } // `email` is text
132/// const FIND: TypedSql<User> = sql!(
133/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
134/// "SELECT id, email, note FROM users WHERE id = $1"
135/// );
136/// ```
137///
138/// ```compile_fail
139/// use persistant::{FromRow, TypedSql, sql};
140/// #[derive(FromRow)]
141/// struct User { id: i64, email: String, note: String } // `note` can be NULL
142/// const FIND: TypedSql<User> = sql!(
143/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
144/// "SELECT id, email, note FROM users WHERE id = $1"
145/// );
146/// ```
147///
148/// ```compile_fail
149/// use persistant::{FromRow, TypedSql, sql};
150/// #[derive(FromRow)]
151/// struct User { id: i64, mail: String, note: Option<String> } // the column is `email`
152/// const FIND: TypedSql<User> = sql!(
153/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
154/// "SELECT id, email, note FROM users WHERE id = $1"
155/// );
156/// ```
157#[cfg(feature = "sql-macros")]
158pub use persistant_macros::FromRow;
159
160/// The trait `#[derive(FromRow)]` implements (feature `sql-macros`).
161#[cfg(feature = "sql-macros")]
162pub use persistant_relational::FromRow;
163
164#[cfg(test)]
165mod tests {
166 use super::VERSION;
167
168 #[test]
169 fn version_is_the_manifest_version() {
170 assert_eq!(VERSION, "0.9.3");
171 }
172}