# Getting started

> Install the crate, compile a price book, rate a month and print the bill lines with their explanations.


To run the operator Desk with sample data, follow [Run the services](../docs/services/#run-them). To use the rating library in an application, supply the book files, subscription dates and measured usage. The caller controls storage and time.

Add the rating crate and `time` to your Rust project:

```sh
cargo add telco-core
cargo add time --features macros
```

## A book, a month, a bill

```rust
use telco_core::{PeriodState, Subscription, UsageRecord, compile, rate, totals};
use time::macros::date;

let book = compile(&[(
    "book.toml".to_owned(),
    br#"
[book]
id = "example"
version = "2026-11"
prices = "gross"
currency = "EUR"

[tax.standard]
rates = [{ from = "2021-01-01", percent = "19" }]

[kind.calls-mobile]
label = "Anrufe in Mobilfunknetze"
measure = "seconds"

[plan.fibre]
name = "Glasfaser 1000"
tax = "standard"

[plan.fibre.recurring.base]
label = "Glasfaser 1000"
amount = "49.99"

[plan.fibre.discount.welcome]
label = "Aktionsrabatt"
amount = "-30.00"
scope = "base"
months = 3

[plan.fibre.usage.calls-mobile]
label = "Anrufe in Mobilfunknetze"
price = "0.19"
per = "1 min"
round = "60/60"
"#
    .to_vec(),
)])?;

let subscription = Subscription { window: None, plan: "fibre".into(), start: date!(2026 - 10 - 01), end: None };
let usage = [UsageRecord {
    id: "c1".into(),
    kind: "calls-mobile".into(),
    date: date!(2026 - 11 - 12),
    quantity: 754, // seconds
}];

let rated = rate(&book, &subscription, &PeriodState::new("2026-11".parse()?), &usage)?;
let bill = totals(&book, &rated.charges)?;

let lines: Vec<(String, String)> =
    bill.lines.iter().map(|l| (l.label.clone(), l.amount.to_string())).collect();
assert_eq!(lines, [
    ("Glasfaser 1000".into(), "49.99".into()),
    ("Aktionsrabatt (Monat 2 von 3)".into(), "-30.00".into()),
    ("Anrufe in Mobilfunknetze (13 Min.)".into(), "2.47".into()),
]);
assert_eq!(bill.gross.to_string(), "22.46");
assert_eq!(bill.vat.to_string(), "3.59");

// Every charge says why.
assert!(rated.charges[2].explanation.contains("im 60/60-Takt gerundet auf 13 Min."));
# Ok::<(), telco_core::Refusal>(())
```

## From files

In an application, read the book directory and pass every `*.toml` with its path
relative to the book root. The order does not matter: the book's content hash is the
same either way.

```rust,no_run
let dir = std::path::Path::new("pricebooks/example-fibre");
let mut files = Vec::new();
for entry in std::fs::read_dir(dir.join("plans"))? {
    let path = entry?.path();
    files.push((format!("plans/{}", path.file_name().unwrap().to_string_lossy()), std::fs::read(&path)?));
}
files.push(("book.toml".to_owned(), std::fs::read(dir.join("book.toml"))?));
let book = telco_core::compile(&files)?;
println!("{} {}", book.id(), book.hash());
# Ok::<(), Box<dyn std::error::Error>>(())
```

The [CLI](../docs/cli/) does exactly this, recursively.
