StructOp Handbook
Contents Examples Commands
Handbook / What is StructOp?

A worked application: the inventory manager

The inventory manager is a stock list for a small shop, written in StructOp: a window, a command line and a database file, built as a C program and as a Rust program. It puts together what the catalogue’s examples show one at a time: a window, a database reached through a bound library, modules, command-line parameters and tests. This page shows how it is declared, the commands run on it with their recorded output, what is verified and how it works within version 0.1.0.

2targets: C and Rust
13tests, passing on each target
1577lines of declarations in the C build
7improvements it brought to StructOp

What is verified. With StructOp 0.1.0 on macOS on Apple silicon, on 1 December 2026, both builds were initialised, validated, planned and built, and each passed the same 13 tests. The tests drive the command line, and the window is exercised by a scripted session that prints what it does. Runs on Linux and on Intel and AMD processors come next.

On this page

What the application does#

Each item has a code, a name, a quantity, a unit price and a reorder level, and is kept in an SQLite database file. Two front ends share the same rules:

An empty code or name, a quantity that is not a whole number of 0 or more, a price that is not an amount from 0 to 9999.99 and a code already in use are refused with a message, and nothing is written. The database table has constraints that refuse the same, so stock never goes below zero.

Option or command of the built programWhat it does
--db FILEThe database file (default inventory.db); it is created when absent
--sampleAdds four sample items whose codes are not in use yet
add CODE NAME QUANTITY PRICE REORDERAdds an item
in CODE QUANTITY, out CODE QUANTITYReceives or issues stock
list, report, lowEvery item with its value; the totals; only the items to reorder
no commandOpens the window
--demo, --quit_after MSFills the form and presses the buttons once the window is open; closes the window after MS milliseconds

How it is put together#

The C build is 1577 lines of declarations in eight files, with 95 lines of tests in two more. Each file describes one part of the application.

The excerpts below are whole blocks taken from those files.

intent.st
# The inventory manager is a C program: the C language provider, GTK 3 for the window and
# SQLite through the provider that "structop bind" writes from sqlite3.h (bind/sqlite3.bind.st).
structop {
    type_app {
        exec = {
            lang     = "C"
            standard = "c11"
        }
    }

    lib_provider {
        C_lang = {
            source  = "StructOp/c"
            version = "@latest"
        }
        Gui = {
            source  = "StructOp/gtk3"
            version = "@latest"
        }
        Db = {
            source  = "C/sqlite3"
            version = "@latest"
        }
    }
}

The modules. database takes the file as its input and gives the connection, the status and the error text as outputs. sample_item is one declaration used four times with different inputs. The rules in logic.st touch neither the database nor the window, so both front ends call them and the tests check them through the command line.

The bound library and crate. Neither build uses a hand-written SQLite provider. For the C build, structop bind reads the header sqlite3.h and writes the provider C/sqlite3; in the recorded run it wrote 186 ability kinds. For the Rust build it reads the rusqlite crate, version 0.31.0, through the hints below and writes the provider Crate/rusqlite with 6 ability kinds. C libraries and Rust crates explains hints files; the SQLite example shows the hints used for the header.

bind/rusqlite.bind.st · hints for structop bind
# rusqlite for the Rust build of the inventory manager: open a file, run statements that
# return nothing, count the rows a statement changed, and read rows through a closure.
bind "rusqlite" {
    crate    = "rusqlite"
    version  = "0.31.0"
    features = ["bundled"]
    items    = ["Connection::open", "Connection::execute_batch", "Connection::changes", "Connection::prepare", "Row::get"]
}

function "Connection::open" {
    variant "connection_open_path" {
        P = String
    }
}

function "Connection::prepare" {
    then = ["Statement::query_map"]
    variant "connection_prepare_query_map" {
        P = Nothing
        T = Int
    }
}

function "Row::get" {
    variant "row_get_text" {
        I = Size
        T = String
    }
    variant "row_get_int" {
        I = Size
        T = Int
    }
}

The workflow, with its output#

The output in this section and the next three was recorded in one session with StructOp 0.1.0 on macOS, in a fresh copy of the application with an empty home folder. Where lines are left out, a line of three dots stands in their place.

init installs the providers named in intent.st and locks their versions. The SQLite provider is the one structop bind wrote on this machine, so it is installed as a development provider, not a verified package.

structop init
$ structop init
Initializing provider plugins...
- Installed StructOp/c v2.2.2
- Installed StructOp/gtk3 v2.0.3
- Installed C/sqlite3 v3.54.0 (development, not verified)

StructOp has been successfully initialized!
structop validate
$ structop validate
Success! The configuration is valid.

plan lists what apply would build: one program made of 268 abilities, in run order. The abilities of a module copy carry the module’s name.

structop plan
$ structop plan
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.print.NoDatabase
      + ability.if.Front
      + ability.value.at
      + ability.loop.Commands
      + ability.if.Add
      + ability.if.AddWords
...
      + module.sample_lamp.ability.sqlite3_open.Open
      + module.sample_lamp.ability.sqlite3_exec.Insert
      + module.sample_lamp.ability.sqlite3_changes.Count
      + module.sample_lamp.ability.sqlite3_close.Close
      + ability.if.Mode

Plan: 1 to add, 0 to change, 0 to destroy.

apply prints the same plan, builds the program and runs it. Here it adds the four sample items, lists the stock and prints the report.

structop apply
$ structop apply -- --db shop.db --sample list report
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.print.NoDatabase
...
Plan: 1 to add, 0 to change, 0 to destroy.

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

B100 Hex bolt M6: 120 at 0.15 = 18.00
G300 Work gloves: 25 at 8.99 = 224.75
L400 LED lamp: 3 at 12.50 = 37.50
  at or below its reorder level of 4: order 5
T200 Insulating tape: 8 at 2.45 = 19.60
  at or below its reorder level of 10: order 12
4 items, 156 units, stock value 299.85, 2 to reorder

Nothing has changed since, so a second plan is empty, and a second apply only runs the program. This run adds an item, is refused twice and issues stock.

structop plan
$ structop plan
No changes. Your application matches the configuration.
structop apply
$ structop apply -- --db shop.db add C500 "Cable ties" 6 3.2 4 add C500 "Other ties" 1 1 1 out L400 5 out L400 2 report
No changes. Your application matches the configuration.

Added C500 (Cable ties): 6 in stock at 3.20 each.
Refused: the code C500 is already in use.
Refused: there are fewer than 5 of L400 in stock.
Issued 2 of L400.
5 items, 160 units, stock value 294.05, 2 to reorder

graph draws the application in the terminal as a flowchart: a box for each ability at the top level, with a count on one that holds others, and lines for run order, calls, dependencies and data. -depth=1 opens one level of frames. An application of this size is wider than a page, so the block scrolls sideways (in a terminal, structop graph | less -S). These are its first rows:

structop graph
$ structop graph
┌─────────────────────────┐             ┌────────────────────────────────────┐     ┌────────────────────────────────────┐     ┌─────────────────────┐     ┌────────────────────────┐             ┌───────────────────────┐     ┌──────────────────────────────┐     ┌─────────────────────────────────┐             ┌──────────────────────────────────┐     ┌────────────────────────────────────┐             ┌──────────────────────────────────────┐
│                         │             │                                    │     │                                    │     │                     │     │                        │             │                       │     │                              │     │                                 │             │                                  │     │                                    │             │                                      │
│         value.at        ├──────┐      │ function.parse_count (4 abilities) │     │ function.parse_price (7 abilities) │     │ function.line_value │     │ function.needs_reorder │             │ function.order_amount │     │ function.money (5 abilities) │     │ function.sql_text (5 abilities) │             │ function.add_item (29 abilities) │  ┌┄┄┤ function.move_stock (20 abilities) │             │ function.stock_report (15 abilities) │
│                         │      │      │                                    │     │                                    │     │                     │     │                        │             │                       │     │                              │     │                                 │             │                                  │  ┆  │                                    │             │                                      │
└────────────┬────────────┘      │      └────────────────────────────────────┘     └────────────────────────────────────┘     └─────────────────────┘     └────────────────────────┘             └───────────────────────┘     └──────────────────────────────┘     └─────────────────────────────────┘             └─────────────────┬────────────────┘  ┆  └────────────────────────────────────┘             └───────────────────┬──────────────────┘
             │                   │                                                                                                                                                                                                                                                                                                    ┆                   ┆                                                                         ┆
             │                   │                                                                                                                                                                                                                                                                                                    ┆                   ┆                                                                         ┆
             │                   └─────────────────────────┐                                                                                                                                                                                                                                                                          ┆                ┌┄┄┘                                                                         ┆
             │                                          source                                                                                                                                                                                                                                                                        ┆                ┆                                                                            ┆
             ▼                                             ▼                                                                                                                                                                                                                                                                          ┆                ┆                                                                            ┆
┌─────────────────────────┐             ┌────────────────────────────────────┐                                                                                                                                                                                                                                                        ┆                ┆                                                                            ┆
│                         │             │                                    │                                                                                                                                                                                                                                                        ┆                ┆                                                                            ┆
│     value.unit_text     ├──────┐      │           data.value.at            │                                                                                                                                                                                                                                                        ┆                ┆                                                                            ┆
│                         │      │      │                                    │                                                                                                                                                                                                                                                        ┆                ┆                                                                            ┆
└────────────┬────────────┘      │      └────────────────────────────────────┘                                                                                                                                                                                                                                                        ┆                ┆                                                                            ┆
             │                   │                                                                                                                                                                                                                                                                                                    ┆                ┆                                                                            ┆
             │                   │                                                                                                                                                                                                                                                                                                    ┆                ┆                                                                            ┆
             │                   └─────────────────────────┐                                                                                                                                                                                                                                                                          ┆                ┆                                                                            ┆
             │                                          source                                                                                                                                                                                                                                                                        ┆                ┆                                                                            ┆
             ▼                                             ▼                                                                                                                                                                                                                                                                          ┆                ┆                                                                            ┆
┌─────────────────────────┐             ┌────────────────────────────────────┐                                                                                                                                                                                                                                      ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘                                                                            ┆
│                         │             │                                    │                                                                                                                                                                                                                                      ┆                 ┆                                                                                             ┆
│     value.line_text     ├──────┐      │        data.value.unit_text        │                                                                                                                                                                                                                        ┌┄┄┄┄┄┄┄┄┄┄depends┄on┄┄┄┄┄┄┄┄┄┄┄┘                                                           ┌┄┄┄┄┄┄┄┄┄┄┄depends┄on┄┄┄┄┄┄┄┄┄┄┄┄┘
│                         │      │      │                                    │                                                                                                                                                                                                                        ┆             ┆                                                                             ┆
└────────────┬────────────┘      │      └────────────────────────────────────┘                                                                                                                                                                                      ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘             ┆                                        ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘
             │                   │                                                                                                                                                                                                                                  ┆                                               ┆                                        ┆
             │                   │                                                                                                                                                                                                                                  ┆                                               ┆                                        ┆
             │                   └─────────────────────────┐                                                                                                                                                                                                     ┌┄┄┘                                 ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┘                                     ┌┄┄┘
             │                                          source                                                                                                                                                                                                   ┆                                    ┆                                                   ┆
             ▼                                             ▼                                                                                                                                                                                                     ┆                                    ┆                                                   ┆
┌─────────────────────────┐             ┌────────────────────────────────────┐                                                                                                                                                                 ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘                                    ┆                               ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘
│                         │             │                                    │                                                                                                                                                                 ┆                                                      ┆                               ┆
│      value.message      ├──────┐      │        data.value.line_text        │                                                                                                                                                 ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘                                                      ┆             ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘
│                         │      │      │                                    │                                                                                                                                                 ┆                                                                      ┆             ┆
└────────────┬────────────┘      │      └────────────────────────────────────┘                                                                                                                                                 ┆                                                     ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘             ┆
             │                   │                                                                                                                                                                                             ┆                                                     ┆                              ┆
             │                   │                                                                                                                                                                                             ┆                                                     ┆                              ┆
             │                   └─────────────────────────┐                                                                                                                                     ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘                                    ┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┘
             │                                          source                                                                                                                                   ┆                                                                  ┆                ┆
             ▼                                             ▼                                                                                                                                     ┆                                                                  ┆                ┆
┌─────────────────────────┐             ┌────────────────────────────────────┐                                                                                                            ┌┄┄┄┄┄┄┘                                                               ┌┄┄┘                ┆
│                         │             │                                    │                                                                                                            ┆                                                                      ┆                   ┆
│         value.ok        ├──────┐      │         data.value.message         │                                                                                                            ┆                                                                      ┆                   ┆
│                         │      │      │                                    │                                                                                                            ┆                                                                      ┆                   ┆
└────────────┬────────────┘      │      └────────────────────────────────────┘                                                                                                            ┆                                                                      ┆                   ┆
...

The whole graph has nodes for 239 abilities and 29 data blocks, joined by 383 arrows. Without -depth the drawing shows every ability, and -format=mermaid and -json give the same graph to other tools; see Visualise.

A change, and the plan that shows only that change#

The shop decides to order up to three times the reorder level instead of twice. That is one line in one function of logic.st:

logic.st, edited
# How many to order: enough to reach three times the reorder level (at least one).
ability "function" "order_amount" {
    params  = [{ name = "qty", type = Int }, { name = "level", type = Int }]
    returns = Int
    return  = max(level * 3 - qty, 1)
}

The plan names that function and the expression that changed, and nothing else:

structop plan
$ structop plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      ~ ability.function.order_amount
          return: max(level * 2 - qty, 1) -> max(level * 3 - qty, 1)

Plan: 0 to add, 1 to change, 0 to destroy.

graph -changed draws the same picture with the changed ability marked ~:

structop graph -changed
$ structop graph -changed
┌─────────────────────────┐             ┌────────────────────────────────────┐     ┌────────────────────────────────────┐     ┌─────────────────────┐     ┌────────────────────────┐             ┌─────────────────────────┐     ┌──────────────────────────────┐     ┌─────────────────────────────────┐             ┌──────────────────────────────────┐     ┌────────────────────────────────────┐             ┌──────────────────────────────────────┐
│                         │             │                                    │     │                                    │     │                     │     │                        │             │                         │     │                              │     │                                 │             │                                  │     │                                    │             │                                      │
│         value.at        ├──────┐      │ function.parse_count (4 abilities) │     │ function.parse_price (7 abilities) │     │ function.line_value │     │ function.needs_reorder │             │ ~ function.order_amount │     │ function.money (5 abilities) │     │ function.sql_text (5 abilities) │             │ function.add_item (29 abilities) │  ┌┄┄┤ function.move_stock (20 abilities) │             │ function.stock_report (15 abilities) │
│                         │      │      │                                    │     │                                    │     │                     │     │                        │             │                         │     │                              │     │                                 │             │                                  │  ┆  │                                    │             │                                      │
└────────────┬────────────┘      │      └────────────────────────────────────┘     └────────────────────────────────────┘     └─────────────────────┘     └────────────────────────┘             └─────────────────────────┘     └──────────────────────────────┘     └─────────────────────────────────┘             └─────────────────┬────────────────┘  ┆  └────────────────────────────────────┘             └───────────────────┬──────────────────┘
...
~ changed

graph -impact also marks what a change can reach. This rule feeds the stock report, which every command prints, so the view marked 253 of the other 267 nodes as affected and the remaining 14 as possibly affected: a change to a shared rule reaches the whole application, and the view shows it before you build.

The tests state the old rule, so five of the 13 runs flag the change, each showing the expected and the actual output. The plan shows what changed in the declarations; the tests show which recorded behaviour changes with it.

structop test · exit status 1
$ structop test
tests/database.sttest
  FAIL round_trip
      output_equals: the output differs; expected:
        Added K1 (O'Neill's key): 10 in stock at 2.50 each.
        Received 5 of K1.
        Issued 12 of K1.
        K1 O'Neill's key: 3 at 2.50 = 7.50
          at or below its reorder level of 3: order 3
        1 items, 3 units, stock value 7.50, 1 to reorder
      output:
        Added K1 (O'Neill's key): 10 in stock at 2.50 each.
        Received 5 of K1.
        Issued 12 of K1.
        K1 O'Neill's key: 3 at 2.50 = 7.50
          at or below its reorder level of 3: order 6
        1 items, 3 units, stock value 7.50, 1 to reorder
...
Failure! 8 passed, 5 failed.

apply rebuilds the program and runs it on the same database file. The amounts to order follow the new rule, and the next plan is empty again.

structop apply -- --db shop.db list report
$ structop apply -- --db shop.db list report
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      ~ ability.function.order_amount
          return: max(level * 2 - qty, 1) -> max(level * 3 - qty, 1)

Plan: 0 to add, 1 to change, 0 to destroy.

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

B100 Hex bolt M6: 120 at 0.15 = 18.00
C500 Cable ties: 6 at 3.20 = 19.20
G300 Work gloves: 25 at 8.99 = 224.75
L400 LED lamp: 1 at 12.50 = 12.50
  at or below its reorder level of 4: order 11
T200 Insulating tape: 8 at 2.45 = 19.60
  at or below its reorder level of 10: order 22
5 items, 160 units, stock value 294.05, 2 to reorder
structop plan
$ structop plan
No changes. Your application matches the configuration.

The plan is this short because the change edits one ability. A change that adds or removes abilities also lists the abilities whose place in the run order moves.

Two targets and thirteen tests#

The Rust build is a second project in the folder rust/: 1620 lines of declarations on StructOp/rust, StructOp/gtk-rs and Crate/rusqlite. The rules, the inputs and the tests are the same, byte for byte: logic.st and inputs.st (310 lines) and both test files are copies. The database declarations, the command line and the window are written once for each target, because the two SQLite providers have different ability kinds.

structop init, in rust/
$ structop init
Initializing provider plugins...
- Installed StructOp/rust v2.2.2
- Installed StructOp/gtk-rs v2.0.1
- Installed Crate/rusqlite v0.31.0 (development, not verified)

StructOp has been successfully initialized!
structop apply, in rust/
$ structop apply -- --db shop.db --sample list report
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.print.NoDatabase
...
Plan: 1 to add, 0 to change, 0 to destroy.

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

B100 Hex bolt M6: 120 at 0.15 = 18.00
G300 Work gloves: 25 at 8.99 = 224.75
L400 LED lamp: 3 at 12.50 = 37.50
  at or below its reorder level of 4: order 5
T200 Insulating tape: 8 at 2.45 = 19.60
  at or below its reorder level of 10: order 12
4 items, 156 units, stock value 299.85, 2 to reorder

The Rust program prints the same list and report as the C program above. structop test builds the program in a temporary copy of the project for each run, so each run has a database file of its own. The C build’s result:

structop test
$ structop test
tests/database.sttest
  PASS round_trip
  PASS duplicate_code_writes_nothing
  PASS sample_items_and_report
  PASS sample_codes_are_in_use
  PASS missing_folder_is_reported

tests/rules.sttest
  PASS money_and_reorder_rules
  PASS empty_fields_are_refused
  PASS bad_quantities_are_refused
  PASS bad_prices_are_refused
  PASS stock_rules
  PASS unknown_command_and_short_commands
  PASS short_add
  PASS low_stock_filter

Success! 13 passed, 0 failed.

The Rust build’s structop test printed the same lines, also ending Success! 13 passed, 0 failed.

What the 13 runs check:

Testing describes test files and structop test.

The window#

Run without a command, the program opens the window: a menu bar, the item list, five entry fields, three buttons, the report and a status line. A scripted session verifies it: --demo types into the form and presses the buttons as a person would, through the same functions the command line calls, and the program prints each status message and the final report. This run kept the window open for 2.5 seconds:

structop apply
$ structop apply -- --db window.db --sample --demo --quit_after 2500
No changes. Your application matches the configuration.

window: Added C500 (Cable ties, 100): 6 in stock at 3.20 each.
window: Refused: the code C500 is already in use.
window: Refused: the quantity 'six' is not a whole number of 0 or more.
window: Issued 4 of C500.
window: Refused: there are fewer than 4 of C500 in stock.
window: Received 4 of C500.
window report:
5 items, 162 units, stock value 319.05
At or below the reorder level:
  L400 LED lamp: 3 left, reorder at 4, order 5
  T200 Insulating tape: 8 left, reorder at 10, order 12

The Rust build’s window, run with the same options, printed the same lines.

What building it gave StructOp#

Building complete applications is how StructOp is hardened. This one brought seven improvements to StructOp 0.1.0 and to the official providers, and each is covered by StructOp’s own tests. Five of them are shown by catalogue examples you can read and run: a library call with a callback inside a function, and a provider’s constant read inside a module, a window program with command-line parameters, a database query inside a window handler and scan into a Float64. Since StructOp/c and StructOp/rust 2.2.2, scan also reads numbers the same way on both targets (Scan not read), and since StructOp/gtk3 and StructOp/gtk2 2.0.3 a window program reads and prints decimal numbers the same way whatever the desktop’s language settings. The price check relies on both.

How the application works within version 0.1.0. These are the design choices the application makes with what 0.1.0 offers. Scope and roadmap lists what each release covers.

Try the pattern#

Downloads and installation has the installers and lists the supported systems. With the program installed, the pattern of this application is small enough to start from the catalogue: rules as functions, a module that owns the database, two front ends that call the same functions, and tests through the command line.

Part of the patternStart fromRead
A database through a bound C libraryLib sqlite, Lib sqlite functionC libraries and Rust crates
The same through a Rust crate, in a windowCrate rusqlite, Crate rusqlite guiRust crates
Rules as functions and a module used more than onceMod localAbilities and functions, Modules and outputs
Command-line parameters, also in a window programParam flags, Gui paramsInputs and values
Tests through the command lineHelloTesting

The commands are the ones recorded above. A provider written by structop bind on your machine is a development provider, so set STRUCTOP_DEV_BINDINGS=1 in that shell first (why). The C build needs a C compiler, clang for structop bind, and the development files of SQLite and GTK 3; the Rust build needs Cargo, the nightly Rust toolchain once for structop bind, and the rusqlite crate.

The workflow
export STRUCTOP_DEV_BINDINGS=1    # the bound SQLite provider is a development provider
structop bind bind/sqlite3.bind.st    # the hints file shown on the SQLite example page
structop init
structop validate
structop plan
structop apply -- --sample list report
structop test