# Use the Rust crates

> Seal, recompute and render bills, keep the books and assess collections in your own Rust program, with pure crates that need no I/O, clock or floats.

The crates are the service without the service: pure functions over plain values. They
read no clock, open no file or socket, draw no randomness and use no floating-point
numbers. Dates, keys and identifiers are arguments, so a result depends only on its
inputs.

## Add the crates

```toml
[dependencies]
konto-core = "0.1"
konto-bill = "0.1"
konto-render = "0.1"    # documents
konto-ledger = "0.1"    # books
konto-collect = "0.1"   # collections
```

The minimum supported Rust version is 1.96. `konto-core`, `konto-bill` and
`konto-collect` also build for `wasm32-unknown-unknown`.

## Seal and recompute a bill

```rust
use konto_bill::{BillRequest, InvoiceNumber, Verdict, preview, recompute, seal, variance};

let request: BillRequest = serde_json::from_str(&json)?;     // money as decimal strings
let next = preview(&request)?;                                // „Was kommt auf die nächste Rechnung?“
let bill = seal(&request, InvoiceNumber { series: "R-2027".into(), sequence: 42 }, Some(&previous))?;
assert_eq!(recompute(&bill), Verdict::Matches);
println!("{}", recompute(&bill).message_de());                // Stimmt auf den Cent.
println!("{}", variance(&last_month, &bill)?.summary_de());   // Der Rechnungsbetrag ist um …
```

`seal` refuses a number that does not follow `previous` in its series. To put sealing
behind review-then-confirm, use `review_seal` and `confirm_seal` with a `ReviewKey`;
`assemble` builds the request from held charges, sections and open payments, as kontod
does.

## Render it

```rust
use konto_render::{Syntax, girocode, html, notice, pdf, xrechnung};

let xml = xrechnung(&bill, Syntax::Ubl)?;   // validated against XRechnung 3.0 first
let file = pdf(&bill, None)?;               // PDF/A-3b + PDF/UA-1 with the e-invoice inside
let page = html(&bill, None);               // the German bill, no script
let qr = girocode(&bill);                   // Some(EPC payload) for a transfer
let mail = notice(&bill);                   // subject and text of the message
```

## Keep the books

`konto-ledger` posts bills, receives payments and allocates them with review, then
confirm; see [Ledger](ledger.md).

## Errors

Every fallible function returns `konto_core::Refusal`: a stable `kind`, a German
`message` and optionally the `subject` it concerns. `kind.code()` is the stable code and
`kind.hint_de()` what to do ([Refusal codes](refusals.md)).

## API documentation

```sh
cargo doc --workspace --no-deps --open
```
