Getting started
This guide writes a rule, tests it, runs it from the command line, and calls it from a Rust program. For Go, follow Getting started with Go once you have a rule.
Requires Rust 1.99 or newer and a C++ compiler. Bree builds the Luau VM and the Luau type checker from source, so there is nothing else to install.
Install the bree command
cargo install --git https://github.com/breeso/bree bree-cliStart a rules directory
In your project:
$ bree init
wrote rules/bree.d.luau
wrote rules/free_shipping.luau
wrote rules/free_shipping.test.luau
Next: bree checkbree.d.luau declares what Bree adds to Luau, for your editor; see Editor setup. The other two files are an example rule and its tests. rules/free_shipping.luau:
--!strict
-- Whether an order ships free.
export type Country = "CA" | "US"
export type Input = {
-- The order total before tax, in cents.
order_total_cents: int,
country: Country,
}
export type Output = {
free: boolean,
}
local THRESHOLD_CENTS = { CA = 7500, US = 5000 }
return function(input: Input): Output
if input.order_total_cents < 0 then
invalid("order_total_cents", "must not be negative")
end
return { free = input.order_total_cents >= THRESHOLD_CENTS[input.country] }
endA rule is a file that exports an Input type and an Output type and returns a function from one to the other. Its name is its path without .luau: free_shipping. int is Bree's whole-number type, and invalid rejects the input with a message for one field. The comments are kept: they become doc comments on the generated Rust and Go types. Rules and types covers the rest.
rules/free_shipping.test.luau:
--!strict
local free_shipping = require("./free_shipping")
test("US orders ship free from $50", function()
expect(free_shipping({ order_total_cents = 5000, country = "US" })).toEqual({ free = true })
end)
test("Canadian orders under $75 pay for shipping", function()
expect(free_shipping({ order_total_cents = 7499, country = "CA" }).free).toBe(false)
end)
test("a negative total is rejected", function()
expect(function()
free_shipping({ order_total_cents = -1, country = "US" })
end).toBeInvalid("order_total_cents")
end)Check it
$ bree check
ok free_shipping 3 tests passed
1 rule, 3 tests passedbree check type checks every rule, module and test in strict mode, then runs the tests. It exits 1 if anything fails.
Run it
$ echo '{"order_total_cents": 8000, "country": "CA"}' | bree run free_shipping
{
"rule": "free_shipping",
"digest": "sha256:f7c9611f58d4f678cfc0ae1b74b66a9f69e51dc05aad015e1f54b18e5917855c",
"output": {
"free": true
}
}The digest identifies the exact source that decided: the rule and every module it requires. Log it with a decision and you can always find the code that made it.
Call it from Rust
Add Bree to your crate's Cargo.toml:
[dependencies]
bree = { git = "https://github.com/breeso/bree" }Add a build.rs next to Cargo.toml, so the crate also rebuilds when you add a rule file (edits to existing files already rebuild it):
fn main() {
println!("cargo::rerun-if-changed=rules");
}And in src/main.rs:
mod rules {
bree::rules!("rules");
}
use rules::free_shipping::{self, Country, Input};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let engine = bree::Engine::embedded(&rules::RULES)?;
let decision = free_shipping::decide(&engine, &Input {
order_total_cents: 8_000,
country: Country::Ca,
})?;
println!("free shipping: {}", decision.output.free);
Ok(())
}$ cargo run
free shipping: truebree::rules!("rules") read rules/ while the crate compiled: it type checked the rule, ran its three tests, generated Input, Output and Country from the Luau types, and embedded the source in the binary as rules::RULES. Rust lists everything it generates.
Break it
This is what Bree is for. Each of these mistakes fails the build.
Misspell a field in the rule (input.order_totl_cents), and cargo build stops at the macro:
error: rules/free_shipping.luau:22: Key 'order_totl_cents' not found in table 'Input'
--> src\main.rs:2:5
|
2 | bree::rules!("rules");
| ^^^^^^^^^^^^^^^^^^^^^Change >= to > in the rule. The types are still fine, but a test now fails, and so does the build:
error: rules/free_shipping.test.luau:5: test "US orders ship free from $50" failed
expected { free = true }
got { free = false }
--> src\main.rs:2:5Write a field the rule does not have in your Rust code (here order_total), and the compiler rejects it, because Input is generated from the rule. Renaming a field in the rule breaks the Rust code that uses the old name the same way:
error[E0560]: struct `Input` has no field named `order_total`
--> src\main.rs:11:9
|
11 | order_total: 8_000,
| ^^^^^^^^^^^ `Input` does not have this field
|
= note: available fields are: `order_total_cents`bree check reports the same problems as the build, so you can run it while you edit, or let your editor show them as you type (Editor setup).
Next steps
- Rules and types: what a rule can declare, share and use.
- Tests:
test,expect, and what a failure tells you. - Laravel Rust: a service provider and a handler, with a complete example application.
- Go: the same rules from Go and Laravel Go.