|
@@ -31,7 +31,7 @@ reconstructs every balance, so the ledger is auditable by construction. See
|
|
|
├─────────────────────────────────────────────────────┤
|
|
├─────────────────────────────────────────────────────┤
|
|
|
│ kuatia-core (pure) │
|
|
│ kuatia-core (pure) │
|
|
|
│ │
|
|
│ │
|
|
|
-│ Types: Account · Transfer · Posting · Cent │
|
|
|
|
|
|
|
+│ Types: Account · Transfer · Posting · Amount│
|
|
|
│ Validation: validate_and_plan() │
|
|
│ Validation: validate_and_plan() │
|
|
|
│ Hashing: double-SHA256, content-addressed │
|
|
│ Hashing: double-SHA256, content-addressed │
|
|
|
│ Selection: greedy posting selection │
|
|
│ Selection: greedy posting selection │
|
|
@@ -40,37 +40,99 @@ reconstructs every balance, so the ledger is auditable by construction. See
|
|
|
|
|
|
|
|
## Crates
|
|
## Crates
|
|
|
|
|
|
|
|
|
|
+An application depends on **kuatia** alone. It re-exports the whole chain, the
|
|
|
|
|
+SQL backend as `kuatia::sql` behind the `sqlite` or `postgres` feature, so the
|
|
|
|
|
+crates below it are reachable without being named in a manifest. They are
|
|
|
|
|
+published for custom development: a hand-written `Store`, or an embedding of
|
|
|
|
|
+the pure core with no async layer at all.
|
|
|
|
|
+
|
|
|
| Crate | Purpose |
|
|
| Crate | Purpose |
|
|
|
|-------|---------|
|
|
|-------|---------|
|
|
|
-| **kuatia-types** | Domain types — `AccountId`, `Posting`, `Transfer`, `Cent`, etc. |
|
|
|
|
|
-| **kuatia-core** | Pure, sans-IO decision logic — validation, hashing, posting selection. |
|
|
|
|
|
-| **kuatia-storage** | `Store` trait (7 sub-traits), `InMemoryStore`, `store_tests!` conformance macro. |
|
|
|
|
|
-| **kuatia-storage-sql** | SQL-backed `Store` — SQLite and PostgreSQL via sqlx. |
|
|
|
|
|
-| **kuatia** | Async resource layer: `Ledger`, the atomic commit engine, intent-layer API. |
|
|
|
|
|
|
|
+| **kuatia** | What you depend on. Async resource layer: `Ledger`, the atomic commit engine, intent-layer API. |
|
|
|
|
|
+| **kuatia-core** | Pure, sans-IO decision logic: validation, hashing, posting selection. |
|
|
|
|
|
+| **kuatia-types** | Domain types: `AccountId`, `Posting`, `Transfer`, `Asset`, `Amount`. |
|
|
|
|
|
+| **kuatia-money** | The `Cent` monetary type behind `Amount`. Its integer width is swappable and never appears in a public signature. |
|
|
|
|
|
+| **kuatia-storage** | `Store` trait (8 sub-traits), `InMemoryStore`, `store_tests!` conformance macro. |
|
|
|
|
|
+| **kuatia-storage-sql** | SQL-backed `Store`: SQLite and PostgreSQL via sqlx. |
|
|
|
|
|
|
|
|
## Quick Example
|
|
## Quick Example
|
|
|
|
|
|
|
|
|
|
+`kuatia` is the only crate to name in `Cargo.toml`:
|
|
|
|
|
+
|
|
|
|
|
+```toml
|
|
|
|
|
+[dependencies]
|
|
|
|
|
+kuatia = "0.4"
|
|
|
|
|
+tokio = { version = "1", features = ["full"] }
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+An asset is registered by code on first sight, and the handle it returns is
|
|
|
|
|
+what every amount is denominated in. `"100.00"` parses at that asset's own
|
|
|
|
|
+scale. Alice and Bob forbid overdraft, so neither can go negative; `external`
|
|
|
|
|
+is the boundary account where value enters and leaves, and it permits
|
|
|
|
|
+overdraft, so it goes negative by exactly what the ledger holds.
|
|
|
|
|
+
|
|
|
```rust
|
|
```rust
|
|
|
-use kuatia::ledger::Ledger;
|
|
|
|
|
-use kuatia::mem_store::InMemoryStore;
|
|
|
|
|
-use kuatia_core::*;
|
|
|
|
|
|
|
+use kuatia::prelude::*;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
#[tokio::main]
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
let ledger = Ledger::new(InMemoryStore::new());
|
|
let ledger = Ledger::new(InMemoryStore::new());
|
|
|
|
|
|
|
|
- let usd = AssetId::new(1);
|
|
|
|
|
|
|
+ let usd = ledger.asset("USD", "$", 2).await?;
|
|
|
|
|
+
|
|
|
let alice = AccountId::new(1);
|
|
let alice = AccountId::new(1);
|
|
|
let bob = AccountId::new(2);
|
|
let bob = AccountId::new(2);
|
|
|
- let bank = AccountId::new(3); // external account
|
|
|
|
|
-
|
|
|
|
|
- // Create accounts, deposit, pay...
|
|
|
|
|
- // See doc/crates.md for the full API reference.
|
|
|
|
|
|
|
+ let external = AccountId::new(99);
|
|
|
|
|
+
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .create_account(Account::debit_must_not_exceed_credit(alice))
|
|
|
|
|
+ .await?;
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .create_account(Account::debit_must_not_exceed_credit(bob))
|
|
|
|
|
+ .await?;
|
|
|
|
|
+ ledger.create_account(Account::new(external)).await?;
|
|
|
|
|
+
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .commit(
|
|
|
|
|
+ TransferBuilder::new()
|
|
|
|
|
+ .deposit(alice, ("100.00", &usd), external)
|
|
|
|
|
+ .build()?,
|
|
|
|
|
+ )
|
|
|
|
|
+ .await?;
|
|
|
|
|
+
|
|
|
|
|
+ ledger
|
|
|
|
|
+ .commit(
|
|
|
|
|
+ TransferBuilder::new()
|
|
|
|
|
+ .pay(alice, bob, ("25.00", &usd))
|
|
|
|
|
+ .build()?,
|
|
|
|
|
+ )
|
|
|
|
|
+ .await?;
|
|
|
|
|
+
|
|
|
|
|
+ println!("alice: {}", ledger.balance(&alice, &usd).await?);
|
|
|
|
|
+ println!("bob: {}", ledger.balance(&bob, &usd).await?);
|
|
|
|
|
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
+That is `crates/kuatia/examples/quickstart.rs` verbatim. Run it with
|
|
|
|
|
+`cargo run -p kuatia --example quickstart`; a test fails if the two drift.
|
|
|
|
|
+
|
|
|
|
|
+### Persistence
|
|
|
|
|
+
|
|
|
|
|
+The SQLite and PostgreSQL backend is a feature of the same crate:
|
|
|
|
|
+
|
|
|
|
|
+```toml
|
|
|
|
|
+kuatia = { version = "0.4", features = ["sqlite"] }
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+```rust
|
|
|
|
|
+let ledger = Ledger::connect("sqlite://ledger.db?mode=rwc").await?;
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+Only that line changes. The URL scheme picks the backend, and `connect`
|
|
|
|
|
+installs the driver, sizes the pool and applies the schema.
|
|
|
|
|
+
|
|
|
## Documentation
|
|
## Documentation
|
|
|
|
|
|
|
|
[`doc/`](doc/README.md) holds the full documentation: the model, the accounting
|
|
[`doc/`](doc/README.md) holds the full documentation: the model, the accounting
|