Bree

Business rules that fail the build when they are wrong

Write each rule in Luau, with its input and output types, and its tests in a file beside it. Bree type checks the rules and runs the tests while your Rust or Go application compiles, then evaluates them inside your process.

Get started Source on GitHub

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] }
end

cargo build

   Compiling shop v0.1.0 (/work/shop)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.99s
The rule bree init writes. Changing one character keeps the types valid, but a test fails, so the build does too.

Tests sit next to the rule

A rule that type checks can still be wrong. Its tests use test and expect, are type checked like the rule, and run on bree check, on every cargo build or go generate, and every time an application loads the rules.

Writing tests

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)

Your code gets types from the rule

In Rust, bree::rules! generates structs and enums from each rule's Input and Output. In Go, go generate runs bree-gen, which does the same. Rename a field in the rule, and code that still uses the old name stops compiling.

Rust · Go · What each check guarantees

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(())
}

Decisions run in your process

  • Rust links the engine as a crate, and Go links the same engine through cgo. There is no server to run.
  • About 0.14 ms per decision, measured at the median.
  • Each call gets a fresh sandbox with no files, network, clock or randomness, and step, memory and time limits.
  • Input and output are checked against the rule's types on every call. invalid(field, message) rejects input the business does not allow.
  • Every decision carries a digest of the exact source that made it, so you can find the code behind any logged decision.

The runtime

bree run

$ echo '{"order_total_cents": 8000, "country": "CA"}' | bree run free_shipping
{
  "rule": "free_shipping",
  "digest": "sha256:f7c9611f58d4f678cfc0ae1b74b66a9f69e51dc05aad015e1f54b18e5917855c",
  "output": {
    "free": true
  }
}

Pick your stack

Rust
A crate and a macro. Rules are compiled in and checked by cargo build.
Laravel Rust
A service provider, a Bree singleton, and an example application.
Go
go generate checks the rules and writes Go types. Linux only for now.
Laravel Go
A package that turns invalid() into field validation errors.
Anything else
bree serve puts the same engine behind HTTP.

Try it in a terminal

Requires Rust 1.99 or newer and a C++ compiler. The first install builds Luau from source, which takes a few minutes.

cargo install --git https://github.com/breeso/bree bree-cli
bree init     # an example rule, its tests, and editor types in rules/
bree check    # type check the rules and run the tests

Add it to a Rust crate

[dependencies]
bree = { git = "https://github.com/breeso/bree" }

Call a rule from Rust

Add it to a Go module

git clone https://github.com/breeso/bree ~/bree
export CGO_LDFLAGS="-L$(bash ~/bree/scripts/libbree.sh)"
go get github.com/breeso/bree-go@main
go generate ./...

Getting started with Go