Crate rusqlite
Console program94 lines of .st149 lines of generated Rust
name=apple n=1 name=fig n=2 name=pear n=3 error: no such column: nope in SELECT nope FROM fruit at offset 7
On this page
The .st files#
intent.st
structop {
type_app {
exec = {
lang = "Rust"
standard = "2021"
}
}
lib_provider {
Rust_lang = {
source = "StructOp/rust"
version = "@latest"
}
Db = {
source = "Crate/rusqlite"
version = "@latest"
}
}
}main.st
# SQLite through the provider structop bind writes from the rusqlite crate: open a database in
# memory, fill a table, list its rows through a closure, and report a failing query.
ability "value" "db" {
type = Connection
}
ability "value" "name" {
type = String
}
data "value" "name" {
of = ability.value.name
}
ability "value" "n" {
type = Int
}
data "value" "n" {
of = ability.value.n
}
ability "value" "err" {
type = String
}
data "value" "err" {
of = ability.value.err
}
ability "connection_open_in_memory" "Open" {
target = ability.value.db
}
ability "connection_execute_batch" "Fill" {
connection = ability.value.db
sql = "CREATE TABLE fruit(name TEXT, n INT); INSERT INTO fruit VALUES ('pear', 3), ('apple', 1), ('fig', 2);"
}
ability "connection_prepare_query_map" "List" {
connection = ability.value.db
sql = "SELECT name, n FROM fruit ORDER BY n"
f = [ability.row_get_text.Name, ability.row_get_int.N, ability.print.Row]
}
data "connection_prepare_query_map_f" "R" {
of = ability.connection_prepare_query_map.List
}
ability "row_get_text" "Name" {
row = data.connection_prepare_query_map_f.R.row
idx = 0
target = ability.value.name
}
ability "row_get_int" "N" {
row = data.connection_prepare_query_map_f.R.row
idx = 1
target = ability.value.n
}
ability "print" "Row" {
message = "name=${data.value.name.current} n=${data.value.n.current}"
}
ability "connection_prepare_query_map" "Broken" {
connection = ability.value.db
sql = "SELECT nope FROM fruit"
error = ability.value.err
}
ability "print" "Error" {
message = "error: ${data.value.err.current}"
}Input, plan and generated Rust#
bind/rusqlite.bind.st · hints for structop bind
bind/rusqlite.bind.st · hints for structop bind
# rusqlite: SQLite for Rust. P = Nothing passes no query parameters; the then chain keeps the
# borrowed Statement inside one ability; Row::get is pinned once for text and once for numbers.
bind "rusqlite" {
crate = "rusqlite"
version = "0.31.0"
features = ["bundled"]
items = ["Connection::open_in_memory", "Connection::execute_batch", "Connection::prepare", "Row::get"]
}
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
}
}structop plan
structop plan
# program.app will be created
+ program.app (.structop/build/app)
+ ability.value.db
+ ability.value.name
+ ability.value.n
+ ability.value.err
+ ability.connection_open_in_memory.Open
+ ability.connection_execute_batch.Fill
+ ability.connection_prepare_query_map.List
+ ability.row_get_text.Name
+ ability.row_get_int.N
+ ability.print.Row
+ ability.connection_prepare_query_map.Broken
+ ability.print.Error
Plan: 1 to add, 0 to change, 0 to destroy.Generated main.rs
To inspect this generated file, select Rust in intent.st, initialize the project and export its source.
Export locally
structop init
structop apply -source -no-runTry it
structop bind bind/rusqlite.bind.st # from the repository root
cd crate-rusqlite # a folder holding the .st files above
structop init
structop plan
structop apply